How can we get list of non-system users on linux?

How can we get list of non-system users on linux?

Considering that all users with id >= 1000 are non-system users, how can we get list of these users in a single command?

Solutions/Answers:

Solution 1:

You need to get all users whose gid is greater than or equals 1000. Use this command for that:

awk -F: '($3>=1000)&&($1!="nobody"){print $1}' /etc/passwd

If you want system users (gid<1000) it will be:

awk -F: '($3<1000){print $1}' /etc/passwd

Solution 2:

You can use awk for this task:

awk -F: '$3 >= 1000' /etc/passwd

This will split the /etc/passwd file by colon, then if field 3 (userid) is greater than or equal to 1000, it will print the entire /etc/passwd record.

If you want to get only the username out of this list then:

awk -F: '$3 >= 1000 {print $1}' /etc/passwd

Where $1 is the first field of etc/passwd which is the username.

Solution 3:

Supposing that the system recognizes only local users (i.e. those recorded in /etc/passwd, as opposed to any authenticated via a remote service such as LDAP, NIS, or Winbind), you can use grep, sed, or awk to extract the data from /etc/passwd. awk is the most flexible of those, but how about a solution with sed:

sed -n '/^\([^:]\+\):[^:]\+:[1-9][0-9]\{3\}/ { s/:.*//; p }' /etc/passwd

Solution 4:

System users (should be) those listed in /etc/passwd with UIDs less than 1000. The actual number is a convention only. Non-system users need not be listed there. You can get the list using getent and awk ignoring “nobody” (also a convention):

getent passwd |awk -F : '$3 >= 1000 && $3 < 65534'

References

Related:  Bash - Sed / Awk replacing string with new line