Three ways for linux to view all users

Use the / etc/passwd file, getent command and compgen command to view the user information in the system.

As we all know, the user information in the Linux system is stored in the / etc/passwd file.

This is a text file containing basic information of each user. When we create a user in the system, the details of the new user will be added to this file.

/The etc/passwd file records the basic information of each user as a line in the file, which contains seven fields.

/A line in the etc/passwd file represents a single user. This file divides the user's information into three parts.

  • Part 1: root user information
  • Part 2: system defined account information
  • Part 3: account information of real users

The first part is the root account, which represents the administrator account and has full authority over every aspect of the system.

The second part is the system defined groups and accounts, which are necessary for the correct installation and update of the system software.

At the end of the third part, it represents a real user using the system.

When you create a new user, the following four files are modified.

  • /etc/passwd: the details of the user account are updated in this file.
  • /etc/shadow: the user account password is updated in this file.
  • /etc/group: the details of the new user group are updated in this file.
  • /etc/gshadow: the new user group password is updated in this file.
  1. : use the / etc/passwd file

Use any file operation command such as cat, more, less to print the list of users created on Linux system.

/etc/passwd is a text file that contains the information of each user necessary to log in to the Linux system. It stores useful information of users, such as user name, password, user ID, group ID, user ID information, user's home directory and Shell.

/The etc/passwd file writes the details of each user as a line, which contains seven fields, separated by colons:

[root@centosb ~]# 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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/false
ntp:x:38:38::/etc/ntp:/sbin/nologin

The details of the seven fields are as follows.

User name (ntp): the user name of the created user, with a character length of 1 to 12 characters.
Password (x): represents that the encrypted password is saved in the / etc/shadow file.
User ID (38): represents the user's ID number. Each user must have a unique ID. UID numbers 0 are reserved for root users, UID numbers 1 to 99 are reserved for system users, and UID numbers 100-999 are reserved for system accounts and groups.
Group ID (38): represents the ID number of the group. Each group must have a unique GID, which is saved in the / etc/group file.
User information (MySQL Server): represents the description field, which can be used to describe the user's information.
Home directory (/ var/lib/mysql): represents the user's home directory.
Shell (/ bin/false): represents the shell type used by the user.

  you can use the awk or cut command to print out only the list of user names of all users in the Linux system. The results displayed are the same.

[root@centosb ~]# awk -F':' '{ print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
tss
abrt
sshd
postfix
chrony
mysql
ntp
  1. Using the getent command
    The getent command displays entries in the database supported by the Name Service Switch library. The configuration file for these libraries is / etc/nsswitch.conf.
    The getent command displays user details similar to the / etc/passwd file, which displays each user's details as a single line with seven fields.
getent passwd
  1. Using the compgen command

compgen is bash's built-in command. It will display all available commands, aliases and functions.

compgen -u

Tags: Linux Operation & Maintenance

Posted on Thu, 02 Dec 2021 19:49:50 -0500 by dloeppky