Saturday, May 4, 2024
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 1231  / 1 Year ago, sat, april 22, 2023, 1:19:49

We all know that we have system users and service users. I am looking for a way to separate them as system users and service user list.



Is there any way ?


More From » user-management

 Answers
0

Based on gid, system users and service users can be separated as follows,



/etc/passwd contains list for all users along with some other information. Service users or real users have gid greater than or equals to 1000. So a list of real users can be obtained as,



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


Also a list of system users (gid < 1000) can be extracted as,



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


How it works



The contents of /etc/passwd are like,



    root:x:0:0:root:/root:/bin/bash
...
souravc:x:1001:1001:Souravc:/home/souravc:/bin/bash


When using awk with -F: it splits the contents of a line into several fields treating : as field separator. First field contains the user name and third field has the gid.



Hence to extract real users awk just check value of third field is greater than equals to 1000 and it is not nobody user and prints the first field i.e., the user name.



To list all system users it just checks gid is less than 1000 and prints the user name.



Edit



As you want to list root(gid = 0) in real user list. Get real users as,



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


Get system users as,



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


Note I am always ignoring nobody user.


[#27026] Sunday, April 23, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ingwhin

Total Points: 332
Total Questions: 112
Total Answers: 115

Location: Burkina Faso
Member since Tue, Apr 26, 2022
2 Years ago
;