getent passwdlists every account, including any from LDAP or another directory service that /etc/passwd will not show.- Human accounts almost always have a user ID (UID) of 1000 or above. Below that are system accounts.
whoshows who is logged in now;lastshows who has logged in recently.- An account with the shell /usr/sbin/nologin cannot log in interactively. Service accounts should look like that.
- The accounts worth auditing are the ones that can log in and use sudo, not the long list of system users.
How do I list all users on a Linux system?
Run getent passwd. It lists every account the system knows about. On a standalone server that is the contents of /etc/passwd. On a server joined to a directory service (such as LDAP, a central list of company logins), it includes those accounts too, which reading /etc/passwd directly would miss.
# Every account, full detail
getent passwd
# Just the usernames
getent passwd | cut -d: -f1
# Sorted, one per line
getent passwd | cut -d: -f1 | sort
# How many accounts exist in total
getent passwd | wc -l
# Does one specific account exist?
getent passwd alicegetent passwd alice exits 0 if the account exists and 2 if it does not, so a script can test it without parsing the output.$ getent passwd deploy
deploy:x:1001:1001:Deploy user:/home/deploy:/bin/bash
| | | | | | |
| | | | | | +-- login shell
| | | | | +------------ home directory
| | | | +-------------------------- description
| | | +------------------------------------ primary group ID
| | +----------------------------------------- user ID (UID)
| +-------------------------------------------- password placeholder
+------------------------------------------------- usernameHow do I list only human users, not system accounts?
A fresh server has 30 to 40 accounts, and none of them are people. Each service gets its own account so it can run without root. By convention, system accounts have a UID (the account's number) below 1000 and human accounts start at 1000. Filtering on UID gives you the people.
# Accounts with UID 1000 or above: the humans
getent passwd | awk -F: '$3 >= 1000 && $3 < 65534 {print $1}'
# With UID and home directory, formatted
getent passwd | awk -F: '$3 >= 1000 && $3 < 65534 {printf "%-16s %-6s %s\n", $1, $3, $6}'
# The more useful question: who can actually log in?
getent passwd | grep -vE '(nologin|/bin/false)$' | cut -d: -f1,3,7nobody account. It sits at the top of the range and is not a person either.| UID range | What lives there |
|---|---|
| 0 | root |
| 1 to 999 | System and service accounts: www-data, sshd, mysql |
| 1000 and up | Human accounts, created in order |
| 65534 | nobody, the unprivileged fallback |
On older RHEL systems the human range starts at 500, not 1000. Check UID_MIN in /etc/login.defs if it matters.
The shell field tells you more than the UID
An account with /usr/sbin/nologin or /bin/false as its shell cannot start an interactive session, whatever its UID. That is how a service account should look. A system account with /bin/bash is worth a second look, because it does not need one.
How do I see who is logged in right now?
# Who is logged in, from where, since when
who
# Same, plus what each session is running and system load
w
# Just the usernames, deduplicated
users
# Your own effective username
whoami
# Full identity: UID, primary group, every secondary group
idw first. It shows idle time and the current command, so you can tell an active session from one left open on Friday.# Recent logins, most recent first
last
# One user's history
last alice
# Failed login attempts (needs root)
sudo lastb | head -30
# When did each account last log in at all?
lastlog
# Accounts that have never logged in
lastlog | grep 'Never logged in'lastlog covers every account in one table, which makes it the fastest way to find dormant accounts.Thousands of failed logins is normal, and still worth reading
Any server with SSH open to the internet sees constant automated password guessing. You will see it in the authentication logs too. The volume does not matter. What matters is whether any attempt succeeded, and whether they targeted a username that exists. Turn off password logins for SSH and the problem goes away.
How do I audit which users have root access?
This is the list that matters: who can become root. Most of that access comes through group membership, so adding and removing users from groups is the other half of this job. The sudo group is one route. There are three more, and two are easy to miss.
# 1. Members of the sudo group (Debian/Ubuntu) or wheel (RHEL family)
getent group sudo
getent group wheel
# 2. Rules granted directly in sudoers, including drop-in files
sudo grep -rE '^[^#]*ALL' /etc/sudoers /etc/sudoers.d/
# 3. Any account with UID 0. There should be exactly one: root.
awk -F: '$3 == 0 {print $1}' /etc/passwd
# 4. Members of the docker group, which is root in practice
getent group docker
# What a specific user can run as root
sudo -lU alice| Check | Command | What you want to see |
|---|---|---|
| Accounts with UID 0 | awk -F: '$3 == 0' /etc/passwd | Only root |
| Sudo group members | getent group sudo | Named people you recognise |
| Passwordless sudo | grep NOPASSWD /etc/sudoers.d/* | Automation accounts only |
| Docker group | getent group docker | Treat every member as root |
| Accounts with no password set | sudo awk -F: '$2 == ""' /etc/shadow | Nothing at all |
Run these on any server you inherit, before you change anything else on it.
Dormant accounts are the ones that get compromised, because nobody notices the login. lastlog | grep 'Never logged in' finds accounts that were created and forgotten. Lock them with sudo usermod -L username rather than deleting them, so their files keep a recognisable owner.
How do I find out what a user can access?
# Identity and every group
id alice
# What sudo rights they have
sudo -lU alice
# Account status: L locked, P has a password, NP no password
sudo passwd -S alice
# Password and account expiry
sudo chage -l alice
# Files they own outside their own home directory
sudo find / -user alice -not -path '/home/alice/*' -ls 2>/dev/null | head -30
# Their authorised SSH keys
sudo cat /home/alice/.ssh/authorized_keysCheck authorized_keys on every account, not just theirs
A leaver's public key may also sit in a shared deploy account. sudo grep -rl 'their-key-comment' /home/*/.ssh/authorized_keys /root/.ssh/authorized_keys finds every copy. Removing the user account alone does not.
Once you know who exists and what they can reach, look at what the file permissions allow. Linux users, groups and file permissions covers that end to end. If you would rather not manage server accounts at all, our managed WordPress hosting looks after the operating system for you.
Frequently asked questions
What is the command to list all users in Linux?
getent passwd lists every account the system knows about, including any from LDAP or another directory service. For just the names, run getent passwd | cut -d: -f1. Reading /etc/passwd directly works on a standalone server but misses accounts from a directory service.
How do I list only human accounts and not system accounts?
Filter by user ID: getent passwd | awk -F: '$3 >= 1000 && $3 < 65534 {print $1}'. By convention, accounts numbered below 1000 belong to services and accounts from 1000 upward belong to people. Some older RHEL systems start the human range at 500.
What is the difference between who, w and last?
who lists the sessions open now. w adds idle time, the current command and system load. last reads the login history in /var/log/wtmp and shows sessions that have ended, including reboots.
How can I tell which users have sudo access?
Start with getent group sudo on Debian and Ubuntu, or getent group wheel on the RHEL family. Then check for direct grants with sudo grep -rE '^[^#]*ALL' /etc/sudoers /etc/sudoers.d/, and confirm no second account has UID 0. sudo -lU username shows what one user can run.
How do I see when a user last logged in?
lastlog prints the most recent login for every account in one table, with "Never logged in" for dormant ones. For one account's full history, last username lists each session with its start time and length.
About G7Cloud Engineering
Articles written by the engineers who build and run G7Cloud: UK managed hosting and the AI Website Builder. We write about what we operate every day: containers, backups, databases, and the small-business websites that run on them.
More about G7Cloud →