Skip to content

Instantly share code, notes, and snippets.

@adojos
Last active May 22, 2024 06:21
Show Gist options
  • Save adojos/99fd4b104a55e77cbbad067402da3222 to your computer and use it in GitHub Desktop.
Save adojos/99fd4b104a55e77cbbad067402da3222 to your computer and use it in GitHub Desktop.
Linux: Changing Default Linux Shell #linux

Changing Default Linux Shell

Use one of the below listed methods to change your default shell.

👉 Note:

You should already have more then one shell installed on your system in order to switch to another shell. Use the below commands to check the installed shells and current default shell on your system:

1. List all Installed Shells : cat /etc/shells
2. Verify User’s Current Shell : echo $SHELL
3. You can also check contents of '/etc/passwd' file to see default shell for user : cat /etc/passwd

Method A : Using ‘chsh’ command (util-linux pkg)

The chsh command allows you to change your default shell. It allows you to change both the default login and default interactive shells. So be advised that using this command you would be changing both types of shell sessions.

You’re prompted for your password and returned to the command prompt of your current shell. You need to log out and back in for the change to take effect. You can use either -s or --shell option with the 'chsh' command, both works!


A.1 : Changing logged-on User's Shell

Here are the examples of 'chsh' with some variations:

1. chsh -s $(which zsh) <your-username>
   This would change the default shell to 'ZSH'. The (which zsh) would auto populate the path to your zsh location.
2. chsh --shell /bin/bash <your-username>
   This would change the default shell to 'bash'. Here we are providing the path to 'bash' manually.

A.2 : Change Other User's Shell Using ’chsh’ (Root Privileges)

If you have root privileges and can use sudo, you can change the shells of other user accounts. The command is the same as before, with the addition of that person’s username added to the command line:

sudo chsh -s /usr/bin/fish <username>

With this method you would change other user’s default shell one at a time hence if you are to change shell for a large number of users it may take some time unless you knock-up some automated script or way.



Method B : Using ‘usermod’ command

The ‘usermod’ is a command that modifies the system account files. The 'usermod' can change the shell of users by modifying file /etc/passwd. Hence you can also use ‘usermod’ utility for modifying a user’s account details stored in the /etc/passwd file. Once again you can use either -s or --shell option to change the user’s login shell.

Here two examples :

usermod -s $(which zsh) <your-username>
This would change the default shell to 'ZSH'. The (which zsh) would auto populate the path to your zsh location.
usermod --shell /bin/bash <your-username>
This would change the default shell to 'bash'. Here we are providing the path to 'bash' manually.


Method C : Change User Shell in '/etc/passwd' Manually (Root Privileges)

Another way to change user’s shell is by simply editing the /etc/passwd file using a suitable linux editor. The /etc/passwd file is a plain text file which stores user account information. A record contains the user’s account data, such as username, UID, GID, and home directory, among others. Generally the last field indicates the default shell of the user.

If we change the default shell field in the /etc/passwd file, it’ll take effect when the user logs on to the system next time.

👉 Note:

  1. One advantage of editing the /etc/passwd file is that we can change the default shell of multiple users in one shot. Sometimes, this could be pretty convenient.
  2. However, we should keep in mind that when we save the /etc/passwd file, no validation or verification will happen on the changed default shells.


Method D : Change User Shell via Config Files (Dot Files)


Method D.1: Put the following in your .bashrc file -

if [ -t 1 ]; then
exec zsh
fi

Method D.2 (Without Root Access): Put the following in your .bash_profile file -

export SHELL=/bin/zsh
exec /bin/zsh -l

Method D.3: Put the following in your .bash_profile file -

usermod -s $(which zsh) <your-login-username>
OR
chsh -s $(which zsh)


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment