On Linux a login shell is a shell given to a user upon login into their user account. Today we will look at how to change the default login shell safely for one specific user.
Understanding user shell in Linux
A user’s shell is the program that runs when they login to a Linux system. The default shell for most users is bash, but there are many other options available. To see a list of available shells on your system, run the following command: cat /etc/shells.
we can have the following shells in the system.
[root@host ~]# cat /etc/shells
What is the purpose of a shell in Linux?
A shell provides an interface between you and the underlying operating system. It allows you to execute commands, run programs, and perform other tasks. In some cases, shells may also provide additional features such as advanced text editing and configurable prompts.
There are many different types of shells available on Linux, each with their own strengths and weaknesses. Choosing the right shell can depend on your individual needs and preferences, as well as the type of Linux distribution that you are using.
Change user shell with chsh command in Linux
The best way to change the user’s shell is to use chsh command. Open the terminal and type chsh. Then type the shell file path you want to use. It will issue a warning if the shell is not listed in the /etc/shells file.
If you are root, you can also change shell for other accounts by the following command. Example:
# chsh -s /bin/bash testuser
Change user shell with useradd command in Linux
The useradd command is used to add new users to a Linux system. It can also be used to change the shell of an existing user. The following syntax is used to change the shell of an existing user where “<username>” is the name of the user you want to change their shell.
useradd -s /bin/zsh <username>
The following example will change the shell of user testuser to /bin/bash.
# useradd -s /bin/bash testuser
Change user shell with usermod command in Linux
You can also use the usermod command to change a user’s shell. The following syntax is used to change a user’s shell to zsh:
usermod -s /bin/zsh <username>
Where “<username>” is the name of the user you want to change their shell.
The usermod command modifies the system account files to reflect the changes that are specified on the command line.
# usermod –shell /bin/bash testuser
It is not a safe way to change the user login shell by changing file /etc/passwd directly. We don’t recommend this way.
how to check user shell in Linux
We can use the following two ways to verify the shell for this user.
# echo $SHELL
# cat /etc/passwd | grep testuser
What is the difference between a shell and a terminal?
There are many different types of shells available on Linux, including the Bash, Zsh, and Korn Shell. A shell is essentially a program that provides an interface between you and the underlying operating system. In contrast, a terminal refers to the actual window where you type commands and interact with the shell.
There are a few different ways to change your shell in Linux. One option is to use the chsh command, which allows you to change a user’s shell directly from the command line. Another option is to use the useradd or usermod commands, which allow you to change a user’s shell when creating or modifying their account.
You can also use one of these commands along with the echo or set commands to view or modify your current shell setting. Additionally, some Linux distributions allow you to change your shell from within the system settings or preferences.