How to count local user accounts

You may be aware of the whole number of accounts registered in the system, but if you are wondering how to count only local users ignoring system accounts, then 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 simple shell script to be more descriptive and include system specific configuration.

# Count and print local user accounts

# configuration file and min/max uid values
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

# password file

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 }' \

Sample script output.

UID     Username
1000    milosz
1001    administrator
Total: 2


Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system. He is also open for new opportunities and challenges.

Gdansk, Poland