chsh Command in Linux



In Linux distributions, Bash is the most popularly used default login shell. However, users have various alternatives to choose from, such as Zsh, Tcsh, etc., each offering different features and functionalities for command-line operations. You can switch from the current shell to any other alternative shell using the chsh command. A regular user can only switch the login shell for their own account, while the superuser can change the login shell for any user.

Table of Contents

Here is a comprehensive guide to the options available with the chsh command −

Prerequisites

  • A Linux system (we’re using Ubuntu 24.04 for demonstration)
  • Terminal access
  • Sudo permissions for specific examples

chsh Command in Linux | Basic Usage

chsh is a handy command in Linux that stands for change shell. This command lets a user change the current login shell.

In Linux distros, the "/etc/shells" file contains the valid login shells on the system. Therefore, if you try to change the current shell to one that is not listed in the "/etc/shells" file, then the "chsh" command will prompt a warning. As a result, the change will not be applied.

chsh Command Basic Syntax

The chsh command can be executed with or without any options. To use this command in Linux, simply execute the below-mentioned syntax −

chsh [Option] [Shell] [user_name]

Here, chsh is a command that may accept options like -s, -l, etc., to perform different functionalities. The user_name represents a user for whom you want to change the login shell. If you skip the user_name option, the chsh command will change the shell for the current user.

chsh Command Options

The chsh command supports various options to customize the output. The commonly used options are listed in the following table −

Option Description
-h, --help It shows the chsh command's basic syntax and the information of various valid options.
-s, --shell SHELL It is used to change the current login shell of a user.
-R, --root CHROOT_DIR It is used to change the user’s login shell in a different root directory environment.
-l (may not work on some Linux distros) It is used to specify the login shell of a user.
-v (may not work on some Linux distros) It shows the version details of the chsh command.
–u (may not work on some Linux distros) It is used to get the list of all valid shells.

Manual Page chsh

You can explore the manual page of the chsh for a profound understanding of this command −

man chsh
Manual Page chsh

How to Get the List of All Shells?

The "/etc/shells" file keeps all the necessary information regarding the valid shells. You can access this file using the cat, more, or less command to get the list of all available shells −

cat /etc/shells
#or
more /etc/shells
#or
less /etc/shells
Get the List of All Shells

How to Get the Current Shell?

To get the current shell, type echo followed by "$SHELL", and then press the "ENTER" key −

echo $SHELL
Get the Current Shell 1

Alternatively, you can execute the chsh command without any argument to get the current shell −

chsh
Get the Current Shell 2

How to Change the Current Login Shell?

There are two ways to switch the current login shell as listed below −

You can use the chsh command with the "-s" option as follows −

chsh -s /usr/bin/bash

On successful execution of the command, the cursor will move to the next line without showing any output −

Change the Current Login Shell 1

You can verify the switched shell by running the chsh command without any argument −

chsh

The output confirms that the login shell has been changed from "/bin/bash" to "/usr/bin/bash" −

Change the Current Login Shell 2

Alternatively, you can change the user’s login shell by simply executing the chsh command without any argument, as shown below −

chsh

After providing the password, specify the shell to which you want to switch −

Change the Current Login Shell 3

Let’s verify the switched shell by using the following command −

chsh
Change the Current Login Shell 4

The output shows that the login shell has been successfully switched to "/bin/bash".

How to Change the Login Shell of Another User?

The chsh command enables users with superuser privileges (typically root or users with sudo access) to change the login shell of another user. For this purpose, you must specify the username whose shell you want to change.

For example, in the following command, we are changing the login shell of the user named tutorialspoint −

sudo chsh tutorialspoint

We specify the "/usr/bin/dash" login shell for the user "tutorialspoint" −

Change the Login Shell of Another User 1

Now switch to the desired user, i.e., tutorialspoint using the following command −

su tutorialspoint

After switching to the "tutorialspoint" user account, let’s execute the below-mentioned command to check if the login shell for the desired user has been changed or not −

chsh
Change the Login Shell of Another User 2

The output confirms that the login shell for the user "tutorialspoint" has been changed to "/usr/bin/dash".

How to Apply the Changes in Different Root Directory Environments?

You can use the chsh command with the "-R" option to change the login shell for a user in a different root directory environment. For this purpose, you can use the following syntax −

sudo chsh -R /path_to_root user_name

How to use the -h, --help Option with chsh Command?

The chsh command supports the "-h" or "--help" option, which provides users with detailed information and instructions on how to effectively use this command in Linux. Here is an example −

chsh -h

This command retrieves the help page of the chsh command, which includes the basic syntax, valid options, and their usage −

Use -h --help Option with chsh Command

That’s all about the basic usage of the chsh command in Linux.

Conclusion

The chsh or change shell is a handy command in Linux that helps manage user login shells. This command-line utility enables users to choose from various alternatives like Bash, Zsh, etc.

Normal users can use this command to change their own login shells, while superusers can use it to change shells for any user. The command supports various options like -s, -R, -h, etc., for performing different functionalities.

Advertisements