Script to Copy User according to UID - linux

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

Related

unable to redirect data by using awk or cat

I am using AIX for following code:
#!/bin/sh
cat ip.txt | awk -F ' ' '{print $2,$1}' >op.txt
or
awk -F ' ' '{print $2,$1}' ip.txt > op2.txt
It is generating an unknown file named "oxb1du".
Aslo, I can see file op2.txt after ls -ltr but it does not contain any data.
I/P file:
name 1
info 21
city 28
pin 31
state 34
Maybe you are looking for:
cat ip.txt | awk '{print $2,$1}' > op.txt
You probably have binary characters in your file. Try cleaning it first.
tr -cd '[:graph:]\n\t ' <"$file" >$TEMP_FILE && mv $TEMP_FILE "$file"
dos2unix and other programs may work, but I've had issues with dos2unix only removing carriage returns, and not other garbage so I've given you the above (obviously assign or replace the variables). Then just use:
awk -F" " '{print $2,$1}' ip.txt > op2.txt
I only changed the quotes for readability-- having them hanging away from the -F, and before other single quotes looks wonky. This way is quicker to read.

List all Linux users without systen users

I would like to list all users in Linux without showing systen-user.
How can I make this only the username .
For example cut -d: -f1 /etc/passwd, I can see all users + system users.
This shows all users with uid less than 999:
awk -F':' '$3>999 {print $1 " uid: " $3}' /etc/passwd | column -t | grep -v nobody
EDIT:
With cut showing only human users:
cut -d: -f1,3 /etc/passwd | egrep ':[0-9]{4}$' | cut -d: -f1
You can try this : awk -F: '$6 ~ /\/home/ {print}' /etc/passwd

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?
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

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

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.

Resources