Linux Command List of Users: Top Commands to View and Manage User Accounts

Share On

Introduction

Managing user accounts is an essential task for system administrators in a Linux environment. Whether you need to view the list of users or perform actions like creating, modifying, or deleting user accounts, having the right commands at your disposal is crucial. In this article, we will explore a comprehensive list of Linux commands that can help you effectively view and manage user accounts.

1. cat /etc/passwd

The cat /etc/passwd command is one of the most basic and commonly used commands to view user accounts in Linux. The /etc/passwd file contains information about each user account on the system, including the username, user ID, group ID, home directory, and default shell. By using the cat command, you can display the contents of the /etc/passwd file on the terminal.

To view the list of users, simply open a terminal and type:

cat /etc/passwd

This will display the entire contents of the /etc/passwd file, including all user accounts on the system.

2. cut -d: -f1 /etc/passwd

The cut -d: -f1 /etc/passwd command is another useful way to extract the list of usernames from the /etc/passwd file. The cut command is used to extract specific sections or fields from a file, and in this case, we are specifying the delimiter as “:” and extracting the first field, which corresponds to the username.

To view the list of usernames, open a terminal and enter the following command:

cut -d: -f1 /etc/passwd

This will display a list of usernames, with each username on a separate line.

3. getent passwd

The getent passwd command is a versatile command that retrieves user account information from various sources, including local files and network databases. It is particularly useful in environments where user information is stored in a centralized directory service like LDAP or NIS.

To view the list of user accounts using the getent passwd command, open a terminal and type:

getent passwd

This command will display a list of user accounts, similar to the output of the cat /etc/passwd command, but with the added ability to retrieve information from different sources.

4. awk -F: ‘{ print $1}’ /etc/passwd

The awk -F: ‘{ print $1}’ /etc/passwd command is another way to extract the list of usernames from the /etc/passwd file. Awk is a powerful text processing tool that allows you to manipulate and extract data from structured text files.

To view the list of usernames using the awk command, open a terminal and enter the following command:

awk -F: '{ print $1}' /etc/passwd

This command will extract the first field (username) from each line of the /etc/passwd file and display it on the terminal.

5. lslogins -u

The lslogins -u command is a specialized command that provides detailed information about user accounts on a Linux system. It is part of the util-linux package and offers a wide range of options to view and manage user accounts.

To view the list of user accounts using the lslogins command, open a terminal and type:

lslogins -u

This command will display a detailed list of user accounts, including the username, user ID, group ID, home directory, and login shell.

6. w

The w command provides real-time information about currently logged-in users. It displays a list of logged-in users, their terminal or remote session, login time, idle time, and the command they are currently running.

To view the list of currently logged-in users, open a terminal and enter the following command:

w

This will display a table with information about each logged-in user, including their username, terminal, login time, and idle time.

7. who

The who command is similar to the w command and provides information about currently logged-in users. However, it provides a more concise output, displaying only the username, terminal, and login time.

To view the list of currently logged-in users using the who command, open a terminal and type:

who

This will display a list of currently logged-in users, including their username, terminal, and login time.

8. users

The users command is a simple command that displays a list of currently logged-in users. It provides a space-separated list of usernames without any additional information.

To view the list of currently logged-in users using the users command, open a terminal and enter the following command:

users

This will display a list of currently logged-in users, with each username separated by a space.

9. id

The id command is used to display information about a specific user or the current user. It provides detailed information about the user’s UID (user ID), GID (group ID), and the groups they belong to.

To view information about a specific user using the id command, open a terminal and type:

id username

Replace “username” with the actual username you want to view information for. This command will display the UID, GID, and group membership information for the specified user.

10. finger

The finger command is a versatile command that provides detailed information about user accounts. It displays information such as the user’s login name, real name, terminal, idle time, login time, and office location.

To view information about a specific user using the finger command, open a terminal and enter the following command:

finger username

Replace “username” with the actual username you want to view information for. This command will display detailed information about the specified user.

11. last

The last command displays a list of recently logged-in users. It reads the /var/log/wtmp file, which contains information about previous user logins and logouts.

To view the list of recently logged-in users using the last command, open a terminal and type:

last

This will display a list of recently logged-in users, including their username, terminal, login time, and logout time (if available).

12. ls -l /home

The ls -l /home command is a simple way to view the list of user directories in the /home directory. Each user on a Linux system typically has a corresponding directory in the /home directory, which serves as their home directory.

To view the list of user directories using the ls command, open a terminal and enter the following command:

ls -l /home

