How To Limit User’s Access To The Linux System

One of my Friend asked me how to allow an user to do only certain tasks, and execute certain commands. The user shouldn’t change the environment variables/paths, can’t visit to the other directories except his home directory, can’t switch to other users etc. The user can only be able to execute few commands assigned by the system administrator. Is that possible? Yes! This is where Restricted Shell comes in help. Using Restricted Shell, we can easily limit user’s access to the Linux system. Once you put the users in restricted shell mode, they are allowed to execute only limited set of commands.

In this brief tutorial, we will talk about how to do this in Linux. I tested this guide on CentOS 7 minimal server. However, It will work on most Unix-like distributions.

Limit User’s Access To The Linux System Using Restricted Shell

What is Restricted Shell?

First, let me clarify what exactly Restricted Shell is. It is not a separate shell like Bash, Korn Shell etc. If you start any existing shell using “rbash”, “–restricted”, “-r” options, then it will become Restricted shell. For instance, the Bourne shell can be started as a restricted shell with the command bsh -r, and the Korn shell with the command ksh -r.

The Restricted Shell will limit the users from executing most commands and from changing the current working directory. The Restricted Shell will impose the following restrictions to the users.

It will not allow you to execute cd command. So you can’t go anywhere. You can simply stay in the current working directory.

It will not allow you to modify the values of $PATH, $SHELL, $BASH_ENV, or $ENV environmental variables.

It will not allow you to execute a program that contains a /(slash) character. For example, you can’t run /usr/bin/uname or ./uname command. You can however execute uname command. In other words, you are allowed to run the commands in the current path only.

It will not allow you to get out of the restricted shell mode within scripts.

It will not allow you to turn off restricted shell mode with ‘set +r’ or ‘set +o restricted’.

This can be very useful when a large number of users are using a shared system. So, If you want to allow the users to execute only specific commands, Restricted Shell is one way to do this.

Using Restricted Shell mode

First, create a symlink called rbash from Bash as shown below. The following commands should be run as root user.

# ln -s /bin/bash /bin/rbash

Next, create an user called “ostechnix” with rbash as his/her default login shell.

# useradd ostechnix -s /bin/rbash

Set password to the new user.

# passwd ostechnix

Create a bin directory inside the home folder of the the new user.

# mkdir /home/ostechnix/bin

Now, we need to specify which commands the user can run.

Here, I am going to let the user to run only “ls”, “mkdir”, and “ping” commands. You can assign any commands of your choice.

To do so, run the following commands:

# ln -s /bin/ls /home/ostechnix/bin/ls

# ln -s /bin/mkdir /home/ostechnix/bin/mkdir

# ln -s /bin/ping /home/ostechnix/bin/ping

Now, you understand why we created the “bin” directory in the earlier step. The users can’t run any commands except the above three commands.

Next, prevent the user from modifying .bash_profile.

# chown root. /home/ostechnix/.bash_profile

# chmod 755 /home/ostechnix/.bash_profile

Edit /home/ostechnix/.bash_profile file:

# vi /home/ostechnix/.bash_profile

Modify the PATH variable like below.

PATH=$HOME/bin

Save and close the file by pressing ESC key followed by :wq.

Now when the user logs in, the restricted shell(rbash) will run as the default login shell and read the .bash_profile, which will set PATH to $HOME/bin so that the user will only be able to run the ls, mkdir and ping commands. The restricted shell will not allow the user to change PATH, and the permissions on .bash_profile will not allow the user to alter the environment to bypass the restrictions during the next login session.

Verifying Rbash

Now, log out from root user and log in to the newly created user i.e ostechnix in our case.

Then, run some commands to check whether it works or not. For example, I want to clear the Terminal.

To do so, I ran:

$ clear

Sample output:

-rbash: clear: command not found

You can’t come use cd command to change to the different directory.

$ cd /root

Sample output:

-rbash: cd: restricted

You can’t redirect the output using > operator either.

$ cat > file.txt

Sample output:

-rbash: file.txt: restricted: cannot redirect output

The user “ostechnix” is allowed to use only the commands assigned by you(the system admin, of course). In our case, the user can execute ls, mkdir and ping commands.

$ ls

$ mkdir dir1

$ ping ostechnix.com

Apart from these three commands, the user can’t do anything. S/He is completely under your control. If you want to assign more commands to him/her, log in to the root user again and assign the commands as shown below.

For example, I want to allow him/her to execute rm command, so I ran the following command as root user.

It will not allow you to execute “cd” command. So you can’t go anywhere. You can simply stay in the current working directory. If you allow the “cd” command, what is the point of using Restricted Shell? The user can go anywhere and do whatever he wants. There won’t be any restriction.

Anyway, try the following and let me know if it works.

Run the following command to allow “cd” command:

# ln -s /bin/cd /home/ostechnix/bin/cd

Make sure you have created the “/home/ostechnix/bin” directory. Now, the user can able to use “cd” command.

This is a really helpful article and exactly what I needed in a clear manner and thanks for that. But I have a question, if I wanted to allow users to run certain Python scripts from an alias stored in the /opt/ directory, how would I allow a user to run a command say

sorry ignore the above about path . it is working fine with PATH=$HOME/bin , it was rather /home/userx/bin directory permission issue . thanks again for this intuitive article .

actually I wanted this user to have the right to read some system logs like /var/log/messages , so have added sudo privileges (/etc/sudoers) to user group .
after doing so , the user was no more restricted to those commands … is their a way to have : root privileged user with restricted commands ?