Linux: List All Users

Another very common request, both form my Unix/Linux beginner users and from the visitors to Unix Tutorial blog. Usually, user list is needed because you plan on doing something with it – so please leave a comment and let me know what it is. Who knows, there might be a quicker and easier way of doing the same!

List all users with getent

This is probably the quicked and easiest way of getting the list of users in your Linux system, along with most relevant info about each of them:

greys@ec2 ~]$ 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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:997:995:User for polkitd:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:996:993::/var/lib/chrony:/sbin/nologin
centos:x:1000:1000:Cloud User:/home/centos:/bin/bash

IMPORTANT: if your Linux system is part of an AD or LDAP infrastructure, the getent passwd command will get you all the users in AD, rather than just those locally created on your Linux server.

List all users from /etc/passwd

You can also just look at the contents of the /etc/passwd file: it will look very similar to the getent output:

[greys@ec-ws1 ~]$ 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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:997:995:User for polkitd:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:996:993::/var/lib/chrony:/sbin/nologin
centos:x:1000:1000:Cloud User:/home/centos:/bin/bash

Extract usernames from passwd with awk

All these lists are fine, but they’re not easily actionable in scripts or any other command line processing in Unix. The reason for this is, of course, because we’re getting too much information: instead of just the list of usernames, we’re looking at lots of passwd fileds like full name, user id, group if, user shell and so on.

So the next step is probably extracting usernames from the output we received. Here’s how we can do it: we’ll use the awk field separator to split fields.

Here’s the result:

[greys@ec-ws1 ~]$ cat /etc/passwd | awk -F: '{print $1}'
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
avahi-autoipd
systemd-bus-proxy
systemd-network
dbus
polkitd
rpc
tss
rpcuser
nfsnobody
postfix
sshd
chrony
centos


That’s it for today! Stay tuned for more!

See Also




How to identify what Unix groups are available on your system

Today, I’d like to answer one of the oldest questions I have in my incoming UnixTutorial questions  email folder. Please leave comments if you need any more help with researching Unix groups on your system.

How to confirm what Unix groups are available

If you remember, a while ago I’ve introduced you to the getent command. It’s a great way of querying various information databases about your systems’ users, groups and some other objects. Here’s how you would use the command to get a full list of Unix groups known to your system:

ubuntu# getent group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
uucp:x:10:
man:x:12:
proxy:x:13:
kmem:x:15:
dialout:x:20:
fax:x:21:
...

This is an abridged output, but I hope you get the idea. This output helps you confirm the following:

  1. Unix group name – first field
  2. Unix group ID (gid) – third field
  3. All the usernames of members for various groups – fourh field, unfortunatelly empty for all the groups in my example.

How to confirm the members of a Unix group

Using the same getent command, you can query the groups database using a group name. In my example below, I’m confirming the membership of a mygroup Unix group:

ubuntu# getent group mygroup
mygroup:x:1002:user1,greys,user2
As you can see, mygroup has 3 users: user1, greys and user2.

How to determine the number of Unix groups known to your system

One more thing you can learn about your Unix groups using getent command is to confirm the overall number of Unix groups – some scenarious require you to have this number. Here’s how you would use getent together with the wc command to confirm the number of groups:

ubuntu# getent group | wc -l
62
That’s it for today, let me know if there’s anything else you’d like to learn about this topic!

See also: