to list all users :
cut -d: -f1 /etc/passwd
To remove user :
sudo userdel username
To remove home directory :
sudo rm -r /home/username
To add a home directory to an existing user :
create a home directory
chown this directory for the user
sudo usermod -d /home/directory user
Answer from nux on askubuntu.comto list all users :
cut -d: -f1 /etc/passwd
To remove user :
sudo userdel username
To remove home directory :
sudo rm -r /home/username
To add a home directory to an existing user :
create a home directory
chown this directory for the user
sudo usermod -d /home/directory user
You can use the more advanced deluser command:
sudo deluser --remove-home user
You can also try the the --remove-all-files option. From man deluser:
By default, deluser will remove the user without removing the home
directory, the mail spool or any other files on the system owned by
the user. Removing the home directory and mail spool can be achieved
using the --remove-home option.
The --remove-all-files option removes all files on the system owned by
the user. Note that if you activate both options --remove-home will
have no effect because all files including the home directory and mail
spool are already covered by the --remove-all-files option.
As can be expected, the second option may take a while to complete.
Does `userdel` delete the home directory?
Does deleting a user remove their cron jobs?
How do I undo a user deletion?
Videos
The -r does the exact opposite of what you want. From userdel --help:
-r, --remove remove home directory and mail spool
Instead of userdel -r user, just use:
userdel user
Option 1:
Use the deluser command.
However, before doing so check the file /etc/deluser.conf
And check the line
# Remove home directory and mail spool when user is removed
REMOVE_HOME = 0
Ensure this to 0 (not 1) before calling deluser.
Option 2:
sudo vipw Find the entry you want to remove (first part of the line up to the first ':' is the username). Then type "dd" to remove the entire line.
Save and exit with "esc + wq".
(Debian 12.9)
Do I need to back up the contents of the user's home folder before deleting?
I'm currently trying to refresh a server after it got a little bloated, and I just want to make sure I can do so without losing the old data. The user in question is only used as a dummy user for a custom systemd service.
You can either use @RalfFriedl's answer and use the -d option, which is used mostly with /nonexistent for that case, or you can edit the /etc/passwd file:
Just delete /home/squeezelite:
From something like:
squeezelite:x:1001:1001::/home/squeezelite:
You can either make it:
squeezelite:x:1001:1001:::
or
squeezelite:x:1001:1001::/nonexistent:
You can also use the root directory / for the home directory. However, my top suggestion is to just delete /home/squeezelite from the passwd file. Then the user will have no home directory.
From man usermod
-d, --home HOME_DIR
The user's new login directory.
If the old home didn't matter, the new home probably also wouldn't matter, but you could use something like /var/nonexistent to show that the home directory is not needed or supposed to exist.