How to Change Default User Shell in Linux

0
14


The shell, or command-line interface, is a crucial component of the Linux operating system, providing users with a powerful way to interact with the system.

Each user on a Linux system is associated with a default shell, which determines the command interpreter they use when interacting with the system.

Changing a user’s default shell can be necessary for various reasons, such as personal preference or the need for specific features offered by a different shell.

In this article, we will explore the process of changing a user’s default shell in Linux. The shell is a program that accepts and interprets commands; there are several open-source shells such as bash, sh, ksh, zsh, fish, and many other lesser-known shells available on Linux.

Linux Bash Shell

Bash (/bin/bash) is a popular shell on most if not all Linux distributions, and it’s normally the default shell for user accounts.

There are several reasons for changing a user’s shell in Linux including the following:

  • To block or disable normal user logins in Linux using a nologin shell.
  • Use a shell wrapper script or program to log user commands before they are sent to a shell for execution. Here, you specify the shell wrapper as a user’s login shell.
  • To meet a user’s demands (wants to use a specific shell), especially those with administrative rights.

When creating user accounts with the useradd or adduser utilities, the --shell flag can be used to specify the name of a user’s login shell other than that specified in the respective configuration files.

A login shell can be accessed from a text-based interface or via an SSH from a remote Linux machine. However, if you log in via a graphical user interface (GUI), you can access the shell from terminal emulators like xterm, konsole, and many more.

List Available Shells on a Linux System

In Linux, the file /etc/shells is commonly used to store a list of installed valid login shells on the system. This file helps in identifying which shells are available for users to set as their default shell using commands such as chsh.

To view the list of available shells on a Linux system, you can use the following command:

cat /etc/shells

Sample Output:

# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/usr/bin/screen

Before you proceed any further, note that:

  • A user can change their own shell to anything: which, however, must be listed in the /etc/shells file.
  • Only root can run a shell not listed in the /etc/shells file.
  • If an account has a restricted login shell, then only the root can change that user’s shell.

Find the Current Shell in Linux

Before changing a default user’s shell, it is important to determine the current shell by running the following echo command, which displays the current shell for the logged-in user.

echo $SHELL

/bin/bash

The output shows the user is currently using the Bash (/bin/bash) shell.

Changing the Default Shell in Linux

Now let’s discuss three different ways to change the default Linux user shell.

1. usermod Utility

The usermod command is used for modifying a user’s account details, stored in the /etc/passwd file and the -s or --shell option is used to change the user’s login shell.

In this example, we’ll first check user tecmint’s account information to view his default login shell and then change its login shell from /bin/sh to /bin/bash as follows.

grep tecmint /etc/passwd
sudo usermod --shell /bin/bash tecmint
grep tecmint /etc/passwd
Change User Shell using Usermod
Change User Shell using Usermod

Now the default shell for the user “tecmint” will be changed to bash.

2. chsh Utility

The chsh command is used to change the user’s default login shell interactively by using the -s or –shell option as shown.

grep tecmint /etc/passwd
sudo chsh --shell /bin/sh tecmint
grep tecmint /etc/passwd
Change User Shell Using chsh
Change User Shell Using chsh

Replace “/bin/sh” with the actual path to the shell you want to set. For example, to set the shell to sh, you would use “/bin/sh“.

It’s important to note that changes to the default shell usually take effect upon your next login.

The two methods above all modify the shell specified in /etc/passwd file which you can edit manually as in the third method below.

3. Change User Shell in /etc/passwd File

In this method, simply open the /etc/passwd file using any of your favorite command line text editors and change a specific user’s shell.

sudo vi /etc/passwd
OR
sudo nano /etc/passwd
Change User Shell in Passwd File
Change User Shell in Passwd File

Locate the line corresponding to the user for whom you want to change the default shell. For example, if the line looks like this:

tecmint:x:1000:1000:John Doe:/home/tecmint:/bin/bash

The last field, /bin/bash, represents the default shell (in this case, Bash).

To change the value of the shell field to the desired shell, simply specify the path to a shell (/bin/zsh) as shown.

tecmint:x:1000:1000:John Doe:/home/tecmint:/bin/zsh

When you are done editing, save and close the file.

How to Install Another Shell?

Similar to other software packages, popular shells such as Zsh, and Fish can be easily installed from your distribution’s repository as shown.

Install Zsh Shell in Linux

To install Zsh on Linux, use the following appropriate command for your specific Linux distribution.

sudo apt install zsh         [On Debian, Ubuntu and Mint]
sudo yum install zsh         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/zsh  [On Gentoo Linux]
sudo apk add zsh             [On Alpine Linux]
sudo pacman -S zsh           [On Arch Linux]
sudo zypper install zsh      [On OpenSUSE]    

Install Fish Shell in Linux

To install Zsh on Linux, use the following appropriate command for your specific Linux distribution.

sudo apt install fish         [On Debian, Ubuntu and Mint]
sudo yum install fish         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/fish  [On Gentoo Linux]
sudo apk add fish             [On Alpine Linux]
sudo pacman -S fish           [On Arch Linux]
sudo zypper install fish      [On OpenSUSE]    

Do not forget to read these related topics:

In this article, we described various ways of changing a user’s shell in Linux. To share any thoughts with us, use the comment section below.



Source link