chsh Command in Linux with Examples

August 15, 2023

Introduction

The bash shell is among Linux's most popular login shells. However, alternatives are available for different command-line operations. The chsh (change shell) command in Linux enables users to modify the login shell. 

The following tutorial explains how to use the chsh command in Linux with examples. 

chsh Command in Linux with Examples

Requirements

  • A Linux system (this tutorial uses Ubuntu 22.04 and CentOS7).
  • Access to the terminal.
  • Sudo privileges for some examples.

chsh Command Syntax

The chsh command is used to change the login shell for a user. The syntax is as follows:

chsh [Options] [Shell] [Username]

None of the arguments are mandatory. When invoked on its own, chsh offers the opportunity to change the shell for the current user:

chsh
chsh terminal output

The terminal asks for the current user's password. Next, the command requires to specify the new shell for the current user. An alternative is hitting Enter to keep the default shell.

Terminal output for chsh

The options customize the output of the command. The [shell] argument is also optional, but if it is not specified, the command prompts you to enter a shell. The username is the name of the account whose shell the user wants to change.

chsh Command Options

The chsh command has several options for customizing the output. All arguments are in the table below:

OptionExplanation
-lSpecifies the user's login shell.
-uShows the available shells list.
-sAssigns the specified shell as the user's login shell. 
-RDirectory to chroot into.
-vPrints details about the chsh version and exits.
-hDisplays help text and exits.

Note: Not all arguments work in every Linux distribution and version.

chsh Command Examples

Given the command's straightforward nature, practical examples assist in illustrating its various uses. The following sections demonstrate chsh usage through examples.

Example 1: Show Available Shells

Before using chsh to change the shell, check the available shells in the system.

Running chsh with the -l argument prints the available shell list. However, in certain Ubuntu and Debian-based Linux systems, chsh may work differently.

For instance, run the following command in Ubuntu 22.04:

chsh -l
Ubuntu: chsh -l terminal output

The output does not recognize the -l option. Running man, further verifies this:

 man chsh
chsh man terminal output

The -l option is not listed among the chsh arguments.

On the other hand, running the same command on CentOS7 provides the output:

chsh -l
CentOS: chsh -l terminal output

To avoid such issues, use cat to list available shells:

cat /etc/shells
cat /etc/shells terminal output

Example 2: Verify Current Shell

Apart from having a chance to change the shell for the current user, running chsh without arguments shows the current shell.

Run the command:

chsh
showing current shell

The square brackets in the output show the current shell is /bin/bash/.

Example 3: Change Login Shell

To change the current user shell to, for example, /usr/bin/tmux, use -s with chsh:

chsh -s /usr/bin/tmux

The command has no output. To verify the change, run chsh with no arguments:

chsh
Verifying shell change

The value in square brackets is /usr/bin/tmux, confirming the change.

Example 4: Changing Login Shell for Another User

Root or a user with sudo privileges can set a new login shell for another user by providing the username as an argument.

For instance, change the default shell for user1 with:

sudo chsh user 1
sudo chsh user1 terminal output

Choose the new shell by typing the path. This example shows the /bin/sh as the chosen shell:

sudo chsh user1 changed shell

To verify the change:

1. Switch to user1 with su:

su user1
su user1 terminal output

2. Run chsh:

Confirmed shell change for user1

The output shows the shell changed do /bin/sh.

Example 5: Change Shell Without User Interaction

 By default, chsh is interactive, which means it prompts the user for the new shell. However, -s changes the shell in a non-interactive way. For example, change the current user's login shell to /bin/sh:

chsh -s /bin/sh
chsh -s shell terminal output

This command doesn’t have output. However, chsh -s changes the shell without further user input. Verify the change by running chsh.

The -s argument also allows to change the shell for a different user without any interaction. This command requires root or sudo privileges. The syntax is:

chsh -s [Shell] [Username]

For example, change the shell for user1 to /bin/bash:

sudo chsh -s /bin/bash user1
sudo chsh -s user1 shell terminal output

Example 6: Apply Changes in Different Environment

The chsh command is able to change the shell for a user in a different environment. To do this, use -R. The -R argument tells chsh to use the configuration files from the CHROOT_DIR directory.

The chroot utility changes the root directory for a process. This means the process only sees the files and directories located inside the CHROOT_DIR directory. This is useful for security purposes, as it limits the process's access to the rest of the file system

Note: The -R option isn't a standard option for chsh in all Linux distributions and versions. If this option is still not part of the chsh command on your system, the command returns an error.

To test the command, we'll change the login shell for the user sara in a chrooted environment located in /mnt/my_chroot_dir with:

sudo chsh -R /mnt/chroot -s /bin/sh sara
sudo chsh -R /mnt/chroot -s /bin/sh sara

If the command runs successfully, there isn't any output. 

The command contains:

  • -R /mnt/chroot. The part that specifies the chroot directory.
  • -s /bin/sh. The part which sets the new login shell to sh.
  • sara. The name of the user for whom the login shell is being changed.

Note: The -R argument only works if chsh is run as root or with sudo privileges.

Example 7: Print the Usage

Run the -u option with chsh to show the usage message and exit.

For example, run: 

chsh -u
chsh -u terminal output

The argument displays the usage information for the chsh command without changing the login shell.

Conclusion

After reading this article, you now know everything about the chsh command to get started.

Next, learn about the usermod command.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
8 Types of Linux Shells
October 26, 2022

You've probably heard of the Bourne shell and the bash shell. But did you know there are other alternatives? This...
Read more
How to Open a File in Bash Shell
August 7, 2023

If you want to open a file in Bash, there are several possibilities. This article explains how to open files using the terminal commands or different text...
Read more
How to Switch from Zsh to Bash
August 2, 2023

Zsh is the default macOS terminal shell since macOS Catalina, where it replaced Bash. However, there are still use cases where...
Read more
How to Append to File in Bash
May 30, 2023

This guide explains how to use the Bash shell to append text to an existing file without overwriting its contents...
Read more