List only local user accounts with a single command
Andrew Henderson
I am using Ubuntu 18.04 and I need to list all the user accounts on the computer but not all of the accounts, only the local users.
the command cut -d: -f1 /etc/passwd gives:
root
daemon
bin
sys
sync
games
...
pulse
avahi
colord
hplip
geoclue
gnome-initial-setup
gdm
esnow
stuartsnow
samsnowDoes anyone know of a command that would output only the local users, for example:
root
esnow
stuartsnow
samsnow 2 1 Answer
If you want the list of users that can actually log into the machine, look for the users whose login shell (the last field in /etc/passwd) is not set to /bin/false or /sbin/nologin:
$ awk -F: '$NF!~/\/false$/ && $NF!~/\/nologin$/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:4:65534:sync:/bin:/bin/sync
terdon:x:1000:1000::/home/terdon:/bin/bash
git:x:996:996:git daemon user:/:/bin/bash
bib:x:1001:1001::/home/bib:/bin/bash
bob:x:1002:1002::/home/bob:/bin/bashAnd to get the user name only:
$ awk -F: '$NF!~/\/false$/ && $NF!~/\/nologin$/{print $1}' /etc/passwd
root
sync
terdon
git
bib
bobIf you want only "normal" users, those who were created using the standard approach and therefore will have a home directory under /home, use:
$ awk -F: '$6~/\/home/' /etc/passwd
syslog:x:101:104::/home/syslog:/bin/false
terdon:x:1000:1000::/home/terdon:/bin/bash
bib:x:1001:1001::/home/bib:/bin/bash
bob:x:1002:1002::/home/bob:/bin/bashAnd, for the username only:
$ awk -F: '$6~/\/home/{print $1}' /etc/passwd
syslog
terdon
bib
bobFinally, you can combine the two to get all users with a home in /home and a valid login shell:
$ awk -F: '$NF!~/\/false$/ && $NF!~/\/nologin$/ && $6~/\/home/{print $1}' /etc/passwd
terdon
bib
bobOn a sane Ubuntu system, all of the non-system users should have a user ID between 1000 and 29999. So, assuming you haven't created any user with a lower UID, you could do:
awk -F: '$3>999 && $3<30000{print $1}' /etc/passwdAnd you could combine everything again (some services have regular user IDs but don't have a login shell, jira on my work server, for instance):
awk -F: '$3>999 && $3<30000 && $NF!~/\/false$/ && $NF!~/\/nologin$/ && $6~/\/home/{print $1}' /etc/passwd 7