How to view all users in Linux: 1. Use cat and other file operation commands to read the contents of the "/etc/passwd" file and print the user list created on the Linux system. 2. Use the getent command to view the syntax "getent passwd" to display user details similar to the "/etc/passwd" file. 3. Use the compgen command with the syntax "compgen -u".
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
As we all know, user information in the Linux system is stored in the /etc/passwd file.
This is a text file containing basic information for each user. When we create a user in the system, the details of the new user are added to this file.
/etc/passwd The file records the basic information of each user as one line in the file, and one line contains 7 fields.
/etc/passwd A line in the file represents an individual user. This file divides the user's information into 3 parts.
* 第 1 部分:`root` 用户信息 * 第 2 部分:系统定义的账号信息 * 第 3 部分:真实用户的账户信息
The first part is the root account, this represents the administrator account and has complete power over every aspect of the system.
The second part is the system-defined groups and accounts that are required to correctly install and update system software.
The third part is at the end and represents a real user using the system.
When creating a new user, the following 4 files will be modified.
* `/etc/passwd`: 用户账户的详细信息在此文件中更新。 * `/etc/shadow`: 用户账户密码在此文件中更新。 * `/etc/group`: 新用户群组的详细信息在此文件中更新。 * `/etc/gshadow`: 新用户群组密码在此文件中更新。
Method 1: Use /etc/passwd
file
Use any of the file manipulation commands like cat, more, less etc. to print the list of users created on the Linux system.
/etc/passwd is a text file that contains the information for each user necessary to log in to the Linux system. It saves user's useful information such as username, password, user ID, group ID, user ID information, user's home directory and shell. The
/etc/passwd file writes each user's details as a single line containing seven fields, each separated by a colon ::
# cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin 2gadmin:x:500:10::/home/viadmin:/bin/bash apache:x:48:48:Apache:/var/www:/sbin/nologin zabbix:x:498:499:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin mysql:x:497:502::/home/mysql:/bin/bash zend:x:502:503::/u01/zend/zend/gui/lighttpd:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin 2daygeek:x:503:504::/home/2daygeek:/bin/bash named:x:25:25:Named:/var/named:/sbin/nologin mageshm:x:506:507:2g Admin - Magesh M:/home/mageshm:/bin/bash
The details of the 7 fields are as follows.
你可以使用 awk 或 cut 命令仅打印出 Linux 系统中所有用户的用户名列表。显示的结果是相同的。
# awk -F':' '{ print $1}' /etc/passwd or # cut -d: -f1 /etc/passwd root bin daemon adm lp sync shutdown halt mail ftp postfix sshd tcpdump 2gadmin apache zabbix mysql zend rpc 2daygeek named mageshm
方法 2 :使用 getent
命令
getent 命令显示 Name Service Switch 库支持的数据库中的条目。这些库的配置文件为 /etc/nsswitch.conf。
getent 命令显示类似于 /etc/passwd 文件的用户详细信息,它将每个用户详细信息显示为包含七个字段的单行。
# getent passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin 2gadmin:x:500:10::/home/viadmin:/bin/bash apache:x:48:48:Apache:/var/www:/sbin/nologin zabbix:x:498:499:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin mysql:x:497:502::/home/mysql:/bin/bash zend:x:502:503::/u01/zend/zend/gui/lighttpd:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin 2daygeek:x:503:504::/home/2daygeek:/bin/bash named:x:25:25:Named:/var/named:/sbin/nologin mageshm:x:506:507:2g Admin - Magesh M:/home/mageshm:/bin/bash
7 个字段的详细信息如上所述。(LCTT 译注:此处内容重复,删节)
你同样可以使用 awk 或 cut 命令仅打印出 Linux 系统中所有用户的用户名列表。显示的结果是相同的。
方法 3 :使用 compgen
命令
compgen 是 bash 的内置命令,它将显示所有可用的命令,别名和函数。
# compgen -u root bin daemon adm lp sync shutdown halt mail ftp postfix sshd tcpdump 2gadmin apache zabbix mysql zend rpc 2daygeek named mageshm
相关推荐:《Linux视频教程》
The above is the detailed content of How to view all users in linux. For more information, please follow other related articles on the PHP Chinese website!