1. Overview
An interactive shell reads commands from user input on a tty terminal. We call the interactive shell after a user’s successful login the default shell or login shell.
In this tutorial, we’re going to discuss how to change the default shell of a user.
2. Introduction to the chsh Command
The chsh command is shipped with the util-linux package — a standard package that’s available on all Linux distros.
Using the chsh command with the -l option, we can list the shells installed in the current system:
$ chsh -l
/bin/sh
/bin/bash
/bin/zsh
/usr/bin/zsh
/usr/bin/git-shell
A more common usage of the chsh command is to change the default shell of a user.
We’ll address how to do that in later sections.
3. Which Shell Am I Currently Using?
After a successful login, we’ll be with our default shell.
Before we talk about changing the default shell, let’s first learn a couple of ways to find out which shell we’re currently using.
One way is to read the system environment $SHELL, which stores the currently running shell command:
kent$ echo $SHELL
/bin/zsh
As the example above shows, the running shell of my current login user is Zsh.
Alternatively, we can know the current running shell by exploring its process information. Linux stores the PID of the running shell in the special variable $$:
kent$ echo $$
153492
In the example above, the process with PID 153492 would be the current shell process. We can get detailed information about the process using the ps command with the -p option:
kent$ ps -p $$
PID TTY TIME CMD
153492 pts/2 00:00:00 zsh
The output tells us that Zsh is our current shell.
4. Changing the Default Shell of the Current User
If we want to change the default shell of the current login user, we can execute the chsh command with the -s <NEW_SHELL> option.
Let’s change the default shell of the current user to Bash:
kent$ chsh -s /bin/bash
Changing shell for kent.
Password:
Shell changed.
It’s worthwhile to mention that after we successfully changed the default shell, the currently running shell will not be changed. For example, we’ve set Bash as the default shell of the user kent, but the current shell is still Zsh:
kent$ ps -p $$
PID TTY TIME CMD
153492 pts/2 00:00:00 zsh
The newly changed default shell will take effect the next time we log in with the user kent.
The default shell command is essential for users’ login. If the command is invalid, we’ll have trouble logging in with the user.
Therefore, as we’re changing the default shell, the chsh command will verify if the given shell command is valid.
Let’s see what will happen if we pass an invalid path to the chsh command:
kent$ chsh -s /bin/bashhhh
Changing shell for kent.
Password:
chsh: "/bin/bashhhh" does not exist
We’ve learned the chsh command can list the currently installed shells in the system.
The chsh command will refuse to change the default shell if the given shell isn’t on the system’s list of valid shells:
kent$ chsh -s /usr/bin/cat
Changing shell for kent.
Password:
chsh: "/usr/bin/cat" is not listed in /etc/shells.
Use chsh -l to see list.
In the example above, we attempted to set the /usr/bin/cat command as the default shell of the user kent.
The cat command path is valid. However, it isn’t on the shell list. Therefore, chsh aborted with the corresponding error message and hint.
5. Changing the Default Shell of Other Users
So far, we’ve learned how to use the chsh command to change the default shell of the current login user.
We can also change the default shell of other users using the chsh command, but we need the root permission to do that.
The syntax is pretty straightforward:
root# chsh -s NEW_SHELL USERNAME
This time, we log in as the root user and change the default shell of the user kent to /bin/sh:
root# chsh -s /bin/sh kent
Changing shell for kent.
Shell changed.
Now, let’s log in again with the user kent and check if the default shell was changed successfully:
sh-5.1$ echo $SHELL
/bin/sh
As the output above shows, the default shell of the user kent is now /bin/sh.
6. Editing the /etc/passwd File
We’ve learned to use the chsh command to change a user’s default shell. Alternatively, we can also do that by editing the /etc/passwd file.
In Linux, the /etc/passwd file is a plain text file. It stores user account information, which is required during user login:
-rw-r--r-- 1 root root 2.1K Mar 12 10:27 /etc/passwd
If we have a look at its permission flags, we’ll see all users can read it, but only the root user is allowed to write to /etc/passwd.
Let’s check the content in the /etc/passwd file:
kent$ cat /etc/passwd
root:x:0:0:root:/root:/bin/zsh
bin:x:1:1:bin:/bin:/bin/false
...
kent:x:1000:1000::/home/kent:/bin/zsh
guest:x:1001:1001::/home/guest:/bin/zsh
...
cups:x:209:209:cups helper user:/:/sbin/nologin
...
Every line in the file records the account information of a user. The fields in a record are separated by colons.
A record contains the user’s account data, such as username, UID, GID, and home directory, among others. The last field indicates the default shell of the user.
If we change the default shell field in the /etc/passwd file, it’ll take effect when the user next logs in to the system next time.
One advantage of editing the /etc/passwd file is that we can change the default shell of multiple users in one shot. Sometimes, this could be pretty convenient.
However, we should keep in mind that when we save the /etc/passwd file, no validation or verification will happen on the changed default shells.
Therefore, we must make sure we’ve given valid shell command paths. Otherwise, the user will have a problem logging in to the system next time.
7. Conclusion
In this quick article, we’ve learned how to change a user’s default shell through examples.
The chsh command is pretty straightforward for the task. Moreover, it can verify if the given shell is valid in the system.
If we want to change the default shell of a user other than the current login one, we need to execute the chsh command with root user permission.
Further, if we can log in as the root user, we can also change the default shell of multiple users by directly editing the /etc/passwd file.
But we must double-check the changes we made to the /etc/passwd file. Any mistakes made in the /etc/passwd file may lead to login failures for the corresponding users.