If you are wondering how to count only local users ignoring system accounts, I will give you a concise and straight answer.
One-liner awk command
You can use the following awk
command to quickly count local users.
$ awk -F: '$3 >= 1000 && !($1 == "nobody" && $3 == 65534) { SUM+=1 } END { print SUM }' /etc/passwd
It will get the number of local user accounts by counting users with UID greater or equal to 1000 and ignoring special
nobody
account.Shell script
The above idea can be extended using a simple shell script to be more descriptive and include system specific configuration.
#!bin/sh # Count and print local user accounts # configuration file and min/max uid values SHADOW_CONFIG="/etc/login.defs" UID_MIN=$(awk '$1 == "UID_MIN" {print $2}' $SHADOW_CONFIG) UID_MAX=$(awk '$1 == "UID_MAX" {print $2}' $SHADOW_CONFIG) # "nobody" user to be ignored NOBODY_USR="nobody" NOBODY_UID=65534 # password file PASSWD_FILE="/etc/passwd" awk -F: '($3 >= '$UID_MIN' && $3 <= '$UID_MAX') && \ !($1 == '$NOBODY_USR' && $3 == '$NOBODY_UID') \ { print $3 "\t" $1; SUM+=1 } \ BEGIN { print "UID\tUsername"; SUM=0 } \ END { print "Total: " SUM }' \ $PASSWD_FILE
Sample script output.
UID Username 1000 milosz 1001 administrator Total: 2
References
- The GNU Awk User’s Guide
passwd
manual page (section 5)login.defs
manual page (section 5)