Open In App

Shell Scripting – Restricted Shell

Shell is one of the most important and powerful tools available in GNU/Linux-based systems. One can control the entire system if used correctly, this exact feature makes the shell a double-edged sword. It can potentially harm the system if one executes a system command without knowing its underlying details and consequences. To prevent this we use Restricted Shell.

Restricted Shell – rbash

A restricted shell provides an extra layer of security and restricts certain features of the shell. The restriction applies to the commands and scripts that are executed using the shell. One can start a restricted shell session using the rbash command or by using the -r flag supplied during the invocation (returns 0 if executed successfully), this can be used to set up a more controlled environment than the standard shell.



$ rbash

or

$ bash -r

 

Restrictions Imposed

Once a restricted shell session has been activated most of the standard system command support is revoked. The following operations are not allowed or disabled in restricted shell,



Let us test few restrictions while inside the restricted shell,

$ cd

 

$ unset PATH

 

$ /usr/bin/ping

 

$ source /home/onworks/.bashrc

 

$ echo "Hello" > /tmp/hello.txt

 

$ exec tail -f /var/log/messages

 

$ enable -d wait

 

1. Restricting a User

We can restrict a user to use only a restricted shell during their sessions, Let us create a new user called ‘lucy’ and set their default shell to rbash. The following commands can be used to create a new user, set their password, and create their home folder.

$ sudo useradd lucy -s /bin/rbash
$ sudo passwd lucy
$ sudo mkdir -p /home/lucy/bin

 

We can switch user’s using the su command

$ su - lucy

 

The user can still execute the commands found in the path, this can be prevented by changing the default path of the user’s shell. To do this we have to edit the bash_profile file of the user and secure the restrictions, this is discussed in the following section.

2. Reinforcing The Restrictions

Run the following command to open the ‘bash_profile’ file in edit mode.

$ sudo gedit /home/lucy/.bash_profile

add the following line in the bash_profile file.

PATH=$HOME/bin

 

Next, we’ll alter the owner and file permissions so that only the root user can edit the file.

$ sudo chown root:root /home/lucy/.bash_profile

$ sudo chmod 755 /home/lucy/.bash_profile

$ ls -l /home/lucy/.bash_profile

 

During the next user session, most of the commands will be restricted.

 

We can link user commands from the user’s directory to exclude the restricted commands. Here we’ll link some commands so that the user ‘lucy’ can access these commands from within the rbash environment.

$ sudo ln -s /bin/clear /home/lucy/bin
$ sudo ln -s /bin/neofetch /home/lucy/bin
$ sudo ln -s /bin/ls /home/lucy/bin

Before linking the commands.

 

After linking the commands.

 

3. Restricting pre-existing users

A user called ‘debo’ already exists, now we’re going to try and restrict debo’s shell to rbash. The following command can be used to achieve this.

sudo usermod -s /bin/rbash <username>
sudo usermod -s /bin/rbash debo

 

This command would change the user’s default shell to rbash. Next time this user logs in they use the restricted shell by default.

 

4. Restricting Scripts

By default, every shell script that is executed is run in unrestricted mode. To run a script in a restricted mode, set-r can force the script to use a restricted shell during execution. Let us create a simple shell script.

#FILENAME: gfg.sh

set -r
echo
echo "## In restricted mode! ##"
echo
echo "Current directory: `pwd`"
echo "Changing directory to /home/"
cd /home
echo "Still in directory: `pwd`"

This will force the script to be executed in a restricted environment. Running the above script will yield the following result.

$ . gfg.sh

 

Advantages of Restricted Shell

Disadvantages of Restricted Shell


Article Tags :