As a Linux system administrator, you may need to delete a user account for various reasons – the user may have left the company, you want to clean up old unused accounts, or the account has been compromised. In Ubuntu and other Linux distributions, deleting a user can sometimes be tricky if you want to remove all traces of that user from the system.
In this comprehensive guide, I‘ll walk you through the proper steps to completely delete the default "ubuntu" user account in Ubuntu Linux, including removing the user‘s home folder, files, and any other remnants.
Prerequisites
Before deleting the ubuntu user, make sure you have:
- Root access to the Ubuntu system. We‘ll be using
sudo
for most commands. - Created a separate admin account to use after deleting ubuntu. Can‘t delete the account you‘re logged into!
- Backed up any ubuntu files you may need to keep. The user deletion will wipe everything.
Check Current Users on the System
First, let‘s check what user accounts exist currently on our system.
Use the cat /etc/passwd
command to view all users:
sudo cat /etc/passwd
Sample output:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
We can see that the ubuntu user indeed exists on this system, with home directory /home/ubuntu
and default shell /bin/bash
.
Now let‘s go through deleting it completely.
Step 1 – Delete the User Account
The first step is deleting the ubuntu user account itself. This will remove the user from the systems‘ account list.
Use the userdel
command:
sudo userdel ubuntu
This will delete the ubuntu user account, but leaves the user‘s home folder and files intact. We‘ll handle those next.
Double check that the user is now removed from /etc/passwd
:
sudo cat /etc/passwd
The ubuntu line should now be gone.
Step 2 – Delete the User‘s Home Folder
Even though the account is deleted, the home folder at /home/ubuntu
likely still exists with many leftover configuration files and folders.
We want to wipe it all out.
Warning: This will delete the entire /home/ubuntu
folder and all contents so be 100% sure there is nothing you need to save first!
Use the rm
command to recursively force-delete everything, including hidden files:
sudo rm -rf /home/ubuntu
The ubuntu home folder should now be gone. You can verify with ls /home
.
Step 3 – Remove References in /etc/group
In Linux, user groups are utilized to control permissions for files and processes. Any groups that the deleted user belonged to may still contain references to that user in the /etc/group
file and these need to be cleaned up.
First, let‘s see if the ubuntu user is still referenced in any groups with:
sudo grep ubuntu /etc/group
Sample output showing ubuntu still existing in the sudo and other groups:
sudo:x:27:ubuntu
adm:x:4:syslog,ubuntu
cdrom:x:24:ubuntu
dip:x:30:ubuntu
plugdev:x:46:ubuntu
lpadmin:x:106:ubuntu
sambashare:x:128:ubuntu
We need to remove ubuntu from all these groups with the groupmems
command:
sudo gpasswd -d ubuntu sudo
sudo gpasswd -d ubuntu adm
# Repeat for each group...
Now when checking for references again, there should be none:
sudo grep ubuntu /etc/group
# No output
The user ubuntu is now completely deleted from any groups.
Step 4 – Delete Associated Files
Next we‘ll want to delete any filesystem objects associated directly with the ubuntu user.
This includes files owned by the user in locations like /tmp
, /var/spool
and others.
First find any files still belonging to the user with:
sudo find / -user ubuntu
This may output various files like:
/var/spool/mail/ubuntu
/var/tmp/ubuntu-logo.png
Delete the files by doing:
sudo rm -rf /var/spool/mail/ubuntu
sudo rm /var/tmp/ubuntu-logo.png
# Repeat for any other files
Run the find command again to ensure no more ubuntu files exist.
Additionally, if ubuntu ran any cron jobs or had local service files, remove those via:
sudo crontab -u ubuntu -r
sudo rm -rf /etc/systemd/system/ubuntu.*
# etc
Step 5 – Logout All Associated Sessions
If the ubuntu user has any active login sessions or processes still running, we should terminate those for a complete logout:
View and terminate sessions:
sudo loginctl list-sessions | grep ubuntu
sudo loginctl terminate-session <session>
And kill any owned processes:
sudo pkill -u ubuntu
Now there should be nothing active associated with the deleted user account.
Step 6 – Create New User Account
As a final step, I recommend creating a new user account that can become the new main admin account, if ubuntu was being used as such previously.
For example, create a ‘johnsmith‘ admin user:
sudo adduser johnsmith
sudo usermod -aG sudo johnsmith
You can then assign ownership of any critical folders like /home/johnsmith
as needed.
Confirm Deletion
With the above steps complete, you have ruthlessly deleted the ubuntu user from root to stem.
Confirm by checking a few locations:
cat /etc/passwd # No ubuntu
ls /home # No ubuntu folder
grep ubuntu /etc/group # Not in any groups
find / -user ubuntu # No files owned by ubuntu
If all checks out, congratulations! You have successfully purged the ubuntu user.
The system is now ready for a fresh admin user to take over control.
Tips for Other Default User Accounts
While we focused specifically on deleting the ubuntu user in this guide, the same overall process applies to removing any system user account:
- Use
userdel
to delete the core user - Remove the user‘s home folder
- Delete group associations in
/etc/group
- Wipe owned files with
find
- Terminate sessions & processes
- Check for any custom user config or cron jobs
Some common default accounts you may want to delete on other Linux distributions include:
- fedora user on Fedora
- ec2-user on Amazon Linux AMI‘s
- centos user on CentOS systems
- pi user on Raspberry Pi OS
And desktop user accounts like:
- debian on Debian
- arch on Arch Linux
- manjaro user on Manjaro
Follow the same steps to remove extra accounts and declutter your Linux system.
Closing Thoughts
Removing a Linux user fully takes a bit more care than just quickly deleting the account itself. Make sure to clear out all file ownership, home folder contents, group memberships and other stray user links.
With the flexible ubuntu delete steps provided in this guide, however, you should be equipped to cleanly wipe default user accounts on Ubuntu or really any Linux distro. This allows you to start fresh with a single or multiple new admin users best suited for your needs.
As always when modifying critical system files and accounts, be extremely cautious, work as root, and have backups ready!
Let me know in the comments if you have any issues deleting your unneeded Linux users or have optimized tricks to share.