How to Change the Default Shell in Linux

Spread the love

A shell is the interface through which you can communicate with your Linux system. It allows you to type commands and control your computer. Every Linux user has a default shell, which is bash in most cases. There are also many other options available, including Zsh, ksh, rc, and fish. Here we show you the various methods to change your default shell in Linux.

Content

How to Find Your Current Shell Name

Before we dive into changing the default shell, it’s good to know which shell you’re currently using. You can find this out by checking the system environment variable using the following echo command:

echo $SHELL

This command will print out the path of your current shell, such as /bin/bash or /bin/zsh.

The /etc/passwd file contains important user account information. You can also use this file to find your current shell:

grep "^${USER}" /etc/passwd

You can also find more information about the current shell by checking the process ID (PID), using the ps command:

ps -p $$

This output confirms that the current shell is bash.

List All the Installed Shells

Now that you know your current shell, you might want to see what other options are available on your system. To locate all the installed shells on your system, you need to take a look at the /etc/shells file.

Let’s use the cat command to view a list of all the shells available on your system:

cat /etc/shells

This will give you an idea of what shells you can switch to. You can also install any new shell using your package manager and use it as your default shell.

Furthermore, you can try out these listed shells temporarily without changing your default shell. To start a new shell session, simply type the shell’s name in the terminal.

For example, to try Zsh shell, type:

zsh

This lets you experiment with different shells before deciding to make a permanent switch.

Change Default Shell in Linux Using Chsh

The most straightforward way to change the default shell in Linux is to use the chsh command. The chsh command stands for “change shell,” and it’s used along with the -s option followed by your shell path.

If you don’t know your shell path, then run type zsh in the terminal to get the desired path. After getting your shell path, you can change your default shell to your specific shell.

For example, if you want to switch to Zsh, you need to use the chsh -s with the Zsh shell path:

chsh -s /bin/zsh

You’ll be prompted to enter your password. Once you do that, you need to log out and log back in to confirm the changes.

The chsh command will change the default login shell permanently. You can verify the changes by running this command:

grep "^${USER}" /etc/passwd

Changing Another User Shell

If you have root privileges and the ability to use the sudo command, you can change the default shell for other user accounts. This can be useful if you want to provide a different shell environment, such as fish, for another user.

You can use the same chsh command syntax with an additional parameter specifying the username of the account you wish to modify.

For example, to change another user’s shell to Zsh, execute this command in the terminal:

sudo chsh -s /bin/zsh user

This command tells the system to change the default shell for the specific user to the Zsh shell, which is located at /bin/zsh.

Change Default Shell in Linux Using Usermod

Another method to switch shells is by using the usermod command. This command is used to modify user accounts, and it can also be used to change the default shell.

To change your default shell using usermod, open a terminal with root privileges and type this command:

sudo usermod -s /path/to/new/shell your_username

Here, you need to replace /path/to/new/shell with the desired shell path and your_username with your actual username.

For example, let’s change the default bash shell to zsh:

sudo usermod -s /bin/zsh user

Just like before, log out and log back into your Linux system to make these changes take effect.

You can verify the switch to the new shell by looking at the user account details with the following command:

getent passwd user

Manually Changing the Default Shell in Linux

You can also change the default shell manually by editing the /etc/passwd file. You can do this by replacing the default shell path with the new shell. However, be careful with this technique, as it can lead to issues if not done correctly.

To manually change your shell, open the /etc/passwd file using your favorite text editor (like nano or vim) with root privileges:

sudo nano /etc/passwd

Look for your username in the file. The last field in your user’s entry will show your current shell. Change it to your desired shell’s path.

For example, let’s change our default /bin/bash shell path to /bin/zsh:

Lastly, save the file by pressing Ctrl + S and exit the editor with Ctrl + X. Again, log out and back in to apply the changes.

Alternative to Your Default Shell

While Bash is a great and powerful scripting tool, there are several other shells that can enhance your command-line experience and provide better customization options:

Fish

fish refers to Friendly Interactive Shell. This shell has an intuitive command-line interface, with syntax highlighting, autosuggestions, and tab completions that make typing commands a breeze.

fish also includes a web-based configuration tool. This means you can tweak settings and create functions without digging into configuration files.

Zsh

Zsh, or Z shell, is highly customizable and has a lot of useful features. Some of its best features include globbing, autocorrection, custom configurations, and advanced tab completion.

Zsh also supports plugins and themes, making it easy to tailor your shell experience to your liking. With Zsh, you can also show your Git branch and perform complex file manipulations with simple patterns.

Ksh

ksh, or the KornShell, takes the best features from the C shell and Bourne shell, making it a powerful and easy-to-use shell with advanced scripting capabilities. Some features of ksh include built-in math functions, command-line editing, shell scripting, and extensibility.

Wrapping Up

Changing your default shell in Linux is a straightforward process that can enhance your command-line experience. If you are a power user, you should definitely try out other alternative Linux shells.

Image credit: Lukas via Unsplash. All screenshots by Haroon Javed.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Haroon Javed
Contributor

Haroon is a lifelong tech enthusiast with over five years of experience writing thousands of articles about Linux, programming languages, and more. He loves exploring new technologies and experimenting with them to find innovative ways to use them. Haroon’s work has been featured on various online platforms, including HTG, Baeldung, and LinuxHint.

Comments (2)