📅  最后修改于: 2023-12-03 15:17:20.763000             🧑  作者: Mango
In Linux, there are several ways to list the users of the system using shell or bash commands. This article will provide you with different methods to accomplish this task.
cat /etc/passwd
commandThe /etc/passwd
file contains a list of all users on the system. Each line in this file represents a user and contains information such as username, user ID, group ID, home directory, and shell. You can use the cat
command to display the contents of this file and filter out the required information using other shell commands.
For example:
cat /etc/passwd | awk -F: '{print $1}'
This command reads each line of the /etc/passwd
file, separates the fields using the :
delimiter (specified by the -F
option of awk
), and prints the first field (username) using the {print $1}
command of awk
. This will output a list of all usernames.
cut -d: -f1 /etc/passwd
commandSimilar to the previous method, you can also use the cut
command to extract the username field from the /etc/passwd
file. The -d
option specifies the delimiter (in our case, :
) and the -f
option specifies the field to cut (in this case, the first field).
For example:
cut -d: -f1 /etc/passwd
This command will give you a list of all usernames on the system.
getent passwd
commandThe getent
command is used to get entries from administrative databases, including the user database. By specifying the passwd
database, you can list all usernames.
For example:
getent passwd | cut -d: -f1
This command combines the getent
and cut
commands to extract the username field from the passwd
database.
/etc/passwd
file directlyIf you prefer a simple way to display the usernames without using any commands, you can directly view the /etc/passwd
file using a text editor or the less
command.
For example:
less /etc/passwd
This command will open the /etc/passwd
file in the less
pager, allowing you to scroll through the file and view all usernames.
These are some of the methods you can use to list users in Linux using shell or bash commands. Depending on your requirements and preferences, you can choose any of these methods to retrieve the desired information.