Removing a user account in Linux is a common administrative task. It becomes necessary when a user no longer requires access to the system. This can occur when an employee leaves or when a user’s access privileges are no longer relevant. The removal process ensures the system remains secure by eliminating unnecessary user accounts.
User data is stored in files such as etc/passwd, etc/group, and etc/shadow. Removing a user requires deleting their entries from these files. While this can be done manually, using tools like userdel or deluser is more efficient and reduces the risk of errors. These tools handle the removal of user data from all relevant system files automatically.
When deleting a user, it’s important to consider whether to remove the user's home directory and any personal files. This can be handled with options within the removal tools. Backup important data before deleting the account, as the process can result in permanent data loss.
Steps to delete a user in Linux:
- Open the terminal.
Ensure you have root or sudo privileges before proceeding.
- Retrieve the list of users on your system.
$ getent passwd 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 uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin proxy:x:13:13:proxy:/bin:/usr/sbin/nologin www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin backup:x:34:34:backup:/var/backups:/usr/sbin/nologin list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin systemd-timesync:x:100:101:systemd Time Synchronization,,,:/run/systemd:/usr/sbin/nologin systemd-network:x:101:103:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd:/usr/sbin/nologin messagebus:x:103:106::/nonexistent:/usr/sbin/nologin syslog:x:104:110::/home/syslog:/usr/sbin/nologin _apt:x:105:65534::/nonexistent:/usr/sbin/nologin tss:x:106:111:TPM software stack,,,:/var/lib/tpm:/bin/false uuidd:x:107:114::/run/uuidd:/usr/sbin/nologin tcpdump:x:108:115::/nonexistent:/usr/sbin/nologin avahi-autoipd:x:109:117:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/usr/sbin/nologin usbmux:x:110:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologin rtkit:x:111:118:RealtimeKit,,,:/proc:/usr/sbin/nologin dnsmasq:x:112:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologin avahi:x:113:120:Avahi mDNS daemon,,,:/run/avahi-daemon:/usr/sbin/nologin cups-pk-helper:x:114:121:user for cups-pk-helper service,,,:/home/cups-pk-helper:/usr/sbin/nologin speech-dispatcher:x:115:29:Speech Dispatcher,,,:/run/speech-dispatcher:/bin/false kernoops:x:116:65534:Kernel Oops Tracking Daemon,,,:/:/usr/sbin/nologin saned:x:117:123::/var/lib/saned:/usr/sbin/nologin nm-openvpn:x:118:124:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologin whoopsie:x:119:125::/nonexistent:/bin/false colord:x:120:126:colord colour management daemon,,,:/var/lib/colord:/usr/sbin/nologin sssd:x:121:127:SSSD system user,,,:/var/lib/sss:/usr/sbin/nologin geoclue:x:122:128::/var/lib/geoclue:/usr/sbin/nologin pulse:x:123:129:PulseAudio daemon,,,:/var/run/pulse:/usr/sbin/nologin hplip:x:124:7:HPLIP system user,,,:/run/hplip:/bin/false gnome-initial-setup:x:125:65534::/run/gnome-initial-setup/:/bin/false gdm:x:126:131:Gnome Display Manager:/var/lib/gdm3:/bin/false user:x:1000:1000:user,,,:/home/user:/bin/bash systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin shakir:x:1001:1001:shakir,,,:/home/shakir:/bin/bash
Check the output to identify the user you want to delete.
- Force the user to log out from the system.
$ sudo pkill -u shakir [sudo] password for user:
If the user is logged in, this will terminate their session.
Related: How to force logout a user in Linux
- Verify that the user is no longer logged in to the system.
$ who user :0 2021-01-23 10:28 (:0)
- Back up the user's home directory or important files if necessary.
$ sudo cp -a /home/shakir /backup-shakir
- Delete the user account.
$ sudo deluser shakir Removing user `shakir' ... Warning: group `shakir' has no more members. Done.
Use the --remove-home option to also delete the user’s home directory.
$ sudo deluser --remove-home shakir
- Verify that the user's account has been removed from the system.
$ id shakir id: ‘shakir’: no such user $ grep shakir /etc/passwd $ grep shakir /etc/group $ ls -l /home total 8 drwxr-xr-x 5 1001 1001 4096 Jan 23 10:46 shakir drwxr-xr-x 15 user user 4096 Okt 25 09:28 user
Home directory should not exist if you previously used --remove-home option.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.