How to List All User Accounts on Ubuntu 16.04 | 18.04

You can easily find commands to add new user, delete user in the linux system, but you cannot easily get list of all users in linux system. If you are using desktop gui then you can easily see all user accounts as an administrator. Although there could be multiple ways to find all user accounts on a linux system. Through this post we will let you know how to list all user accounts on a ubuntu or any linux system.

1. List local users

You can list local user by viewing content of /etc/passwd file. The file keeps local user information like username, account id. Each line in this file represent a login information of one user account. You can use less or cat command to see contents of this file.

$ sudo less /etc/passwd

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
lxd:x:105:65534::/var/lib/lxd/:/bin/false
uuidd:x:106:110::/run/uuidd:/usr/sbin/nologin
dnsmasq:x:107:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologin
landscape:x:108:112::/var/lib/landscape:/usr/sbin/nologin
sshd:x:109:65534::/run/sshd:/usr/sbin/nologin
pollinate:x:110:1::/var/cache/pollinate:/bin/false
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
mysql:x:111:115:MySQL Server,,,:/nonexistent:/bin/false

user accounts linux
Each line in this file consists of 7 fields delimited by colon(:). We are listing below fields in the order as there in each line.

  • Username
  • Encrypted Password (X means password is encrypted and stored in /etc/shadow file.
  • User ID (UID)
  • Group ID (GID)
  • User fullname (GECOS)
  • User home directory
  • Login shell (default /bin/bash)

You can print or display any particular field through awk or cut commands. Let’s say to print only the first field i.e username:

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

OR

$ cut -d: -f1 /etc/passwd

usernames linux

2. List users through Getent tool

Getent is a multi purpose tool to search info in database files under /etc directory. This command is also used to list the linux users. Getent also displays user list from /etc/passwd file through database stored in /etc/nsswitch.conf file.

$ sudo getent passwd

This will output below content, same as first command.

Now, as we know how to list all the users in ubuntu or any linux system, lets see how to list all the users belong to a particular group. User’s group info is stored in /etc/group file, so for this we can use getent comamnd to query /etc/group file.

Usage: getent group groupname

For example:

$ sudo getent group sudo

Output:


sudo:x:27:ubuntu
Help us Grow

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code