This brief guide explains how to list all users in Linux operating systems. Before getting into the topic, we will see what is /etc/passwd file. Because we depend on this file through out this guide.
Table of Contents
A brief note about the /etc/passwd file
In Linux (and Unix in general), the basic information about each user account is stored in "/etc/passwd" file. It is nothing but a plain text file that contains the attributes of all user accounts present in a Linux system.
By default, this file is readable by all users with the help of any text or graphical programs, such as Vi, Nano, Gedit etc or with commands like "cat".
If you open this file..
$ cat /etc/passwd
...you will see several entries like below:
root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin [...]
Each line in the /etc/passwd file represents a single user. The root user will be listed on the top. Each line contains seven attributes or fields separated by a colon without any spaces between each field. The name of the seven fields are given below:
- name,
- password,
- user ID,
- group ID,
- gecos,
- home directory,
- shell.
1. Name
The "name" field is the user's login name that you use to login to your Linux box.
2. Password
The "password" field merely contains a letter X. Meaning - the password is encrypted and stored in a separate file called "/etc/shadow" for security purpose. The /etc/shadow can not be read by the normal users. Here the X indicates that the user has a password. If this field is empty, the user has no password, so he can login without password.
3. User ID
The "User ID" (or UID in short) indicates the user's unique numeric identification number. The root account's UID is 0 (zero). The numbers from 1 to 99 are reserved for system accounts. And the numbers starting from100 to 999 are reserved for normal users and groups.
4. Group ID
The "Group ID" (or GID in short) indicates the primary group identifier the user belongs to. The GID is usually the same as the UID.
5. Geckos
The "geckos" field contains the general information of a user. For example, It may contain the user's real name. This filed might be either empty separated by two colons or it might contain multiple entries separated by commas.
6. Home directory
The "home directory" field indicates the full path of the user's home directory, for example /home/sk. This is the directory that the user is first in when he logged into a system. The home directory usually contains programs and configurations files belonged to the user.
7. Shell
The last filed is "shell". It indicates the full path of the default shell for that user.
Now, let us take the following line as an example:
root:x:0:0:root:/root:/bin/bash
In the above line,
- root - is the user name,
- x - indicates the root user has password,
- 0 - The first 0 (zero) indicates the UID,
- 0 - The second zero indicates the GID,
- root - It indicates the real name of the user.
- /root - It is the home directory of the root user,
- /bin/bash - Default shell for root user.
Hope you get a basic idea about the /etc/passwd file. Now let us get back to our topic i.e listing all users in a Linux system.
List All Users In Linux
There are couple ways to list all users in a Linux system. Here I have included all possible ways.
1. List all users in Linux using cat command
As the names says, the cat command is generally used to concatenate files and print the contents of the files.
To list all users in a Linux system using cut command, run:
$ cat /etc/passwd
This command will list all available users
Sample output:
root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin proxy:x:13:13:proxy:/bin:/usr/sbin/nologin www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin backup:x:34:34:backup:/var/backups:/usr/sbin/nologin list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin systemd-network:x:100:102:systemd Network Management,,,:/run/systemd/netif:/usr/sbin/nologin systemd-resolve:x:101:103:systemd Resolver,,,:/run/systemd/resolve:/usr/sbin/nologin syslog:x:102:106::/home/syslog:/usr/sbin/nologin messagebus:x:103:107::/nonexistent:/usr/sbin/nologin _apt:x:104:65534::/nonexistent:/usr/sbin/nologin uuidd:x:105:111::/run/uuidd:/usr/sbin/nologin avahi-autoipd:x:106:112:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/usr/sbin/nologin usbmux:x:107:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologin dnsmasq:x:108:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologin rtkit:x:109:114:RealtimeKit,,,:/proc:/usr/sbin/nologin cups-pk-helper:x:110:116:user for cups-pk-helper service,,,:/home/cups-pk-helper:/usr/sbin/nologin speech-dispatcher:x:111:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false whoopsie:x:112:117::/nonexistent:/bin/false kernoops:x:113:65534:Kernel Oops Tracking Daemon,,,:/:/usr/sbin/nologin saned:x:114:119::/var/lib/saned:/usr/sbin/nologin avahi:x:116:122:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/usr/sbin/nologin colord:x:117:123:colord colour management daemon,,,:/var/lib/colord:/usr/sbin/nologin hplip:x:118:7:HPLIP system user,,,:/var/run/hplip:/bin/false geoclue:x:119:124::/var/lib/geoclue:/usr/sbin/nologin gnome-initial-setup:x:120:65534::/run/gnome-initial-setup/:/bin/false gdm:x:121:125:Gnome Display Manager:/var/lib/gdm3:/bin/false sk:x:1000:1000:sk,,,:/home/sk:/bin/bash Debian-exim:x:122:128::/var/spool/exim4:/usr/sbin/nologin pulse:x:115:120:PulseAudio daemon,,,:/var/run/pulse:/usr/sbin/nologin systemd-timesync:x:123:127:systemd Time Synchronization,,,:/run/systemd:/usr/sbin/nologin tss:x:124:131:TPM software stack,,,:/var/lib/tpm:/bin/false tcpdump:x:125:132::/nonexistent:/usr/sbin/nologin _flatpak:x:126:133:Flatpak system-wide installation helper,,,:/nonexistent:/usr/sbin/nologin systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin
As I mentioned already, the first field in the above entries represents the users. If you don't want all fields but only display the user names, the following methods would help.
3. List users using cut command
The "cut" command is used to remove sections (or fields) from each line of files.
To list all users with cut command in a Linux system, run:
$ cut -d: -f1 /etc/passwd
This command will only display the user names from the /etc/passwd file and ignore all other fields.
Sample output:
root daemon bin sys sync games man lp mail news uucp proxy www-data backup list irc gnats nobody systemd-network systemd-resolve syslog messagebus _apt uuidd avahi-autoipd usbmux dnsmasq rtkit cups-pk-helper speech-dispatcher whoopsie kernoops saned avahi colord hplip geoclue gnome-initial-setup gdm sk Debian-exim pulse systemd-timesync tss tcpdump _flatpak systemd-coredump
4. Print users in Linux with awk command
We can also use "awk" command to list all users in Linux.
$ awk -F':' '{ print $1}' /etc/passwd
This command will also display only the users and omit all other details.
5. View list of users with compgen
Compgen is BASH built-in to manipulate the programmable completion facilities.
To list all users using compgen, run:
$ compgen -u
6. Display all users using getent command
Another way to list the users in Linux is to use "getent" command like below:
$ getent passwd
This command will read the /etc/passwd file and lists all users in that file.
One notable advantage of getent command is it not only lists the users in the /etc/passwd file but all users in all configured userdb backends, for example LDAP, on a given system.
7. List users using the "more" or "less" commands
The more and less commands are used to open a given file for interactive reading, allowing scrolling and search.
To print all users using more or less command, run:
$ more /etc/passwd
Or,
$ less /etc/passwd
Related read:
- How To List The Members Of A Group In Linux
- How To Find Currently Logged In Users In Linux
- How To Monitor User Activity In Linux
- How To Limit User’s Access To The Linux System
- The Difference Between more, less And most Commands
- How To Add, Delete And Grant Sudo Privileges To Users In Ubuntu
- How To Find Out Which Groups A User Belongs To In Linux
- The Right Way To Edit /etc/passwd And /etc/group Files In Linux
Hope this helps.
1 comment
Thanks a lot! One of the best websites of that kind on the web.