Linux chsh Command Tutorial for Beginners (5 Examples)
The bash shell is one of the most widely used login shells in Linux. But there exist other shells as well, and you can use them for your command line work (until of course there's a specific requirement for your work). In this article, we will discuss a tool - dubbed chsh - that lets you switch to a login shell different from your current shell.
But before we do that, it's worth mentioning that all examples in this tutorial have been tested on an Ubuntu 18.04 LTS machine.
Linux chsh command
As already explained in the introduction above, the chsh command lets you change your login shell. Following is its syntax:
chsh [options] [LOGIN]
And here's what the tool's man page says about it:
The chsh command changes the user login shell. This determines the name
of the user's initial login command. A normal user may only change the
login shell for her own account; the superuser may change the login
shell for any account.
Following are some Q&A-styled examples that should give you a good idea on how the chsh command works.
Q1. How to use chsh?
Basic usage is fairly simple - just execute the 'chsh' command sans arguments. You'll be asked for your login password, and after you successfully enter that, you'll have the option to change your login shell.
Note that by default, the login shell for the current user is changed.
Q2. How to change login shell using chsh?
As you'd expect, just enter the new value (see screenshot in previous example) and your shell will be changed to the new one.
For example, I entered /bin/sh as the new value.
To verify the change, you can run the chsh command again. The value shown in square brackets is the current shell. So in my case, the current shell became /bin/sh:
Q3. How to change login shell for some other user?
For this, just specify the username as input to the chsh command.
For example, to change login shell of root, use chsh in the following way:
Q4. How to make chsh run in non-interactive mode?
Use the -s command line option for this, which requires shell name to passed as input to it.
chsh -s /bin/sh
So this way, the login shell for current user will be changed to /bin/sh in a non interactive manner.
Q5. How to see a list of available login shells on system?
To quickly take a look at the list of shells available in your system, just cat the /etc/shells file.
For example, here is the output produced on my system:
# /etc/shells: valid login shells
The only restriction placed on the login shell is that the command name
must be listed in /etc/shells, unless the invoker is the superuser, and
then any value may be added. An account with a restricted login shell
may not change her login shell. For this reason, placing /bin/rsh in
/etc/shells is discouraged since accidentally changing to a restricted
shell would prevent the user from ever changing her login shell back to
its original value.
So you see, the chsh command is an important tool that command line users should be aware of. Here, in this tutorial, we have discussed the majority of the command line option the tool offers. To learn more about chsh, head to its man page.