chsh Command in Linux

Introduction

In this hands-on lab, you'll discover how to leverage the chsh command to modify the default shell associated with a user account within a Linux system. The chsh command empowers you to define the shell program that automatically launches when you log in to your designated user account. This tutorial guides you through verifying the successful alteration of the default shell and ensuring the new shell operates as expected upon login.

This lab presents the vital procedures for managing user shells in a Linux environment, a cornerstone of effective user and permission management for any systemadmin. The clearly articulated instructions accompanied by real-world examples simplify the understanding and practical application of the chsh command.

Understand the chsh Command

In this segment, we'll delve into the intricacies of the chsh command, a powerful tool used to redefine the default shell for user accounts on a Linux system.

The chsh command provides the capability to change the default shell, the program that initiates upon logging in. While the default is often set to /bin/bash, you have the flexibility to switch to any other shell residing on your system, such as /bin/zsh, /bin/fish, or /bin/tcsh. This is crucial for systemadmin tasks related to user environment customization.

To execute the chsh command, simply input the following in your terminal:

sudo chsh -s /bin/zsh labex

This instruction modifies the default shell for the user labex to /bin/zsh.

Example output:

Changing shell for labex.
chsh: shell '/bin/zsh' does not exist

In this scenario, the specified shell /bin/zsh is absent from the system, leading to command failure. Verify the existence of the desired shell on your system before attempting to set it.

After modifying the default shell, validate the changes by executing:

echo $SHELL

This will display the currently active shell utilized by the user.

Example output:

/bin/bash

Here, the default shell remains /bin/bash, indicating the change hasn't yet taken effect. A logout and subsequent login may be necessary for the update to be applied. This is a common troubleshooting step for systemadmin when dealing with shell changes.

Change the Default Shell Using chsh

This section demonstrates how to change a user account's default shell using the chsh command.

First, let's determine the current default shell configured for the labex user:

sudo chsh -s /bin/bash labex
echo $SHELL

Example output:

/bin/bash

As shown, the labex user currently defaults to /bin/bash.

Now, let's proceed with changing the default shell to /bin/zsh:

sudo chsh -s /bin/zsh labex

Executing this will update the labex user's default shell to /bin/zsh.

To confirm the successful change, re-check the active shell:

echo $SHELL

Example output:

/bin/zsh

The output confirms that the default shell has been successfully switched to /bin/zsh.

Verify the Changed Default Shell

In this final stage, we'll rigorously verify that the default shell for labex is indeed /bin/zsh.

First, a quick check of the current shell:

echo $SHELL

Example output:

/bin/zsh

As evidenced, the system now defaults to /bin/zsh, confirming a successful change. This is a key step for systemadmin to ensure configurations are properly applied.

Further confirmation can be obtained by inspecting the user's shell information within the /etc/passwd file:

sudo cat /etc/passwd | grep labex

Example output:

labex:x:1000:1000:labex,,,:/home/labex:/bin/zsh

This output clearly indicates that the labex user's shell is set to /bin/zsh.

For conclusive verification, log out and back in as the labex user to guarantee the new default shell is utilized. This is a standard practice for systemadmin to validate user environment changes.

Summary

This lab provided practical experience with the chsh command, a fundamental tool for systemadmin responsible for managing user account shells in a Linux environment. We explored the command's purpose and its usage in changing the default shell. A step-by-step demonstration illustrated how to transition the labex user's shell from /bin/bash to /bin/zsh, accompanied by thorough verification of the change. This lab equips users with the knowledge and skills to effectively manage default shells for their user accounts, improving their Linux system administration capabilities and overall user experience.

400+ Linux Commands