Separate system users and service users
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 ?
Solution 1:
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.
Solution 2:
To list local (system users) than can login and have homedir and GID less than 1000
cat /etc/passwd | cut -d: -f 1,3,6 | grep "[1-9][0-9][0-9][0-9]" | grep "/home" | cut -d: -f1
To list all other users:(mainly system users and have GID less than 1000):
cat /etc/passwd | cut -d: -f 1,3,6 | grep -v "[1-9][0-9][0-9][0-9]" | cut -d: -f1