8

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 ?

3 Answers 3

7

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.

6
  • How it can take care of root user ?
    – Raja G
    Feb 18, 2014 at 18:16
  • did not understand your point? you will get root as system user. do you wish to include root in real user list?
    – sourav c.
    Feb 18, 2014 at 18:27
  • yes because he is also user.
    – Raja G
    Feb 19, 2014 at 3:43
  • @rajagenupula is it ok now?
    – sourav c.
    Feb 20, 2014 at 16:20
  • I would like to have root user in real user list @S
    – Raja G
    Feb 20, 2014 at 16:55
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
2
  • 1
    Root user's group ID and user's ID is less than 1000 right.thank you for answer
    – Raja G
    Feb 13, 2014 at 18:29
  • @rajagenupula yes for sure. only root is the exception. and in ubuntu generally, root cannot login by default.
    – kamil
    Feb 13, 2014 at 18:30
0

Actual users (other than root) on a nix system should have a home directory identified. So, to find all of those users and root:

cat /etc/passwd | egrep '/root|/home' | cut -d":" -f1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .