Get users with UID >= 1000 from /etc/passwd
I am trying to get usernames from /etc/passwd if UID equal/greater than 1000 using a for loop. The below simple bash script works but prints the username twice for some reason.
#!/bin/bash for userid in `awk -F: '' /etc/passwd` do if (("$userid" >= 1000)); then echo "Valid User" :`cat /etc/passwd | grep $userid | awk -F: ''` fi done
Why not just awk -F : ‘$3 >= «1000»
1 Answer 1
maybe you might have the user IDs like 1000 and 10001. So your grep leads to duplicate while iterating in for loop.
echo "Valid User" :`cat /etc/passwd | grep ":$userid:" | awk -F: ''`
Thanks @msp9011 awk -F ‘:’ ‘$3>=1000
@Kumar as mentioned in the answer , the duplicate happens becos grep is not having boundaries. if we use grep XX it will get you both XX and XXX. So you should have boundaries to iterate properly like grep «\bXX\b» or with a static string which I used in the answer as grep «:$userid:»
@msp9011 His comment gives the correct output as yours has a syntax error. By having 3 and not $3 before muru edited it, your answer prints the username and then a comma and the number 3 when he wants it to print the username and the userid.
You must log in to answer this question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529
Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
What does 1000 mean in chgrp and chown?
I am reading a blog to integrate EFK(a log system) into k8s in centos 7.4. There are following instructions:
# mkdir ~/es_data # chmod g+rwx es_data # chgrp 1000 es_data # chown 1000 -R es_data # ls -l /root/es_data/ total 8 drwxrwxr-x 2 1000 1000 4096 Jun 8 09:50 ./ drwx------ 8 root root 4096 Jun 8 09:50 ../
I log in as root. The instructions say, If I do chgrp 1000 es_data and chown 1000 -R es_data, the director’s owner and group would be 1000. But when I follow the instructions: I see following:
drwxr-xr-x. 2 master16g master16g 6 Jul 11 15:27 es_data
The owner and group appears to machine hostname, master16g . Could someone drop me hints what happens here for chgrp 1000 and chown 1000 ?
First of all, nobody other then you knows what that instruction is. however, i think this is about ES docker image. If you run official ES docker image — you’ll see, that elasticsearch UID is 1000 and elasticsearch GID is also 1000
1 Answer 1
chown changes the owner, chgrp changes the group. Because you have user and group both named master16g having 1000 as UID and GID respectively, this is why you see the user name and the group name on the list. chown accepts UID as parameter as well as username, this is well documented in the manual. chgrp also accepts GID and group name. You can change both also with one command chown 1000:1000 es_data -R or chown master16g:master16g es_data -R .
First Linux user has usually UID/GID 1000.
For instance, if you chown 0:1000 file you will see root:master16g as the file owner.
You can get the details of the elasticsearch user by running id elasticsearch .
But what’s the purpose why instruction ask us to change owner and group both to 1000, or first linux user? But the screen show numerical 1000.
Those are Linux basics. Without changing the permissions your user won’t be available to do any changes on the given directory or underlying files. Not sure why did you put those file on /root though.
EFK is supposed to run as user elasticsearch, and normally user elasticsearch has uid/gid of 1000/1000, that’s why the instruction. But in your container another user, master16g , occupied this id, that confused you.
Linux uid уже больше 1000
Answers & Comments
Потому что система резервирует определённый диапазон под системные службы. И чтобы не возникало конфликта, пользователям также выделяется свой диапазон значений.
uid’ы необязательно задаются значениями больше 1000. Всё зависит от дистрибутива. Но в большинстве сборок они действительно начинаются либо с 500, либо с 1000