How to List Users in Linux (All Commands)

Short reference
Command | What it shows |
|---|---|
| All accounts defined within the local password file |
| All accounts visible to the system, LDAP and NIS included |
| Only usernames, one line each |
| Only human user accounts, system/service accounts excluded |
| UID, GID and group membership of one user |
| Logged in users, terminal and login time included |
| User login history, completed sessions included |
| Full information of all accounts in a row: UID, GID, home directory, shell and last login session |
Quick way: cat /etc/passwd
Run the command cat /etc/passwd to list all accounts on the system, one per line in format username:x:UID:GID:comment:home:shell. This includes both accounts created by humans and all the system/service accounts (like www-data, sshd, nobody and many others).
cat /etc/passwdPlease note, that it is the full list of accounts, although seldom corresponding to the subset of interest—human users—that we talk about in further sections.
More portable way: getent passwd
Whereas cat /etc/passwd lists only the accounts stored in the local file, accounts that are authenticated through LDAP, NIS or any other directory service won't be listed here. The command getent passwd queries all the sources specified in /etc/nsswitch.conf (local file, LDAP, NIS and etc.) and outputs the result in the same format:
getent passwdIf you're not sure whether a server uses central authentication such as LDAP, use getent passwd instead. With no directory service running, its output is the same as /etc/passwd; with one, the directory accounts are listed as well.
List only usernames
Both the commands below remove unnecessary fields and give usernames one per line:
cut -d: -f1 /etc/passwd
compgen -uExplanation: cut -d: -f1 extracts the first colon-separated field from /etc/passwd. compgen -u is a built-in Bash command that does exactly the same thing but doesn't require you to know the file format, very useful for script writers who don't want to parse /etc/passwd manually.
List only human users, system accounts excluded
Filter by UID to differentiate human users from services:
awk -F: '$3 >= 1000 {print $1}' /etc/passwdThe command shows users whose UID is 1000 or above. Nearly every current distribution treats 1000 as the starting point for regular accounts (Ubuntu, Debian, Fedora 16 and later, RHEL/CentOS 6 and later). Legacy systems such as RHEL 5 began at 500, so change the number when you're on one of those.
Check UID and groups of a particular user
After listing all the accounts and choosing a certain one, the command id gives all the information for one user in one line:
id usernameThe output contains UID, primary GID and all groups to verify if a permissions problem relates to the group membership.
List currently logged in users
cat /etc/passwd lists all the accounts on the system, while who gives the list of currently logged in users only:
whoEvery line shows the username, associated terminal or pseudo terminal and the time when the session was started. The w command gives more detailed information on the same list of sessions.
Look into user login history
The who command gives the information about current sessions. To see the information about previous sessions, including the ended ones, run:
lastThe command gives the history of logins and shows the start time, duration and terminal or remote address for each session. Thus, you can confirm the last login time rather than current.
One command to rule them all: lslogins
Most of the commands above solve the particular task. The command lslogins, which is a part of the util-linux package (comes preinstalled on almost all modern systems) solves the major of them in one query. Running lslogins without any parameters gives the list of all system accounts with UID, GID, home directory, shell and account status in one table.
lsloginsSome options make the output suitable for your particular purpose:
lslogins -u
lslogins -s
lslogins -LThe
-u(or--user-accs) option limits the output to regular user accounts.The
-s(or--system-accs) option shows system accounts; by default those with a UID between 101 and 999.The
-L(or--last) option adds the last login session for every account, thus combining information that usually requires separate invocation of thelastcommand. If you need to remember just one command of this group, thenlsloginsis the best choice.
These are among the first commands you run when you audit access on a server you took over, or before you hand one over to someone else. On OrbitServers VPS and bare-metal servers, a user with root privileges can run them from the very first login.
Get started with Orbit Servers
Low-latency VPS, bare metal, and colocation across the US, EU, and APAC - provisioned instantly and built for performance-critical workloads.
Get startedRelated products
Written by
Julius
Product Manager
Julius is a product-minded builder who cares about making technology simpler and more useful for the people who rely on it. He’s always looking for ways to improve things, one small step at a time, and enjoys turning good ideas into products that work.