Merge pull request #9635 from SunWave/master

翻译完成
This commit is contained in:
MjSeven 2018-07-30 23:07:52 +08:00 committed by GitHub
commit fc28202f73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 234 additions and 237 deletions

View File

@ -1,237 +0,0 @@
Translating... by SunWave
3 Methods to List All The Users in Linux System
======
Everyone knows user information was residing in `/etc/passwd` file.
Its a text file that contains the essential information about each user.
When we create a new user, the new user details will be appended into this file.
The /etc/passwd file contains each user essential information as a single line with seven fields.
Each line in /etc/passwd represents a single user. This file keep the users information in three parts.
* `Part-1:` root user information
* `Part-2:` system-defined accounts information
* `Part-3:` Real user information
**Suggested Read :**
**(#)** [How To Check User Created Date On Linux][1]
**(#)** [How To Check Which Groups A User Belongs To On Linux][2]
**(#)** [How To Force User To Change Password On Next Login In Linux][3]
The first part is the root account, which is administrator account has complete power over every aspect of the system.
The second part is followed by system-defined groups and accounts that are required for proper installation and update of system software.
The third part at the end represent real people who use the system.
While creating a new users the below four files will be modified.
* `/etc/passwd:` User details will be updated in this file.
* `/etc/shadow:` User password info will be updated in this file.
* `/etc/group:` Group details will be updated of the new user in this file.
* `/etc/gshadow:` Group password info will be updated of the new user in the file.
### Method-1: Using /etc/passwd file
Use any of the file manipulation command such as cat, more, less, etc to print the list of users were created on your Linux system.
The `/etc/passwd` is a text file that contains each user information, which is necessary to login Linux system. It maintain useful information about users such as username, password, user ID, group ID, user ID info, home directory and shell.
The /etc/passwd file contain every user details as a single line with seven fields as described below, each fields separated by 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
```
Below are the detailed information about seven fields.
* **`Username (magesh):`** Username of created user. Characters length should be between 1 to 32.
* **`Password (x):`** It indicates that encrypted password is stored at /etc/shadow file.
* **`User ID (UID-506):`** It indicates the user ID (UID) each user should be contain unique UID. UID (0-Zero) is reserved for root, UID (1-99) reserved for system users and UID (100-999) reserved for system accounts/groups
* **`Group ID (GID-507):`** It indicates the group ID (GID) each group should be contain unique GID is stored at /etc/group file.
* **`User ID Info (2g Admin - Magesh M):`** It indicates the command field. This field can be used to describe the user information.
* **`Home Directory (/home/mageshm):`** It indicates the user home directory.
* **`shell (/bin/bash):`** It indicates the users bash shell.
You can use the **awk** or **cut** command to print only the user names list on your Linux system. Both are showing the same results.
```
# 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
```
### Method-2: Using getent Command
The getent command displays entries from databases supported by the Name Service Switch libraries, which are configured in /etc/nsswitch.conf.
getent command shows user details similar to /etc/passwd file, it shows every user details as a single line with seven fields.
```
# 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
```
Below are the detailed information about seven fields.
* **`Username (magesh):`** Username of created user. Characters length should be between 1 to 32.
* **`Password (x):`** It indicates that encrypted password is stored at /etc/shadow file.
* **`User ID (UID-506):`** It indicates the user ID (UID) each user should be contain unique UID. UID (0-Zero) is reserved for root, UID (1-99) reserved for system users and UID (100-999) reserved for system accounts/groups
* **`Group ID (GID-507):`** It indicates the group ID (GID) each group should be contain unique GID is stored at /etc/group file.
* **`User ID Info (2g Admin - Magesh M):`** It indicates the command field. This field can be used to describe the user information.
* **`Home Directory (/home/mageshm):`** It indicates the user home directory.
* **`shell (/bin/bash):`** It indicates the users bash shell.
You can use the **awk** or **cut** command to print only the user names list on your Linux system. Both are showing the same results.
```
# getent passwd | awk -F':' '{ print $1}'
or
# getent passwd | cut -d: -f1
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
ftp
postfix
sshd
tcpdump
2gadmin
apache
zabbix
mysql
zend
rpc
2daygeek
named
mageshm
```
### Method-3: Using compgen Command
compgen is bash built-in command and it will show all available commands, aliases, and functions for you.
```
# compgen -u
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
ftp
postfix
sshd
tcpdump
2gadmin
apache
zabbix
mysql
zend
rpc
2daygeek
named
mageshm
```
Please comment your inputs into our comment section, so based on that we can improve our blog and make effective article. So, stay tune with us.
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/3-methods-to-list-all-the-users-in-linux-system/
作者:[Magesh Maruthamuthu][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.2daygeek.com/author/magesh/
[1]:https://www.2daygeek.com/how-to-check-user-created-date-on-linux/
[2]:https://www.2daygeek.com/how-to-check-which-groups-a-user-belongs-to-on-linux/
[3]:https://www.2daygeek.com/how-to-force-user-to-change-password-on-next-login-in-linux/

View File

@ -0,0 +1,234 @@
列出 Linux 系统上所有用户的 3 种方法
=======
> 通过使用 `/etc/passwd` 文件,`getent` 命令,`compgen` 命令这三种方法查看系统中用户的信息
----------------
大家都知道Linux 系统中用户信息存放在 `/etc/passwd` 文件中。
这是一个包含每个用户基本信息的文本文件。
当我们在系统中创建一个用户,新用户的详细信息就会被添加到这个文件中。
`/etc/passwd` 文件将每个用户的基本信息记录为文件中的一行一行中包含7个字段。
`/etc/passwd` 文件的一行代表一个单独的用户。该文件将用户的信息分为 3 个部分。
* 第 1 部分:`root` 用户信息
* 第 2 部分:系统定义的账号信息
* 第 3 部分:真实用户的账户信息
** 建议阅读 : **
**( # )** [ 如何在 Linux 上查看创建用户的日期 ][1]
**( # )** [ 如何在 Linux 上查看 A 用户所属的群组 ][2]
**( # )** [ 如何强制用户在下一次登录 Linux 系统时修改密码 ][3]
第一部分是 `root` 账户,这代表管理员账户,对系统的每个方面都有完全的权力。
第二部分是系统定义的群组和账户,这些群组和账号是正确安装和更新系统软件所必需的。
第三部分在最后,代表一个使用系统的真实用户。
在创建新用户时,将修改以下 4 个文件。
* `/etc/passwd:` 用户账户的详细信息在此文件中更新。
* `/etc/shadow:` 用户账户密码在此文件中更新。
* `/etc/group:` 新用户群组的详细信息在此文件中更新。
* `/etc/gshadow:` 新用户群组密码在此文件中更新。
### 方法 1 :使用 `/etc/passwd` 文件
使用任何一个像 `cat``more``less` 等文件操作命令来打印 Linux 系统上创建的用户列表。
`/etc/passwd` 是一个文本文件,其中包含了登录 Linux 系统所必需的每个用户的信息。它保存用户的有用信息,如用户名,密码,用户 ID群组 ID用户 ID 信息,用户的家目录和 Shell 。
`/etc/passwd` 文件将每个用户的详细信息写为一行,其中包含七个字段,每个字段之间用冒号 “ :” 分隔:
```
# 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
```
7 个字段的详细信息如下。
* **`Username ( magesh ):`** 已创建用户的用户名,字符长度 1 个到 12 个字符。
* **`Password ( x ):`** 代表加密密码保存在 `/etc/shadow 文件中。
* **`User ID ( UID-506 ):`** 代表用户 ID ,每个用户都要有一个唯一的 ID 。UID 号为 0 的是为 `root` 用户保留的UID 号 1 到 99 是为系统用户保留的UID 号100-999 是为系统账户和群组保留的。
* **`Group ID ( GID-507 ):`** 代表组 ID ,每个群组都要有一个唯一的 GID ,保存在 `/etc/group` 文件中。
* **`User ID Info ( 2g Admin - Magesh M):`** 代表命名字段,可以用来描述用户的信息。
* **`Home Directory ( /home/mageshm ):`** 代表用户的家目录。
* **`shell ( /bin/bash ):`** 代表用户使用的 shell种类。
你可以使用 **`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 个字段的详细信息如下。
* **`Username ( magesh ):`** 已创建用户的用户名,字符长度 1 个到 12 个字符。
* **`Password ( x ):` 代表加密密码保存在 `/etc/shadow 文件中。
* **`User ID ( UID-506 ):`** 代表用户 ID ,每个用户都要有一个唯一的 ID 。UID 号为 0 的是为 `root` 用户保留的UID 号 1 到 99 是为系统用户保留的UID 号100-999 是为系统账户和群组保留的。
* **`Group ID ( GID-507 ):`** 代表组 ID ,每个群组都要有一个唯一的 GID ,保存在 `/etc/group` 文件中。
* **`User ID Info ( 2g Admin - Magesh M):`** 代表命名字段,可以用来描述用户的信息。
* **`Home Directory ( /home/mageshm ):`** 代表用户的家目录。
* **`shell ( /bin/bash ):`** 代表用户使用的 shell种类。
你可以使用 **`awk`** 或 **`cut`** 命令仅打印出 Linux 系统中所有用户的用户名列表。显示的结果是相同的。
```
# getent passwd | awk -F':' '{ print $1}'
or
# getent passwd | cut -d: -f1
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
ftp
postfix
sshd
tcpdump
2gadmin
apache
zabbix
mysql
zend
rpc
2daygeek
named
mageshm
```
### 方法 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
```
------------------------
via: https://www.2daygeek.com/3-methods-to-list-all-the-users-in-linux-system/
作者:[Magesh Maruthamuthu][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[SunWave](https://github.com/SunWave)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.2daygeek.com/author/magesh/
[1]:https://www.2daygeek.com/how-to-check-user-created-date-on-linux/
[2]:https://www.2daygeek.com/how-to-check-which-groups-a-user-belongs-to-on-linux/
[3]:https://www.2daygeek.com/how-to-force-user-to-change-password-on-next-login-in-linux/