chsh command on Linux 1

How to Use chsh Command on Linux

Changing the user’s login shell is what the chsh command does in the Linux ecosystem, but how do you use it? Here’s everything you need to know about the chsh command and how it can be implemented. 

The chsh command stands for change shell, which is used to change the user’s login shell. This command is primarily used to change the default shell in Linux and Unix-based OS.

CLI-based shells require different commands to operate, which must be provided by the users. These commands have strict syntax, which might be complex for beginner users. 

But GUI-based shells or programs, in general, are beginner-friendly. They provide different UI, which can be seen as buttons or others, to hide the complexity of the shell. Some of the shells are:

  • bash: It is the default shell in most of the Linux distros.
  • rbash: It is a restricted bash. Provides minimal functionality.
  • ash: It is the lighter version of bash.
  • zsh: It is an improvement to bash by providing different suggestions and completion to the desired command.
  • fish: It is the most user-friendly and interactive shell. This shell is entirely written from scratch, its sole purpose is to be user-friendly.

The chsh command is one of the setuid programs (command in this case), which only allows users to change their login shells. However, if you are a superuser, you can also modify the shells of other users. This command requires the pathname of the shell to be installed, which is in the directory /etc/shells only. 

Based on the system you are using, chsh command may or may not ask for a password to change your current shell (for the current user). But, some systems require passwords for all the functions of chsh command for security reasons.

Prerequisites

  • A Linux OS system
  • Sudo privilege of your OS
  • Choosing a shell of your choice

Using chsh Command in Linux

Helper command

You can use the -h or --help flag to display the help message and exit. This is helpful to list all the available commands along with their usage.

Input:

$ chsh -h

Sample output:

  • -h, --help: display this message and exit
  • -R, --root {CHROOT_DIR}: directory to chroot into
  • -s, --shell {SHELL}: new login shell for the user account

Printing List of Available Shell

Use the following command to get the list of all shell options you currently have.
Input:

$ cat etc/shells

Here, -l is supplied as a flag to execute a specific command. We will use different flags throughout this tutorial.

Once you run the command, you may get a similar result as seen below on your computer, depending upon the shell you have installed.

chsh command linux

Don’t worry if your results are different. As long as you get at least one, you are good to go.

In our case, it looks like this:

bash

Installing The Shell of Your Choice

You can always install one if your OS doesn’t have the shell you wanted. The fish shell is one of the fun shells so we will be using it for now. You can use the following command to install fish.

Input:

$ sudo apt-get install fish

If you got the error such as unable to locate the fish or similar, you will have to install the fish repository first like this:

$ sudo apt-add-repository ppa:fish-shell/release-3

Now run the following command: 

$ sudo apt-get install fish

Once the command is completed, you can check its installation state by listing all the shells available, just like we’ve done recently using the command:

$ cat /etc/shells

And you should see fish in the directory /user/bin/fish. Up to this point, you can use whatever shell you have installed. But you have to remember its path as we set that shell as default.

Getting Environment Variable Path of The Installed Shell

As we explained to you earlier, you may need the bin path of the shell. You can copy from above, or you can follow this step. So, the $ echo $SHELL command prints the path of the current shell.

To get the path of fish, you must open fish first with the following command:

$ fish

With this, you will enter the fish shell, so you can write the $SHELL command now as you can see below:

chsh command linux

This directory is relative, so you have to /fish.

Changing the Shell

The chsh command allows you to change your default shell (login shell and interactive shells). The login shell is used when you log in to your server or computer remotely, and the interactive shell is used to replace your current shell.

To change the login, you can use chsh without parameters like this:

$ chsh

Then you are asked for your password. Now, you have to type the path to the new shell you want. In our case, it is /user/bin/fish. Hit enter and you are done. 

chsh command on Linux

If you want to test this out, you can remotely connect to this computer and observe the shell.

For now, we must focus on changing our interactive shell from default bash, to fish. For this, we use the -s flag with chsh command like this:

Syntax:

$ chsh -s [path_to_shell]

Example:

$ chsh -s /usr/bin/fish

It’s obvious that the command asks for the password. Provide that and hit enter. To see the change in effect, you should close the shell and log in again. 

Now, you can observe the greeting from the fish shell.

chsh command on Linux

Changing Shell for Other Users

For this, you will be asked sudo privileges. With this command, you can change the shell to other users on your PC. The command is just like the above, but you must provide the user name of the target user like this:

$ sudo chsh -s [path_of_shell] [target_username]

Example:

$ sudo chsh -s /usr/bin/fish john_doe

And just like this, we have covered the chsh command. If you have any questions on how to use the chsh command on Linux OS, don’t forget to leave a comment down below.

If this guide helped you, please share it.

Leave a Reply
Related Posts