How to use the command 'chsh' (with examples)
- Linux
- December 17, 2024
The command chsh
, short for “change shell,” is a utility used to change a user’s default login shell. Part of the util-linux
package, chsh
is handy when a user wants to switch to a different shell environment on their Unix-like system. Users can interactively select a shell or specify their preferred shell via command line options. This tool not only enhances the user’s experience by allowing customization of their working environment, but it also accommodates different workflows and scripts that might depend on specific shells.
Use case 1: Set a specific login shell for the current user interactively
Code:
chsh
Motivation:
This use case is beneficial if you’re unsure about the current default shell or want a guided experience in selecting a new shell. Running chsh
without any options prompts the user to enter the full path of the new shell interactively. This is particularly useful for regular users who prefer a step-by-step process rather than dealing with paths and options directly.
Explanation:
chsh
: Invoked without options,chsh
initiates an interactive session where it asks the user to enter the full path of the desired new shell.
Example output:
Changing shell for username.
Password:
New shell [/bin/bash]: /bin/zsh
Shell changed.
In this interactive session, after verifying the password, the current shell is displayed in brackets, and the user can simply enter the new desired shell.
Use case 2: Set a specific login shell for the current user
Code:
chsh --shell /bin/zsh
Motivation:
Sometimes users want to set their login shell without going through an interactive session. This is often the case when the user knows exactly which shell they wish to change to and is comfortable providing the path directly. It saves time while offering a precise operation.
Explanation:
--shell
: This option allows the person using the command to specify the new shell they want to be set as default, identified by its full path.
Example output:
Password:
Shell changed.
After entering the password, the command will change the shell to the one specified, in this instance, /bin/zsh
.
Use case 3: Set a login shell for a specific user
Code:
sudo chsh --shell /bin/zsh username
Motivation:
System administrators often need to change the default login shell for other users on the system. This command facilitates that in a straightforward way by specifying both the shell and the user. Using sudo
ensures the user has the necessary privileges to modify another user’s shell.
Explanation:
sudo
: Grants administrative privileges necessary to change another user’s shell settings.--shell
: As in the previous example, this option requires the path of the desired shell.username
: Represents the user account whose shell settings are being changed.
Example output:
Password:
Shell changed.
Once authenticated, the specified user’s login shell changes to the new shell path provided.
Use case 4: List available shells
Code:
chsh --list-shells
Motivation:
Listing available shells is crucial when a user or administrator doesn’t know which shells are installed on the system or wants to verify a specific shell’s path. This use case ensures the user selects a valid shell that exists on the system.
Explanation:
--list-shells
: Option to display all shells that can currently be selected by thechsh
command.
Example output:
/bin/sh
/bin/bash
/bin/zsh
/bin/dash
The output lists all valid and available shells on the system, helping the user choose the right one for their needs.
Conclusion:
The chsh
command is a flexible and essential tool for anyone wanting to customize the shell environment in Unix-like systems. Through varied use cases, including interactive settings, specific path configurations, administrative settings for multiple users, and the simple listing of available options, chsh
offers comprehensive control over shell preferences. This capability not only optimizes individual workflows but also aligns the system behavior with the unique requirements of scripts and applications across different environments.