Can't delete user account

7,568

Solution 1

For root/sudo user it is possible to just create folders in /home that are not associated to any user account. That is why ls /home is not a good command to check for users.

To list all users that have their home folder below "/home", you can run:

getent passwd | grep "/home" | cut -d: -f1

List all users: getent passwd | cut -d: -f1

Check if user u942 exists: getent passwd | grep u942. If you get no output, no such user exists.

If the output is empty, you can delete that folder (you might need sudo) rm -Rf /home/u942. But double check the contents of the folder before you delete it.

Solution 2

As you might know, it's possible to create a home directory differ from "username". For example I can have a user named "john" which its home directory is: /home/jack.

If you already know the user name, you could run:

echo ~username

It would print that specific user home directory. You can also run:

ls -ld /home/u942

or

stat -c %U /home/u942

to see which user owns this home directory then delete that user.

Also there is a chance that user is already deleted but its home directory not.

To find it out you can do something like:

id -un `stat -c %U /home/u942`

If it returned nothing it means the owner does not exist on your system. simply remove that directory If only you believe there is no important file there.

Share:
7,568

Related videos on Youtube

hussain alzaabi
Author by

hussain alzaabi

Updated on September 18, 2022

Comments

  • hussain alzaabi
    hussain alzaabi over 1 year

    In a terminal:

    /home$ ls
    abuabdullah  alzaabi  hussain  u942
    

    Then I tried to delete account u942 by using command line:

    /home$ deluser u942
    /usr/sbin/deluser: Only root may remove a user or group from the system.
    

    Then I tried to use sudo

    /home$ sudo deluser u942
    /usr/sbin/deluser: The user `u942' does not exist.
    

    How can I solve this problem?

    • muru
      muru about 7 years
      Did somebody just create a directory in /home? Is there a reason you think there's an account named u942 in your system?
    • pLumo
      pLumo about 7 years
      To verify that there is no such user: Whats the output of cut -d: -f1 /etc/passwd | grep u942. (probably empty)
    • Yaron
      Yaron about 7 years
      please add the output of ls -lsd /home/u942 so we'll able to see the owner of this folder
  • Michal Polovka
    Michal Polovka about 7 years
    rm command won't work, to delete folder with content use rm -rf /home/u942 instead. Note: Be careful, as with -rf flag you can easily delete anything.
  • muru
    muru about 7 years
    You should actually use getent passwd instead of reading /etc/passwd manualy. In particular, to see if a u942 is a user in the system: getent passwd u942.
  • pLumo
    pLumo about 7 years
    Why "should" I. There is no problem with /etc/passwd. getent passwd gives me also non-local users, which I may or may not want.
  • muru
    muru about 7 years
    You gave the reason: "non-local users". There's no guarantee that non-local users also have non-local home directories. "If you get no output, no such user exists" is not necessarily true if you only read /etc/passwd.
  • pLumo
    pLumo about 7 years
    That is true. I edited the answer ...