Signup/Sign In

Change default shell with chsh command in Linux terminal

On Unix-like operating systems (including Linux distributions), the chsh command is used to switch/change the default login shell. A normal user may only change the login shell for his own account; the superuser may change the login shell for any account.

chsh syntax

chsh [options] [LOGIN]

On the command line, users can either give the pathname of the shell they want to switch to, or they can supply no parameters, in which case chsh enables interactive shell switching. This determines the name of the user's initial login command.

Unless the invoker is the superuser (or with root privileges), in which case any value may be supplied, the only constraint placed on the login shell is that the command name must be mentioned in /etc/shells. Deliberately switching to a restricted shell would prohibit the user from ever changing their login shell back to its default value, hence including /bin/rsh in /etc/shells is advised.

Install chsh command

Debian based - apt install passwd
Alpine - apk add util-linux
Arch Linux - pacman -S util-linux
CentOS - yum install util-linux-ng
Fedora - dnf install util-linux-user
OS X - brew install util-linux
Raspbian - apt-get install passwd
Docker - docker run cmd.cat/chsh chsh

chsh command options

-R, --root CHROOT_DIR Apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory.
-s, --shell SHELL The name of the user's new login shell. Setting this field to blank causes the system to select the default login shell.
--help display this help and exit.

chsh command in ubuntu terminal (help menu)

chsh command Examples:

Default shell configurations used in following examples:

You can always check the shell which you are using right now with the $SHELL environment variable.

$ echo $SHELL
/bin/bash


Check all available shells in your /etc/shells configuration file.

$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/pwsh
/opt/microsoft/powershell/7/pwsh
/usr/bin/tmux


In this tutorial, we are going to change the shell /bin/bash (Bourne-Again SHell) to /usr/bin/pwsh (powershell).

If the -s option is not chosen, chsh runs interactively and presents the user with the current login shell as a prompt. If you want to use the current shell, leave the line blank or provide the new value. A shell's current state is shown in between two [] marks. (password required)

1. Change default shell for current user using chsh command

We can execute chsh command without any argument and enter the password for the current logged in user to change the current user.

$ chsh -s /usr/bin/pwsh
Password:
$

Or

$ chsh
Password:
Changing the login shell for user
Enter the new value, or press ENTER for the default
        Login Shell [/bin/bash]: /usr/bin/pwsh
$

Use chsh command to change default shell in Linux (terminal)

2. Change default shell for other users.

We can use this syntax for changing default shells for changing the default shell for any other user:

$ chsh user [SHELL]

Make sure you have required permissions. You can use TAB completion to find users permitted for the action.

$ sudo chsh
_apt              dnsmasq           use         man               postgres          sync              systemd-timesync
backup            games             list              messagebus        proxy             sys               user
bin               gnats             lp                news              root              systemd-network   uucp
daemon            irc               mail              nobody            sshd              systemd-resolve   www-data


As we already know that we can always check the default login shell for any user in the /etc/passwd file. Now, you can either specify new default shell as argument or use interactive prompt for the same. Also, you can see in the output of the command that we change the shell from /bin/bash to /usr/bin/pwsh.

$ sudo chsh user
Changing the login shell for user
Enter the new value, or press ENTER for the default
        Login Shell [/bin/bash]: /usr/bin/pwsh

3. Change default shell for specified command root directory.

To use the configuration files from the CHROOT_DIR (change root) directory and make changes to the CHROOT_DIR directory. We use the -R CHROOT_DIR or --root CHROOT_DIR option.

To change the shell for LDAP I&A load module defined user “davis”, type:

$ sudo chsh -R / davis
Password:
$

Conclusion

In this article, we learned about uses of the chsh command to change the default shell of a user and the permissions required for the same. We used chsh command interactively and to use chsh command in our bash automation scripts.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.