How To List Users In Linux (Ubuntu, Mint, Debian, CentOS)?

Users are one of the most important parts of the Linux operating systems like Ubuntu, Debian, Mint, Kali, CentOS, etc. By creating user accounts different users and people can log in and use the Ubuntu system with their own permissions, files, and configuration.

User Database File /etc/passwd

The Linux users and related information like shell type, user id, etc are stored in passwd file which is located /etc/passwd. This file can be read by all users but only changed by administrative users and the root user. Thecontent of the passwd file can be printed with the cat command like below.


$ cat /etc/passwd
List Linux Users with Related Information
  • Username is the official name of the user which will be verified during logins or SSH connections.
  • The encrypted password is depicted as x because it is sensitive information and stored in the /etc/shadow file.
  • User ID (UID) is the user’s numerical and unique number.
  • User’s Group ID (GID) users own group numerical and unique number.
  • Full name is the name and surname of the user.
  • Home Directory is the path of the user’s home directory where the private files and folder are stored.
  • Login Shell is the user’s default shell which will be provided for console and terminal access.

List Users with cat Command Via /etc/passwd File

As stated previously the users and related information is stored inside the /etc/passwd file. This file is column are separated with semicolons where the user name is the first column. The user’s names can be extracted by using tools like awk, cut, etc. and only the user names can be printed.

$ cut -d: -f1 /etc/passwd

The output will be like below where the username will be listed according to the line sequence.

root
daemon
bin
sys
sync
games
...
gdm
ismail
systemd-coredump
lightdm
libvirt-qemu
libvirt-dnsmasq
sshd

We can also sort these users according to their user names but using the sort command like below. We will just redirect the cut command output into the sort command where the usernames will be sorted and listed alphabetically which starts from a.

$ cut -d: -f1 /etc/passwd | sort
_apt
avahi
avahi-autoipd
backup
bin
colord
cups-pk-helper
daemon
dnsmasq
...
systemd-resolve
systemd-timesync
tcpdump
tss
usbmux
uucp
uuidd
whoopsie
www-data

Also the awk command can be used like below to list linux users.

$ awk -F: '{ print $1}' /etc/passwd

List Users with getent Command

The getent command is used to display users from the database configured with /etc/nsswitch.conf and /etc/passwd. The nsswitch.conf is used to configure an LDAP source which will be generally an active directory to manage users from a centralized source.

$ getent passwd

Like previous way the cut and awk commands can be used to extract only user names from the getent command output.

$ getent passwd | awk -F: '{ print $1}'

or the cut command can be used like below.

$ getent passwd | cut -d: -f1

Check If User Exist

A typical Linux system contains a lot of users for the system related jobs and normal users which are human. Searching and finding a specific user or username in the output of the user listing commands can be error prone and trivial task. You can use the grep command in order to search for a specific user or username wheter it exist. If the user exist in the linux system the user name line will be printed to the terminal and if not exist there will be no output about the user.

$ getent passwd | grep "ismail"

or with the cat command;

$ cat /etc/passwd | grep "ismail"

The output is like below which means the user ismail exist in the current linux system.

Check If User Exist
READ  Read File Line By Line In Bash

Leave a Comment