How to check if a specific user shell is set in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check the default shell for a specific user in Linux. The shell is the command-line interpreter that processes your commands, and different users can have different default shells.

You will explore the /etc/passwd file to identify a user's default shell, understand how to list the available shells on the system using the chsh -l command, and verify a user's shell information using the getent passwd command. This lab provides fundamental knowledge for understanding user configurations in Linux.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux/BasicSystemCommandsGroup -.-> linux/help("Command Assistance") linux/BasicSystemCommandsGroup -.-> linux/man("Manual Access") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") subgraph Lab Skills linux/help -.-> lab-558766{{"How to check if a specific user shell is set in Linux"}} linux/man -.-> lab-558766{{"How to check if a specific user shell is set in Linux"}} linux/cat -.-> lab-558766{{"How to check if a specific user shell is set in Linux"}} end

Check user shell with cat /etc/passwd

In this step, you will learn how to check the default shell for a user in Linux. The shell is the command-line interpreter that processes your commands. Different users can have different default shells.

The /etc/passwd file contains information about all the users on the system. Each line in this file represents a user and is structured with fields separated by colons (:). The last field on each line specifies the user's default shell.

We can use the cat command to display the contents of the /etc/passwd file.

Open your terminal if it's not already open. Remember, you can find the Xfce Terminal icon on the left side of your desktop.

Now, type the following command and press Enter:

cat /etc/passwd

This command will print the entire content of the /etc/passwd file to your terminal. You will see many lines, each corresponding to a user account on the system.

Look for the line that starts with labex:. This is the entry for your current user. The line will look something like this (parts might vary):

labex:x:5000:5000:LabEx user,,,:/home/labex:/usr/bin/zsh

The fields are:

  1. Username: labex
  2. Password placeholder: x (the actual password hash is stored elsewhere for security)
  3. User ID (UID): 5000
  4. Group ID (GID): 5000
  5. User Information (GECOS): LabEx user,,,
  6. Home Directory: /home/labex
  7. Default Shell: /usr/bin/zsh

In this case, the default shell for the labex user is /usr/bin/zsh. This confirms that the terminal you are currently using is indeed zsh.

Understanding the /etc/passwd file is fundamental to Linux system administration. It provides a quick way to get basic information about users, including their default shell and home directory.

Click Continue to proceed to the next step.

List available shells with chsh -l

In the previous step, you saw that your default shell is /usr/bin/zsh. But what other shells are available on this system?

Linux systems often have multiple shells installed. You can list the available shells using the chsh command with the -l option.

The chsh command is typically used to change a user's login shell, but the -l option allows you to list the shells that are listed in the /etc/shells file. This file contains a list of valid login shells on the system.

In your terminal, type the following command and press Enter:

chsh -l

This command will output a list of paths, each representing an available shell on the system. The output will look similar to this:

/bin/sh
/bin/bash
/usr/bin/rc
/usr/bin/rbash
/usr/bin/nologin
/bin/false
/usr/bin/zsh
/usr/bin/ksh
/usr/bin/dash

You can see /usr/bin/zsh in this list, confirming it's a valid shell on this system. You might also see other common shells like /bin/bash (Bourne Again SHell) and /bin/sh (Bourne SHell).

Understanding which shells are available is useful if you ever need to change your default shell or if you encounter scripts written for a specific shell.

Click Continue to move on to the next step.

Verify shell with getent passwd

In the previous steps, you learned how to view the /etc/passwd file directly and list available shells. Now, let's use another command, getent, to retrieve user information, including the shell.

The getent command is a utility that gets entries from Name Service Switch (NSS) databases, which can include /etc/passwd, /etc/group, and others. It's a more standardized way to retrieve user information compared to directly reading /etc/passwd, as it can also query network-based user databases.

To get the entry for the labex user from the passwd database, type the following command in your terminal and press Enter:

getent passwd labex

This command specifically requests the entry for the user labex from the passwd database. The output will be similar to the line you saw when using cat /etc/passwd:

labex:x:5000:5000:LabEx user,,,:/home/labex:/usr/bin/zsh

Again, the last field /usr/bin/zsh confirms the default shell for the labex user.

Using getent is often preferred in scripts or for querying systems that might use centralized authentication systems (like LDAP) instead of just local files. For simple checks on a local system, both cat /etc/passwd and getent passwd <username> work.

You have now successfully used three different methods to understand user shells in Linux: directly viewing /etc/passwd, listing available shells with chsh -l, and querying user information with getent.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check a user's default shell in Linux. You used the cat /etc/passwd command to display user information, specifically focusing on the last field of each line which indicates the default shell. This method provides a direct way to identify the shell configured for any user on the system by examining the /etc/passwd file structure.