Find users with root UID or GID or root home


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find users with root UID or GID or root home
# 1  
Old 12-04-2013
Find users with root UID or GID or root home

I need to list users in /etc/passwd with root's GID or UID or /root as home directory
If we have these entries in /etc/passwd
Code:
root:x:0:0:root:/root:/bin/bash
rootgooduser1:x:100:100::/home/gooduser1:/bin/bash
baduser1:x:0:300::/home/baduser1:/bin/bash
baduser2:x:400:0::/home/baduser2:/bin/bash
baduser3:x:500:500:root:root:/bin/bash
baduser4:x:0:0::/home/baduser4:/bin/bash
gooduser2:x:600:600::/home/gooduser1:/bin/bash

The sed/awk operation should list out only badusers, as they hold either, roots UID or GID or /root as home directory.
# 2  
Old 12-04-2013
So, The UID & GID for the root account are both zero. The fields are separated by : so you can:
Code:
grep ":0:" /etc/passwd

Does that give you a starter? Have a look at the cut manual page if you need to slice up the output to give you just the names.



I hope that this helps,

Robin
Liverpool/Blackburn
UK
# 3  
Old 12-04-2013
Thank you, but it should not list
root:x:0:0:root:/root:/bin/bash
and also, should list
baduser3:x:500:500:root:root:/bin/bash
as baduser3 has home directory set as /root
# 4  
Old 12-04-2013
You said you want to use sed or awk, cut (as suggested by rbatte1) is another possibility. I would guess that awk will be better for this job than sed.

You've been watching what we have been doing here for more than 1.5 years.
Can you show us how to use awk (or grep and cut) to get the UID, GID, and home directory for root out of /etc/passwd?

Once you have those values, can you show us how to use awk or cut to find other users with the same UID as root, the same GID as root, or the same home directory as root?

Show us that you have made an attempt to solve this problem by showing us what you have tried so far. Tell us what is working and what you can't get to work yet.
# 5  
Old 12-04-2013
Strictly, baduser3 has an invalid home directory. You can always extend the grep to include :/root: too. Have a read of the man page for that.

What have you tried so far with awk or sed?


Robin
# 6  
Old 12-04-2013
Thanks for the help guys. Was able to manage.

Code:
$ cat testpasswd
root:x:0:0:root:/root:/bin/bash
rootgooduser1:x:100:100::/home/gooduser1:/bin/bash
baduser1:x:0:300::/home/baduser1:/bin/bash
baduser2:x:400:0::/home/baduser2:/bin/bash
baduser3:x:500:500:root:root:/bin/bash
baduser4:x:0:0::/home/baduser4:/bin/bash
gooduser2:x:600:600::/home/gooduser1:/bin/bash
baduser5:x:0:0:root:/root:/bin/bash

Operation
Code:
$ cat testpasswd |  grep -E ':0:|:root:'  | grep -v  'root:x:0:0'
baduser1:x:0:300::/home/baduser1:/bin/bash
baduser2:x:400:0::/home/baduser2:/bin/bash
baduser3:x:500:500:root:root:/bin/bash
baduser4:x:0:0::/home/baduser4:/bin/bash
baduser5:x:0:0:root:/root:/bin/bash

---------- Post updated at 07:14 AM ---------- Previous update was at 07:00 AM ----------

Quote:
Originally Posted by Don Cragun
You've been watching what we have been doing here for more than 1.5 years.
Can you show us how to use awk (or grep and cut) to get the UID, GID, and home directory for root out of /etc/passwd?
I have attempted.

Code:
$ cat testpasswd | awk 'BEGIN{print "UID\tGID\tHome_Directory";} {FS=":"} {print $3 "\t" $4 "\t" $6}'
UID     GID     Home_Directory

100     100     /home/gooduser1
0       300     /home/baduser1
400     0       /home/baduser2
500     500     root
0       0       /home/baduser4
600     600     /home/gooduser1
0       0       /root

With cut
Code:
$ cat testpasswd | cut -d: -f 3,4,6
0:0:/root
100:100:/home/gooduser1
0:300:/home/baduser1
400:0:/home/baduser2
500:500:root
0:0:/home/baduser4
600:600:/home/gooduser1
0:0:/root

This User Gave Thanks to anil510 For This Post:
# 7  
Old 12-04-2013
Quote:
Originally Posted by anil510
Thanks for the help guys. Was able to manage.

Code:
$ cat testpasswd
root:x:0:0:root:/root:/bin/bash
rootgooduser1:x:100:100::/home/gooduser1:/bin/bash
baduser1:x:0:300::/home/baduser1:/bin/bash
baduser2:x:400:0::/home/baduser2:/bin/bash
baduser3:x:500:500:root:root:/bin/bash
baduser4:x:0:0::/home/baduser4:/bin/bash
gooduser2:x:600:600::/home/gooduser1:/bin/bash
baduser5:x:0:0:root:/root:/bin/bash

Operation
Code:
$ cat testpasswd |  grep -E ':0:|:root:'  | grep -v  'root:x:0:0'
baduser1:x:0:300::/home/baduser1:/bin/bash
baduser2:x:400:0::/home/baduser2:/bin/bash
baduser3:x:500:500:root:root:/bin/bash
baduser4:x:0:0::/home/baduser4:/bin/bash
baduser5:x:0:0:root:/root:/bin/bash

---------- Post updated at 07:14 AM ---------- Previous update was at 07:00 AM ----------



I have attempted.

Code:
$ cat testpasswd | awk 'BEGIN{print "UID\tGID\tHome_Directory";} {FS=":"} {print $3 "\t" $4 "\t" $6}'
UID     GID     Home_Directory

100     100     /home/gooduser1
0       300     /home/baduser1
400     0       /home/baduser2
500     500     root
0       0       /home/baduser4
600     600     /home/gooduser1
0       0       /root

With cut
Code:
$ cat testpasswd | cut -d: -f 3,4,6
0:0:/root
100:100:/home/gooduser1
0:300:/home/baduser1
400:0:/home/baduser2
500:500:root
0:0:/home/baduser4
600:600:/home/gooduser1
0:0:/root

Good. You're learning how to use the tools.

Note that your output doesn't include the login names of the users whose entries are failing your tests. You might want to add that to your output.

If you'd like to get root's UID, GID, and login directory from the password file instead of assuming that they will be 0, 0, and /root, respectively, (and you're willing to live with the limitation that root's information must appear on the 1st line in the passwd file), you could consider something like:
Code:
#!/bin/ksh
awk -F: '
NR == 1 {if($1 != "root") {
                printf("root expected on first line in %s; found %s instead.\n",
                        FILENAME, $1)
                exit 1
        }
        rootuid = $3
        rootgid = $4
        roothd = $6
        next
}
$3 == rootuid || $4 == rootgid || $6 == roothd { print }' "${1:-testpasswd}"

which produces the output:
Code:
baduser1:x:0:300::/home/baduser1:/bin/bash
baduser2:x:400:0::/home/baduser2:/bin/bash
baduser4:x:0:0::/home/baduser4:/bin/bash

Note also that baduser3's login directory (root) is not identical to root's login directory (/root), so I'm not sure if that line should be included in your output or not. (However, all login directories in /etc/passwd should have absolute pathnames. Should you add a check to verify that each user's login directory starts with a /?)
If you don't want to print the entire line from the passwd file for lines that fail your tests, you've already shown that you know how to print just the fields you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Display info about users (UID GID processes terminal)

I would like to get an opinion for my solution for this task and get feedback about better approach or mistakes I have made. 1. The problem statement, all variables and given/known data: The task is to create a script which prints information about users whose names are specified in the... (2 Replies)
Discussion started by: kornfan
2 Replies

2. AIX

Equivalent uid to root

Hi all I have a strange problem on one my my AIX machines. We have created a user called testroot with the same UID as root (uid=0) by changing the uid of that user in the /etc/passwd file. I know that this is a security breach but this is a test system. Now the strange thing that happens is that... (3 Replies)
Discussion started by: abohmeed
3 Replies

3. Solaris

New root account with Different UID number

Hi Unix Gurus . I have requirement where in which - I would like create duplicate root equivalent account with all the privileges equal to root. Is it possible to create this duplicate account with different UID. ? this id i would like give it to my teams - who does multiple activities using... (2 Replies)
Discussion started by: johnavery50
2 Replies

4. Solaris

Migration of system having UFS root FS with zones root to ZFS root FS

Hi All After downloading ZFS documentation from oracle site, I am able to successfully migrate UFS root FS without zones to ZFS root FS. But in case of UFS root file system with zones , I am successfully able to migrate global zone to zfs root file system but zone are still in UFS root file... (2 Replies)
Discussion started by: sb200
2 Replies

5. UNIX for Dummies Questions & Answers

uid ,gid value change

Present /home/dsadm# id dsadm uid=0(root) gid=0(root) ---------------------------------- needs to be /home/dsadm> id dsadm uid=23186(dsadm) gid=16284(gdstage) Please provide the command/steps for the above uid, gid value change Thanks in advance for all your support . ... (3 Replies)
Discussion started by: sridhardwh
3 Replies

6. Solaris

what impact root gid changed from other to root is?

> id root 0(root) 1(other) From CIS scanning result"it should make sure the root's gid is equal to 0", so I don't know what's the impact for that change to whole system? BTW, why is there a group named other under solaris? what does group "other" do ? Thanks very much! (3 Replies)
Discussion started by: a2156z
3 Replies

7. UNIX for Dummies Questions & Answers

Is there a way to find users who have sudo permissions for non root?

I want to check if in a host a set of persons have sudo access or not and I dont have root access to the host. (1 Reply)
Discussion started by: pristine
1 Replies

8. AIX

Can't login root account due to can't find root shell

Hi, yesterday, I changed root's shell in /etc/passwd, cause a mistake then I can not log in root account (can't find correct shell). I attempted to log in single-mode, however, it prompted for single-mode's password then I type root's password but still can not log in. I'm using AIX 5L version 5.2... (2 Replies)
Discussion started by: neikel
2 Replies

9. UNIX for Advanced & Expert Users

Setuid Program with (-rwsr-sr-x 1 root other ) UID/EUID issue

Hi, I have a program with the following suid setup -rwsr-sr-x 1 root other 653 Aug 16 17:00 restart_server It basically starts up a service that has to be started by root. I just want the normal users to be able to restart the service using the script above. But when the... (7 Replies)
Discussion started by: 0ktalmagik
7 Replies

10. UNIX for Dummies Questions & Answers

Run non-root script as root with non-root environment

All, I want to run a non-root script as the root user with non-root environment variables with crontab. The non-root user would have environment variables for database access such as Oracle or Sybase. The root user does not have the Oracle or Sybase enviroment variables. I thought you could do... (2 Replies)
Discussion started by: bubba112557
2 Replies
Login or Register to Ask a Question