How do I delete users that have uid 0?

6k views Asked by At

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.

3

There are 3 answers

0
Bryce On BEST ANSWER

"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.

0
konsolebox On

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.

0
Andrzej Chabierski On

This is a trick: userdel -rf username

This command shows you:

userdel: user XXX is currently used by process 1

but username has removed