How to view all users in linux

青灯夜游
Release: 2023-02-16 11:12:13
Original
40860 people have browsed it

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".

How to view all users in linux

#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 部分:真实用户的账户信息
Copy after login

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`: 新用户群组密码在此文件中更新。
Copy after login

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
Copy after login

The details of the 7 fields are as follows.

  • 用户名magesh): 已创建用户的用户名,字符长度 1 个到 12 个字符。
  • 密码x):代表加密密码保存在 `/etc/shadow 文件中。
  • **用户 ID(506):代表用户的 ID 号,每个用户都要有一个唯一的 ID 。UID 号为 0 的是为 root 用户保留的,UID 号 1 到 99 是为系统用户保留的,UID 号 100-999 是为系统账户和群组保留的。
  • **群组 ID (507):代表群组的 ID 号,每个群组都要有一个唯一的 GID ,保存在 /etc/group文件中。
  • **用户信息(2g Admin - Magesh M):代表描述字段,可以用来描述用户的信息(LCTT 译注:此处原文疑有误)。
  • **家目录(/home/mageshm):代表用户的家目录。
  • **Shell(/bin/bash):代表用户使用的 shell 类型。

你可以使用 awkcut 命令仅打印出 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
Copy after login

方法 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
Copy after login

7 个字段的详细信息如上所述。(LCTT 译注:此处内容重复,删节)

你同样可以使用 awkcut 命令仅打印出 Linux 系统中所有用户的用户名列表。显示的结果是相同的。

方法 3 :使用 compgen 命令

compgenbash 的内置命令,它将显示所有可用的命令,别名和函数。

# compgen -u
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
ftp
postfix
sshd
tcpdump
2gadmin
apache
zabbix
mysql
zend
rpc
2daygeek
named
mageshm
Copy after login

相关推荐:《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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template