user2436428
user2436428

Reputation: 1723

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?

Upvotes: 13

Views: 19278

Answers (6)

Nick ODell
Nick ODell

Reputation: 25210

You'll want to ignore GIDs less than 1000, but also GIDs greater than 60000. Ubuntu/Debian reserve these for various system services.

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

Upvotes: 1

Excalibur
Excalibur

Reputation: 3367

Here's an answer for doing this on all your machines, using Ansible and awk, building on JNevill's answer:

ansible -i inventories/cd_staging all -m shell -a "awk -F: '\$3 >= 1000 && \$7 \!~ /nologin/ {print \$1}' \/etc\/passwd |sort"

Upvotes: 0

Thomas Dickey
Thomas Dickey

Reputation: 54505

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'

Upvotes: 1

John Bollinger
John Bollinger

Reputation: 180141

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

Upvotes: 1

Diogo Rocha
Diogo Rocha

Reputation: 10575

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

Upvotes: 21

JNevill
JNevill

Reputation: 50034

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.

Upvotes: 7

Related Questions