ThisCalculator

Everyday

Add and Delete Users on CentOS 8 Safely

Updated · Standard formula, shown below

This guide shows you how to add and delete user accounts on a CentOS 8 server without locking yourself out or leaving insecure leftovers. You’ll learn the exact commands for creating users with home directories, setting passwords, granting sudo privileges, and removing accounts cleanly—plus how to avoid the common pitfalls that break SSH access or orphan files.

What You’ll Need

  • Root or sudo access to a CentOS 8 server (local console or SSH)
  • Basic comfort with the command line
  • If you’re using SSH, keep your current session open until you’ve verified the new user can log in—this prevents accidental lockouts.

Step-by-Step

  1. Check current users (optional but useful). Run awk -F: '{ print $1}' /etc/passwd to list existing usernames. This helps you avoid duplicates and see what’s already there.
  2. Add a new user with a home directory. Use useradd -m username. The -m flag creates the home directory (e.g., /home/username) and copies default skeleton files like .bashrc. If you don’t use -m, the user gets no home directory—fine for service accounts, but not for human users.
  3. Set a password. Run passwd username. You’ll be prompted to enter and confirm a strong password. Choose something long (12+ characters) with mixed case, numbers, and symbols.
  4. Add the user to the wheel group for sudo access (optional). If the user needs administrative rights, run usermod -aG wheel username. The -aG flag appends the group without removing other group memberships. Do not skip the -a—without it, you’ll overwrite existing groups.
  5. Verify the user’s default shell and account status. Run grep username /etc/passwd to confirm the shell is /bin/bash (or another valid shell). If it shows /sbin/nologin, the user can’t log in interactively—that’s intentional for service accounts.
  6. Test the new user login. If you’re on SSH, open a new terminal and try ssh username@your-server-ip. If you’re local, use su - username to switch to the new account. Verify you can log in and, if applicable, run sudo whoami to confirm sudo works.
  7. To delete a user without removing their files: Run userdel username. This removes the account but leaves the home directory and any files owned by the user intact. This is useful if you need to archive data.
  8. To delete a user and their home directory: Run userdel -r username. The -r flag removes the home directory and mail spool. Be absolutely sure you don’t need any files from that home directory—this is irreversible.
  9. Check for leftover files. After deletion, run find / -user username 2>/dev/null to find any files still owned by the deleted user (if you didn’t use -r). You can then decide to keep, archive, or delete them manually.
  10. Remove the user from any additional groups (if you skipped -r). The user is gone, so group memberships vanish automatically, but if you had manually added them to groups, those references are cleaned up by userdel.

Common Mistakes

  • Forgetting -m when creating a user—results in no home directory, which can confuse users and some applications.
  • Using usermod -G instead of -aG—this removes the user from all other groups, potentially breaking their access to necessary resources.
  • Deleting a user while they are logged in—the user might still have running processes. Use pkill -u username or killall -u username first to terminate their processes.
  • Deleting a user with -r without checking for important files—you might delete critical data. Always search for files owned by the user first.
  • Not testing the new user’s login before closing your current session—if you’re the only admin and you mess up, you could lock yourself out.

Tips

  • Use a consistent naming convention for usernames (e.g., first initial + last name).
  • Set password expiry policies using chage (e.g., chage -M 90 username forces a password change every 90 days).
  • For service accounts, use useradd -r -s /sbin/nologin username to create a system account with no login shell.
  • Consider using SSH keys instead of passwords for remote users—it’s more secure. You can add a public key to /home/username/.ssh/authorized_keys.
  • Before deleting, use chown -R root:root /home/username to transfer ownership of important files to root, then delete the user without -r.

Troubleshooting

User can’t log in via SSH. Check if the user’s shell is valid (grep username /etc/passwd). If it’s /sbin/nologin, change it with usermod -s /bin/bash username. Also verify the SSH service allows password authentication (PasswordAuthentication yes in /etc/ssh/sshd_config) and restart SSH.

Sudo says “user is not in the sudoers file.” This means the user isn’t in the wheel group. Run usermod -aG wheel username as root, then log out and back in.

Home directory is not created. If you forgot -m, you can create it manually: mkdir /home/username, then cp -r /etc/skel/. /home/username, and set ownership with chown -R username:username /home/username.

Deleting a user fails with “user is currently used by process.” Find the processes with pgrep -u username and kill them with pkill -u username, then retry userdel.

After deletion, files remain and you can’t delete them. The files are owned by a now-invalid UID. Use find / -uid -delete as root (replace <old_uid> with the numeric UID you can find in /etc/passwd before deletion).

Frequently Asked Questions

Q: What’s the difference between userdel and userdel -r?
A: userdel removes the account entry but leaves the home directory and files. userdel -r also deletes the home directory and mail spool.

Q: How do I temporarily disable a user without deleting them?
A: Use usermod -L username to lock the account (prefixes the password hash with !). To re-enable, use usermod -U username.

Q: Can I add a user without a home directory?
A: Yes, just use useradd username without -m. This is common for service accounts that don’t need a home.

Q: How do I change a user’s username?
A: Use usermod -l newname oldname, then rename the home directory with mv /home/oldname /home/newname and update ownership with usermod -d /home/newname newname.

Q: What is the wheel group?
A: On CentOS, the wheel group is the default group allowed to use sudo. Adding a user to it grants administrative privileges.

Q: Is it safe to delete a user who owns files elsewhere?
A: It’s safe in the sense that the files remain, but they become orphaned (owned by a numeric UID). You should find and reassign or delete them to avoid confusion.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.