Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

List only local user accounts with a single command

Writer 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
samsnow

Does 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/bash

And to get the user name only:

$ awk -F: '$NF!~/\/false$/ && $NF!~/\/nologin$/{print $1}' /etc/passwd
root
sync
terdon
git
bib
bob

If 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/bash

And, for the username only:

$ awk -F: '$6~/\/home/{print $1}' /etc/passwd
syslog
terdon
bib
bob

Finally, 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
bob

On 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/passwd

And 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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy