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

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

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

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.

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

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'

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"

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

Related

Linux Command to list all users by uid

I am using RedHat Linux 6.
I need redhat Linux terminal code to list all the users above uid=499?
i already tried "cat /etc/passwd". but it shows all users. how do i filter it?
You can use awk to parse the passwd database for the UIDs you want.
To list all the users for UIDs strictly greater than 499, do this:
awk -F ':' '$3 > 499' /etc/passwd
EDIT: If you only want the usernames, do this:
getent passwd | awk -F: '$3 > 499 {print $1}'
you can use in your terminal to find all user
cut -d: -f1 /etc/passwd
hope this code help you thanx

Linux: How can I display all linux users that have an UID between 300 and 500?

I want to display all users that its UID between 300 and 500.
I tried the grep command but I can't get the result that I need.
I tried this syntax, but it does not work:
cat /etc/passwd | grep *:[300-500]
Using awk, here is your answer:
awk -F: '$3 < 500 && $3 > 300 { print $0 }' /etc/passwd
You can print $1 if you just want the username.
egrep 'x:3[0-9][0-9]:|x:4[0-9][0-9]:|x:500:' /etc/passwd
or more eloquent
egrep 'x:[3-4][0-9][0-9]:|x:500:' /etc/passwd

cat passwd | awk -F':' '{printf $1}' Is this command correct?

I'd like to know how cat passwd | awk -F':' '{printf $1}' works. cat /etc/passwd is a list of users with ID and folders from root to the current user (I don't know if it has something to do with cat passwd). -F is some kind of input file and {printf $1} is printing the first column. That's what I've search so far but seems confusing to me.
Can anyone help me or explain to me if it's right or wrong, please?
This is equivalent to awk -F: '{print $1}' passwd. The cat command is superfluous as all it does is read a file.
The -F option determines the field separator for awk. The quotes around the colon are also superfluous since colon is not special to the shell in this context. The print invocation tells awk to print the first field using $1. You are not passing a format string, so you probably mean print instead of printf.

I need a grep command that will pull the usernames only from the /etc/passwd file in linux

I need a grep or another like command that will pull the usernames only from the /etc/passwd file in linux. Anything before the colon. I know this is doing with reg ex however I am not nearly experienced enough...
The following command will give all ACTUAL users, I need a way to pipe to grep or another line of code to only display the username portion.
awk -v LIMIT=500 -F: '{print $1}' '($3>=LIMIT) && ($3!=65534)' /etc/passwd
This should do:
awk -F: '$3>=LIMIT && $3!=65534 {print $1}' LIMIT=500 /etc/passwd
To do this in mostly plain bash:
limit=500
nfsnobody_id=65534
cut -d: -f1,3 /etc/passwd | while IFS=: read username uid; do
(( uid >= limit && uid != nfsnobody_id )) && echo $username
done
Get out of the habit of using VARNAMES_IN_CAPS: one day you'll write PATH=$(dirname $FILE) and then wonder why commands can no longer be found.
You simply need to rewrite the filter part of your awk command a bit. For instance, the following should work:
awk -v LIMIT=500 -F: '{if (($3>=LIMIT) && ($3!=65534)) print $1}' /etc/passwd
Otherwise, to answer your question strictly, if you want to use grep, the command would be
... | grep -o "^[^:]*"
but that would not be the way to go.

Script to Copy User according to UID

I'm looking for a way to copy all non-system users from one PC to another. I can get the group and passwd files copied over using this
awk -F":" ' $3 > 499 ' etc/passwd >> /etc/passwd
awk -F":" ' $3 > 499 ' etc/group >> /etc/group
But, how would I go about getting the shadow file copied over since it does not store the UID? Assume that there are over 1000 users, so doing a grep with the usernames, such as egrep '(bob|bill|sarah|sal):' etc/shadow >> /etc/shadow generating the usernames from the awk code above, would be a bit inefficient, but a possible option.
awk -F":" ' $3 > 499 {print "^"$1":"} ' /etc/passwd | sudo grep -f - /etc/shadow > shadow.out
Previous answer could produce multiple lines per user if username is part of other usernames
awk -F":" ' $3 > 499 {print $1} ' /etc/passwd | sudo grep -f - /etc/shadow > shadow.out

Resources