How to Change Default Shell on Linux

The default shell on most Linux systems is bash. Linux is open source, which means various people have contributed their work resulting in a plethora of distros. The same thing happened with its shell, since many people have also put in their work with different purposes in mind. In this article we will discuss how to change the default shell on Linux.

Shell on Linux: Basics

A system shell enables interaction between an operating system and a user. It is basically an application running on top of an operating system. Linux shell is a text-based command line interface that recognizes and interprets built-in commands for executing system utilities. 

It is a simple programming language for executing scripts, which is also a list of commands in a single file that may include conditions, structures, and loops to automate tasks. There are also many shells available for use on Linux. Some of the common ones are C Shell (csh), Korn Shell (ksh), Bourne Shell (sh), and of course, the GNU Bourne-Again Shell (bash). 

C Shell or csh is also an alternative to bash, it was made mainly for people who are more used to the C syntax. Korn Shell or ksh on the other hand, was created for arithmetic floating point, which is not available in bash. And lastly, Bourne Shell or sh is the original shell for UNIX—an operating system that inspired Linus to create Linux.

Changing the Default Shell on Linux

There are also many different ways to change the default shell for any user on a Linux system. And we will be looking at three of them in detail here.

Verify the Current Shell

The first thing we want to do before we change the default shell on your Linux system is to verify the current shell in use. There are several ways to figure out which shell is currently used. 

After logging in successfully, the default shell for the user account being used will also be loaded. By running the command below, you can also read the system environment shell.

Syntax:

$ echo $SHELL
change default shell on linux

In this screenshot, you can see that the current running shell is bash. Also, you have to  take note that ‘$SHELL’ must be in all caps.

Another way to find out the currently running shell is by exploring the user account’s process information. The PID of the running shell is also stored by Linux in a special variable, which is $$. For our example, the PID of the current shell is 2281. To get the details about the process, we can also use the commands below.

Syntax:

$ echo $$
$ ps - p $$
change default shell on linux

Here you can see the details about the process, and we have also confirmed that bash is the currently running shell.

Verify Which Shells are Currently Installed on Linux

The second thing we need to do is also verify which shells are currently installed on your Linux system. In order to get the list of shells available in your system, run the command below.

Syntax:

$ cat /etc/shells
change default shell on linux

This is what you will see after running the command above. For our example, we have confirmed that there are four shells available.

Changing the Default Shell of the Currently Logged-In User on Linux

Now that we have identified the currently running shell, and which shells are installed. Let’s go ahead and try to change the default shell for the currently logged-in user account. Use the command below to execute this task.

Syntax:

$ chsh -s /bin/dash

or

$ usermod --shell /bin/dash username
change default shell on linux

As you can see in this screenshot, running the command will prompt you for your password. This command will not show you any indication that it has been executed successfully.

Now try running the ‘echo $SHELL’ command again to verify the current shell in use. That’s right! The currently running shell did not change at all. The command we just ran will only take effect the next time you log in. So, go ahead and log out, log back in,  and check again.

shell command

This is what you will see after logging back in. You can notice that the command prompt looks different as well.

Now try running the commands ‘echo $$’ then ‘ps -p $$’ again.

echo

We have now confirmed that the default shell for this user has been changed to dash.

Changing the Default Shell of Other Users on Linux

Before we try changing the default Shell for other users, let’s first check a particular file. The /etc/passwd file is what we are interested in. This file shows the users on a Linux system and their default shell. We will need this information to complete our next task.

To print out the /etc/passwd file, we need to run the command below.

Syntax:

$ cat /etc/passwd
change default shell on linux

As you can see, there is a lot of information in the /etc/passwd file. What we are looking for are the usernames and their current default shell. In the fourth line from the bottom, you can see the username dregs and that the current default Shell is bash.

Let us now try to change the user’s dregs default shell by running the command below.

Syntax:

$ chsh -s /bin/dash dregs

or

$ usermod --shell /bin/dash dregs
ubuntu root

To change the default Shell of other users, you need to be a sudo privileged user. The root user, of course, also has the permission for this. As you can see in the screenshot above, the system is not allowing us to execute the command since we are logged in as a guest user.

Creating a User with Specific Login Shell

Ok, so let’s go back to our sudo privileged user account. First, let’s create a new user account we can play with. Run the command below to create a new user with a specific shell as its default. For our example, let’s set the default shell to ‘rbash’ and the username to ‘Nadz’.

Syntax:

$ sudo useradd -s /usr/bin/rbash Nadz
rbash

Upon running the command, you will be prompted for your password. The command will execute without any notification that it has succeeded. Run the command below to check the /etc/passwd file and see if our user has been created.

Syntax:

$ cat /etc/passwd
password

As you can see at the very bottom, we now have the user Nadz, and the default shell has been set to rbash.

Let’s go ahead and change the default shell to dash. Lets run the command below to do it.

Syntax:

$ sudo chsh -s /bin/dash Nadz

or

$ usermod --shell /bin/dash Nadz
change default shell on linux

In this screenshot, you can see that the command will not go through without the sudo at the beginning. Once the command does execute, you will not get any indication that it was successful. Run the “cat /etc/passwd” command again to verify that the default shell for Nadz has been set to dash.

default shell

As you can see here, the default shell for Nadz has now been changed to dash.

Changing the Default Shell of Multiple Users on Linux

As we have mentioned at the beginning of this article, we will be looking at three methods on how to change the default shell on Linux

To change the default shell of multiple users in one go, all we need to do is to edit the /etc/passwd file using a utility like vi or vim and then save the changes made. You will need to run vi or vim as root, or you must have sudo privilege to be able to save changes to this file.

And that’s about it for this tutorial. We looked at how to change the default shell for the currently logged-in user, as well as changing the default shell for other users, and for multiple users in one go. We’ve even learned how to get a list of currently installed shells and the user lists along with their default shells.

If this guide helped you, please share it. ?

Leave a Reply
Related Posts