How to block command, let say mkdir for specific user ?
What I did just created read-only function and store in users profile ~/.bashrc
/bin/mkdir() {
echo "mkdir command not allow for you"
}
mkdir() {
echo "mkdir command not allow for you"
}
./mkdir() {
echo "mkdir command not allow for you"
}
readonly -f /bin/mkdir
readonly -f mkdir
readonly -f ./mkdir
Test:
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="691b08011c05291c0b1c071d1c">[email protected]</a>:~$ cd /bin/ <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6b4a7aeb3aa86b3a4b3a8b2b3">[email protected]</a>:/bin$ ./mkdir /home/rahul/ggg mkdir command not allow for you <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d0f1c1508113d081f08130908">[email protected]</a>:/bin$ cd <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f381929b869fb38691869d8786">[email protected]</a>:~$ mkdir testing mkdir command not allow for you <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f587949d8099b58097809b8180">[email protected]</a>:~$ /bin/mkdir testing mkdir command not allow for you
So my question is What should be the way of achieving this ? is there any tool for this ?
Update 1 # But if user is smart , he could copy mkdir binary and rename it and use it . So how to achieve this ?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I don’t know how to do it with bash, but I know of another shell that restricts the user environment: lshell (limited shell).
A quick overview of configuration
Lshell is configured via an INI file. By default, it holds a whitelist of allowed commands, but it can be easily configured to prohibit user from using a specific command.
This configuration (default conf /etc/lshell.conf) prohibits user foo from using mkdir:
[foo] allowed = 'all' - ['mkdir', 'bash', 'sh', 'csh', 'dash', 'env']
In order to configure a user account to use lshell by default, you must:
chsh -s /usr/bin/lshell foo
Lshell can do more, like:
- 3 levels of granularity: user, group, all.
- Can restrict access to certain paths in the system.
- Can restrict the use of certain characters (like
|). - Can restrict the use of certain commands only over SSH.
And more.
Update 1# Added Test Result :
rahul:~$ which bash /bin/bash rahul:~$ dd if=$(which bash) of=my_bash *** forbidden syntax: dd if=$(which bash) of=my_bash rahul:~$ bash *** forbidden command: bash rahul:~$ cp /bin/bash my_bash *** forbidden path: /bin/bash rahul:~$ /bin/bash *** forbidden command: /bin/bash rahul:~$ sh *** forbidden command: sh rahul:~$ dash *** forbidden command: dash rahul:~$ env bash *** forbidden command: env rahul:~$ cp /bin/mkdir mycreatedir *** forbidden path: /bin/mkdir
Method 2
The way I usually implement this kind of restrictions requires that several conditions are met, otherwise the restriction can be easily circumvented:
- The user does not belong to the
wheelgroup, the only one authorized to usesu(enforced via PAM). -
The user is given a properly secured
rbashwith a read-only PATH pointing to a private~/bin, this~/bin/directory contains links to simple utilities:$ ll ~/bin total 0 lrwxrwxrwx. 1 root dawud 14 Sep 17 08:58 clear -> /usr/bin/clear* lrwxrwxrwx. 1 root dawud 7 Sep 17 08:58 df -> /bin/df* lrwxrwxrwx. 1 root dawud 10 Sep 17 08:58 egrep -> /bin/egrep* lrwxrwxrwx. 1 root dawud 8 Sep 17 08:58 env -> /bin/env* lrwxrwxrwx. 1 root dawud 10 Sep 17 08:58 fgrep -> /bin/fgrep* lrwxrwxrwx. 1 root dawud 9 Sep 17 08:58 grep -> /bin/grep* lrwxrwxrwx. 1 root dawud 10 Sep 17 08:58 rview -> /bin/rview* lrwxrwxrwx. 1 root dawud 13 Sep 17 08:58 rvim -> /usr/bin/rvim* lrwxrwxrwx. 1 root dawud 13 Sep 17 08:58 sudo -> /usr/bin/sudo* lrwxrwxrwx. 1 root dawud 17 Sep 17 08:58 sudoedit -> /usr/bin/sudoedit* lrwxrwxrwx. 1 root dawud 13 Sep 17 08:58 tail -> /usr/bin/tail* lrwxrwxrwx. 1 root dawud 11 Sep 17 08:58 wc -> /usr/bin/wc*
-
the user is given a restricted, read-only environment (think of stuff like
LESSSECURE,TMOUT,HISTFILEvariables). - the user is mapped to the SELinux user
staff_uand given rights to execute commands as other user as required viasudo. -
the user’s
/home,/tmpand possibly/var/tmpare polyinstantiated via/etc/security/namespace.conf:/tmp /tmp/.inst/tmp.inst-$USER- tmpdir:create root /var/tmp /tmp/.inst/var-tmp.inst-$USER- tmpdir:create root $HOME $HOME/$USER.inst/ tmpdir:create root
Also,
/etc/security/namespace.initmakes all skeletal files readonly for the user and owned byroot.
This way you can choose whether $USER can execute mkdir on his/her own behalf (via a link in the private ~/bin directory, provisioned via /etc/skel, as explained above), on behalf of other user (via sudo) or none at all.
Method 3
Add a dummy group, add the user to that group, chown root:somegroup /bin/mkdir, chmod g-x /bin/mkdir. Note that this relies on the user not being able to modify their groups. IIRC this is true in GNU/Linux but not in some other Unices.
Method 4
The best as i have tested is to use Profile.d best & safest way
Step # 1 (Create a Alias File)
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90e2ffffe4d0fef5e7e2f2f5">[email protected]</a> ~]# vim /etc/customalias.sh
Add Below lines :
alias rm="echo remove contenet is restricted" alias poweroff="echo Poweroff is restricted" alias chmod="echo Change Permission is restristed"
Save & quit
Step # 2 (Create Profile loader)
/etc/profile.d/ this location contains files for bash completion
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e1c01011a2e000b191c0c0b">[email protected]</a> ~]# vim /etc/profile.d/customsource.sh
Add below lines under the files these lines will block mentioned commands for below users
if [ `whoami` == "user1" ] && [ `whoami` == "user2" ]; then
source /etc/customalias.sh
fi
save and quit
Now Exit and relogin
Regards,
-Mansur
Method 5
install sudoers and try to configure there whose users and what command.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0