Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 ?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Thank you for answering –  AgentCool Feb 15 at 6:55
    
How it can take care of root user ? –  AgentCool Feb 18 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? –  souravc Feb 18 at 18:27
    
yes because he is also user. –  AgentCool Feb 19 at 3:43
    
@rajagenupula is it ok now? –  souravc Feb 20 at 16:20

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
share|improve this answer
1  
Root user's group ID and user's ID is less than 1000 right.thank you for answer –  AgentCool Feb 13 at 18:29
    
@rajagenupula yes for sure. only root is the exception. and in ubuntu generally, root cannot login by default. –  kamil Feb 13 at 18:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.