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.
Table of Contents
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
The chsh command in Linux stands for “change shell.” As the name suggests, it’s used to change the login shell for a user. The login shell is the shell that gets started when you log into your system, and is defined in the /etc/passwd file for each user.
By default, when a user account is created, it’s assigned a default login shell. On many systems, this is /bin/bash, but other shells like /bin/sh, /bin/tcsh, /bin/csh, and /bin/zsh may be used instead. The chsh command allows you to change this default shell.
Here’s an example of how you can use chsh to change a user’s shell:
chsh -s /bin/tcsh username
In this example, the -s option specifies that the following argument (/bin/tcsh) is the new login shell to assign, and username is the name of the user whose shell you want to change. Note that you would replace /bin/tcsh and username with the path of the shell and the user that you actually want to use.
Also, note that you need to have the appropriate permissions to change a user’s login shell. If you’re not the root user, you can only change the shell for your own user account, and you can only change it to a shell that’s listed in the /etc/shells file, which contains a list of valid login shells.
To simply display the current shell, you can type echo $SHELL in your terminal.
Remember, each shell has its own set of features and syntax, so make sure you’re comfortable with the new shell before you switch to it.
Change user shell with useradd command in Linux
The useradd command is used to add new users to a Linux system. It can be used to specify the shell for a new user. The following syntax is used to change the shell of an new user. The “<username>” is the name of the user you want to create.
useradd -s /bin/zsh <username>
The following example will create a new user with the shell /bin/bash.
# useradd -s /bin/bash testuser
By executing this command, a new user account is created with the specified username, and when that user logs in, the default shell will be set to bash (/bin/bash).
The following is the more examples about useradd command.
The useradd command in Linux is a utility to create new user accounts on the system. Here are some examples of how it can be used:
1. Create a New User: The simplest use case is to create a new user. To create a new user named newuser, you’d use:
2. Create a New User with a Specific Home Directory: If you want to create a user with a specific home directory, you can use the -d option. For example, to create a user named newuser with the home directory /home/customdir, you would use:
useradd -d /home/customdir newuser
3. Create a New User with a Specific User ID (UID): To create a new user with a specific UID, you can use the -u option. For example, to create a user named newuser with a UID of 5000, you would use:
useradd -u 5000 newuser
4. Create a New User and Add to a Group: If you want to create a new user and add them to a group at the same time, you can use the -G option. For example, to create a user named newuser and add them to the users group, you would use:
useradd -G users newuser
5. Create a New User with a Custom Shell: To create a user with a specific login shell, you can use the -s option. For example, to create a user named newuser with /bin/tcsh as the login shell, you would use:
useradd -s /bin/tcsh newuser
6. Create a New User with a Comment: You can add a comment (like the full name of the user) using the -c option. For example, to create a user named newuser with the comment “New User”, you would use:
useradd -c "New User" newuser
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
The usermod command in Linux enables modifying user properties such as the login shell, group membership, account status, expiration date, and home directory.
Here are some examples.
The usermod command in Linux is used to modify or change any attributes of a already created user account via command line. Here are a few examples demonstrating its usage:
1. Change Username: You can use the usermod command to change the username of an existing user. For instance, to change the username from olduser to newuser, you can use:
usermod -l newuser olduser
2. Change Home Directory: If you want to change the home directory of a user, you can do so using the -d (directory) option along with -m (move) to move the contents of the current home directory to the new directory. For example:
usermod -d /new/home/dir -m username
3. Add User to a Group: usermod can also be used to add a user to a group. The -a (append) and -G (groups) options are used for this. To add a user named username to a group named groupname, use:
usermod -a -G groupname username
4. Change User ID (UID): To change the UID of a user, you can use the -u option. For example, to change the UID of username to 5000, use:
usermod -u 5000 username
5. Lock a User Account: If you need to lock a user account temporarily, you can do so using the -L option. For example, to lock the user username, use:
usermod -L username
6. Unlock a User Account: Conversely, you can unlock a user account using the -U option. For example, to unlock the user username, use:
usermod -U username
Remember, you usually need superuser (root) privileges to use the usermod command. Always use it carefully as it can significantly impact user accounts.
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.