This will display a detailed list of user directories, including the username, permissions, owner, group, and size.

13. ypcat passwd

The ypcat passwd command is used to view user account information in a NIS (Network Information Service) environment. NIS is a directory service that allows centralized management of user accounts in a networked environment.

To view the list of user accounts in a NIS environment using the ypcat passwd command, open a terminal and type:

ypcat passwd

This command will display a list of user accounts stored in the NIS database, including the username, UID, GID, home directory, and login shell.

14. ypcat shadow

The ypcat shadow command is similar to the ypcat passwd command but specifically displays information from the NIS shadow database. The shadow database stores password-related information, such as the encrypted password and password expiration date.

To view the list of user accounts in the NIS shadow database using the ypcat shadow command, open a terminal and enter the following command:

ypcat shadow

This command will display a list of user accounts stored in the NIS shadow database, including the username and password-related information.

15. ypcat group

The ypcat group command is used to view information about groups in a NIS environment. It displays a list of groups and their corresponding group IDs (GIDs).

To view the list of groups in a NIS environment using the ypcat group command, open a terminal and type:

ypcat group

This command will display a list of groups stored in the NIS database, including the group name and GID.

16. dscl . -list /Users

The dscl . -list /Users command is used to list user accounts in a macOS environment. It interacts with the Directory Service Command Line utility, which provides access to the Directory Services database on macOS.

To view the list of user accounts using the dscl command, open a terminal and enter the following command:

dscl . -list /Users

This command will display a list of user accounts stored in the Directory Services database on macOS.

17. dscacheutil -q user

The dscacheutil -q user command is another way to view user accounts in a macOS environment. It queries the system’s cache for user account information and displays it on the terminal.

To view the list of user accounts using the dscacheutil command, open a terminal and type:

dscacheutil -q user

This command will display a list of user accounts stored in the system’s cache, including the username, UID, GID, and home directory.

18. dscl . -list /Users | grep -v ‘^_’

The dscl . -list /Users | grep -v ‘^_’ command is a combination of the dscl and grep commands. It lists user accounts in a macOS environment and filters out system accounts that start with an underscore.

To view the list of user accounts using the dscl and grep commands, open a terminal and enter the following command:

dscl . -list /Users | grep -v '^_'

This command will display a list of user accounts stored in the Directory Services database on macOS, excluding system accounts.

19. dscl . -list /Users | grep -v ‘^_.*’

The dscl . -list /Users | grep -v ‘^_.*’ command is a variation of the previous command and further refines the filtering of system accounts. It uses a regular expression to exclude any accounts that start with an underscore followed by any number of characters.

To view the list of user accounts using the refined dscl and grep command, open a terminal and type:

dscl . -list /Users | grep -v '^_.*'

This command will display a list of user accounts stored in the Directory Services database on macOS, excluding system accounts.

20. dscl . -list /Users | grep -v ‘^_[^_]’

The dscl . -list /Users | grep -v ‘^_[^_]’ command is another variation of the previous command and provides an alternative way to filter out system accounts. It uses a regular expression to exclude any accounts that start with an underscore followed by a single character.

To view the list of user accounts using this refined dscl and grep command, open a terminal and enter the following command:

dscl . -list /Users | grep -v '^_[^_]'

This command will display a list of user accounts stored in the Directory Services database on macOS, excluding system accounts.

In conclusion, these Linux commands provide a variety of ways to view and manage user accounts on a Linux system. Whether you need a simple list of usernames or detailed information about user accounts, these commands offer flexibility and efficiency in managing user accounts. By familiarizing yourself with these commands, you can streamline your administrative tasks and ensure the security and efficiency of your Linux environment.

FAQs

1. How can I create a new user account in Linux?

To create a new user account in Linux, you can use the useradd command. For example, to create a user named “john”, you can run the following command:

sudo useradd john

This will create a new user account with the default settings. You can then set a password for the user using the passwd command:

sudo passwd john

2. How can I delete a user account in Linux?

To delete a user account in Linux, you can use the userdel command. For example, to delete the user account “john”, you can run the following command:

sudo userdel john

This will remove the user account and delete any associated files and directories. Be cautious when deleting user accounts, as this action is irreversible.

3. How can I modify user account settings in Linux?

To modify user account settings in Linux, you can use the usermod command. This command allows you to change various attributes of a user account, such as the username, home directory, login shell, and more. For example, to change the home directory of the user “john”, you can run the following command:

sudo usermod -d /new/home/directory john

This will change the home directory of the user “john” to the specified directory. You can use the usermod command with different options to modify other attributes of a user account.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *