2

I am supposed to clear all users that have the ID of 0 other than root. For example, my passwd file contains a user root with ID 0 and a user homer with ID 0.

I tried something like

grep :x:0: passwd | grep -v root:x: | awk -F : '{ print $1 }' | xargs userdel

but I received error userdel: user homer is currently logged in even though I am logged in as root, not homer. This error comes from them sharing and ID, I presume.

Is there any way around this? Should I just edit the passwd and shadow files? Otherwise, is there a way to force id 0 to be unique so that we can guarantee no other users will be created with id 0? Thanks.

Bryce
  • 8,313
  • 6
  • 55
  • 73
k.schroeder31
  • 801
  • 2
  • 8
  • 15
  • 2
    UIDs *are* unique. You don't have two users with ID = 0, you have two usernames for the user with ID = 0. Your command is asking `userdel` to delete that user (via its username `homer`), which you can't do because you were logged in as that user (via its username `root`). – ruakh Sep 11 '13 at 18:56
  • OK thanks. Is there a way to delete the other usernames? Is removing the lines from passwd and shadow all that is necessary? – k.schroeder31 Sep 11 '13 at 19:06
  • See below for the right command. Editing directly is dangerous on a machine with any load. – Bryce Sep 11 '13 at 19:08

3 Answers3

8

"homer" and "root" are the same account. You can have multiple usernames for a single account. See also https://unix.stackexchange.com/questions/49993/another-account-with-same-uid-as-root-gets-prompted-to-set-new-password-for-root

You'll want vipw and vipw -s to fix this. Editing directly with vim or sed is a bad idea.

Consider https://serverfault.com/ or https://unix.stackexchange.com/ for future questions of this type.

Community
  • 1
  • 1
Bryce
  • 8,313
  • 6
  • 55
  • 73
3

This is a trick: userdel -rf username

This command shows you:

userdel: user XXX is currently used by process 1

but username has removed

2

You can have this sed command:

sed '/^[^:]\+:x:0:/{/^root:/!d}' /etc/passwd

Or

sed -i '/^[^:]\+:x:0:/{/^root:/!d}' /etc/passwd

Which would modify the file.

konsolebox
  • 72,135
  • 12
  • 99
  • 105