kazmax - Home Server on Linux

How to check the Linux user list and about /etc/passwd file

What kind of users are in this Linux OS? There are times when I want to check the list of users.
This can be resolved by checking the "/etc/passwd" file.

Last Update : December 05, 2018

How to check the Linux user list and about "/etc/passwd" file Contents

  1. How to read "/etc/passwd" file
  2. "/etc/passwd" and "/etc/shadow"
  3. Output the user list from "/etc/passwd" file

1. How to read "/etc/passwd" file

User information can be checked in the /etc/passwd file. User name, password, user ID, group ID, comment (real name or telephone number etc.) Home directory, login shell.

The contents of /etc/passwd are like this.

[foo@localhost ~]$ 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
・・・
foo:500:501:KAZMAX_USER:/home/foo:/bin/bash
foo User name
x The letter "x" or encrypted password. "x" means shadow password is used.
500 User ID
501 Group ID
KAZMAX_USER Comment (real name, phone number etc). If there is no comment, nothing is written.
/home/foo The user's home directory.
/bin/bash The login shell for that user.

Notes

If the password part is blank, you may be able to log in without a password. Let's set it immediately. Conversely, if you delete "x" of user from "/etc/passwd" file, it means that no password is set and you can log in without password. (In environments where you can log in with no password)

2. "/etc/passwd" and "/etc/shadow"

In recent distributions, "x" is often written in the password part of "/etc/passwd" file.
Previously, encrypted passwords were written to the "/etc/passwd" file.

It seems that the "/etc/passwd" file can be referred to by the general user and the encrypted password was also decrypted in some cases. Also, if you prevent the general user from reading the "/etc/passwd" file, you will not be able to log in.

For this reason, the shadow password is used and the password part of the "/etc/passwd" file is written as "x". Instead, the "/etc/shadow" file contains encrypted passwords.

By the way, the "/etc/shadow" file is root's read-only.

[root@localhost ~]# ls -l /etc/shadow
-r--------  1 root root 1559  4月 14 00:25 /etc/shadow

3. Output the user list from "/etc/passwd" file

Output the user list from the /etc/passwd file.

[foo@localhost ~]$ cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
・・・

Notes

It is convenient to register as an alias.
Register the alias with the name "listusers".
Add it to ".bashrc" in the home directory so that it can be used when logging in.

[foo@localhost ~]$ cd
[foo@localhost ~]$ vi .bashrc
alias listusers="cut -d: -f1 /etc/passwd"  ← Fill out
・・・

[foo@localhost ~]$ source .bashrc
[foo@localhost ~]$ listusers
root
bin
daemon
adm
lp
・・・

Notes

In the package named "glibc-common" there is a command "getent" and you can output a list of users including the ldap account by executing it with the option "passwd".

[foo@localhost ~]$ 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
・・・