Changing the Default Shell in Linux


Changing the default shell in Linux is simple, it gives you the freedom to use any shell accordingly. The default shell of most Linux systems is 'bash,' which you replace from any other shell such as sh, fish, dash, zsh, etc. There can be many reasons in Linux why you need to change your default shell; some of the main reasons are as follows −

  • To disable or block normal user logins using a nologin shell.

  • Change the default shell on a shared network to meet the user's specific demands. It is associated with many administrative rights.

  • A shell wrapper program/script delays the execution of user commands until the user logs them. In this case, for the shell wrapper, the user login is shell specific.

There are several ways in Linux to change the user's shell. In this guide, we will look at all the available approaches for changing the default shell of the Linux system.

How to Change the Default Shell in Linux

Knowing which Linux shell is installed in your system must first change the default shell. In Linux, the 'etc/shells' file contains all the information about the available shells, so you can list all the shell details using the below cat command −

~$: cat /etc/shells
# /etc/shells: valid login shells 
/bin/sh 
/bin/bash 
/usr/bin/bash 
/bin/rbash 
/usr/bin/rbash 
/usr/bin/sh 
/bin/dash 
/usr/bin/dash 

All the shells included in the above list are available in the Linux system, and you can replace any of them with your default shell. Furthermore, if you want to change any other shell, not on this list, you must install it separately.

First, you must find the current shell to change the user shell first. In Linux, the /etc/passwd file stores the essential information of user accounts. This information is required during login, and you can also get the shell-related information from it. You can execute any of the following commands to find out your current shell −

~$: grep "^${USER}" /etc/passwd
prateek:x:1000:Prateek Jangid,,,:/home/prateek:/bin/bash

Or,

~$: grep `whoami` /etc/passwd
prateek:x:1000:Prateek Jangid,,,:/home/prateek:/bin/bash

Or,

~$: echo $SHELL
/bin/bash

Or,

~$: grep "^${USER}" /etc/passwd
 PID  TTY     TIME CMD
2357 pts/0  00:00:00 bash

As you can see in the above outputs, the current shell is 'bash,' so we will change the 'bash' shell to a 'sh' shell using various methods.

The chsh Command

This command is useful when you don't have root privileges but want to change the default shell. You usually do not need a root account for the chsh utility.

This command changes the user's shell by modifying the /etc/passwd file. You can use the -s option to change the shell −

~$: chsh -s <New_Shell>

For example, let's change the bash to sh through the following command −

~$: chsh -s /bin/sh

The Usermod Command

The usermod command can change the system account files. You can modify the /etc/passwd file using this command line and change the user's shell. With the usermod command, you can change the user's login shell using the --shell or -s option.

~$: sudo usermod --shell <New_Shell> 

Let's now change replace bash from sh as the default shell through the usermod command −

~$: sudo usermod --shell /bin/sh

Note − We changed the default shell for the current user in the above methods. However, you can also change the shell for other users. So here are the commands you can use in which you need to enter the username of other users −

~$: chsh -s <New_Shell> <Username>
~$: sudo usermod --shell <New_Shell> <Username>

From /etc/passwd File

The system automatically modifies the /etc/passwd file through the above commands to change the default shell. In case you don't want to use these commands and edit the /etc/passwd file manually, then you can do it by executing the following command −

~$: nano /etc/passwd

Once you execute the above command, the terminal will open the /etc/passwd, so now make changes accordingly.

GNU nano 6.2                      /etc/passwd * 
root:x:0:0:root:/root /bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync 
games:x:5:60: games:/usr/games:/usr/sbin/nologin

As you can see, the current shell is /bin/bash, and you can replace it with any shell (sh, zsh, dash). Here, we will change the /root /bin/bash to /root /bin/bash.

GNU nano 6.2                      /etc/passwd * 
root:x:0:0:root:/root /bin/sh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync 
games:x:5:60: games:/usr/games:/usr/sbin/nologin

After changing the shell manually, save the file and exit. Manually editing the /etc/passwd file allows you to change the default shell for multiple users simultaneously, which is also very convenient.

Note that saving this file does not provide any verification on the changed default shell. So make sure your shell command path is valid. Otherwise, you may face some issues whenever you log in to the system next time.

Conclusion

In this guide, we have explained how to change the default shell with the help of three methods. Here we have used usermod and chsh utility to change the default shell for the current user directly. In addition, you must define the username separately if you wish to change the shell for other users.

You also change the default shell manually for multi-users at once by editing the etc/passwd file. Therefore, while using this method, you must take special care that you still need to enter your correct shell. Thus you can change your default shell in Linux using any of the above methods.

Updated on: 18-May-2023

908 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements