Add Delete Grant Sudo Users Debian
In this article
This guide shows you how to add a new user, grant them sudo (administrative) privileges, and remove a user or their sudo access on a Debian system. You’ll need root access or an existing sudo user to run the commands, and you’ll do everything from the terminal.
What You'll Need
- A Debian system (any current version, e.g., Debian 11 or 12)
- Access to a terminal (local or SSH)
- Root password, or a user account that already has sudo privileges
Step-by-Step
-
Open a terminal. If you’re logged in as a regular user, switch to root with
su -and enter the root password. If you already have sudo, you can run the commands withsudoinstead. -
Add a new user. Use the
addusercommand (it’s more interactive thanuseraddand creates the home directory, group, and sets a password):
Replaceadduser usernameusernamewith the desired login name. You’ll be prompted to set a password and fill in optional user info (Full Name, etc.). Leave those blank if you want. -
Grant sudo privileges. The standard way is to add the user to the
sudogroup. Use theusermodcommand:
Theusermod -aG sudo username-aGoption appends the user to the group without removing them from other groups. If you prefer to edit the sudoers file directly (advanced), runvisudoand add a line likeusername ALL=(ALL:ALL) ALLunder the “User privilege specification” section. -
Verify sudo access. Ask the user to log out and back in (or run
su - username), then test with:
If it printssudo whoamiroot, the user has sudo privileges. -
Remove sudo privileges only. To take away sudo access but keep the user account, remove them from the sudo group:
Or usegpasswd -d username sudodeluser username sudoon Debian. The user will still be able to log in, but not run sudo commands. -
Delete a user entirely. To remove the user account, use
deluser. To also delete their home directory and mail spool, add the--remove-homeflag:
If you want to remove all files owned by the user (even outside their home), adddeluser --remove-home username--remove-all-files(use with caution).
Common Mistakes
- Using
useraddinstead ofadduser.useraddis a low-level tool; it doesn’t create a home directory or set a password by default. Unless you know the flags, stick withadduser. - Forgetting the
-aflag inusermod. If you runusermod -G sudo usernamewithout-a, you’ll remove the user from all other groups, which can break their access to files or devices. - Editing sudoers with a regular editor. Always use
visudoto edit the sudoers file—it checks syntax and prevents you from locking yourself out. - Not logging out and back in. Group changes don’t take effect until the user’s next login. If they’re already logged in, they won’t have sudo until they re-login.
Tips
- Use
adduserfor new users—it’s the Debian-friendly wrapper that sets up everything correctly. - To grant passwordless sudo for a specific command, you can add a line in
visudolikeusername ALL=(ALL) NOPASSWD: /usr/bin/systemctl. Be careful—this can be a security risk. - Check which users are in the sudo group with
getent group sudo. - If you need to temporarily lock a user’s password (without deleting), use
passwd -l username.
Troubleshooting
- User can’t run sudo after being added to the group. They likely haven’t logged out and back in. Have them run
exitand log in again, or usesu - usernameto test. sudocommand not found. This means the sudo package isn’t installed. Install it as root:apt update && apt install sudo.- “User is not in the sudoers file” error. This usually means the user isn’t in the sudo group. Check with
groups username. If they are, but still getting the error, check the sudoers file for syntax errors withvisudo -c. - Accidentally removed your own sudo access. If you’re locked out, boot into recovery mode or use a live CD to edit the sudoers file and add your user back. Alternatively, if you have root password, log in as root and fix it.
- Deleting a user fails because they’re logged in. Kill their processes or force removal with
deluser --force username(but first make sure you want to remove them).
Frequently Asked Questions
- What’s the difference between
adduseranduseradd?adduseris a friendly Perl script that callsuseraddand also creates the home directory, sets up the user’s group, and prompts for a password.useraddis the low-level binary that does none of that by default. - Can I grant sudo to a user without adding them to the sudo group? Yes, you can add a line in the sudoers file (via
visudo) likeusername ALL=(ALL:ALL) ALL. But adding to the sudo group is simpler and easier to manage. - How do I list all users with sudo access? Run
getent group sudoto see group members. For users with direct sudoers entries, check the sudoers file and files in/etc/sudoers.d/. - What does
deluser --remove-homedo? It deletes the user’s home directory and mail spool. Without it, the home directory remains but is owned by the deleted user’s UID. - Is it safe to delete a user while they’re logged in? It’s not recommended. The user’s processes may continue running, and files they own may become orphaned. Log them out or kill their processes first.
- Can I change a user’s password without deleting them? Yes, use
passwd username(as root or with sudo). You can also force a password change on next login withchage -d 0 username.