Software & OS

How to Add a User to Sudoers on Ubuntu, Debian and Fedora

To add a user to sudoers, add them to the administrator group: sudo usermod -aG sudo alex on Ubuntu and Debian, or sudo usermod -aG wheel alex on Fedora, RHEL, Rocky and AlmaLinux. The user then has to log out and back in, because group changes only apply to new login sessions.

Replace alex with the username in every command below. You need to run them from an account that already has sudo, or as root.

Add a user to sudo on Ubuntu and Debian

sudo usermod -aG sudo alex

-G names a supplementary group and -a appends it. Don't drop the -a: the usermod manual warns that without it, the user is removed from every supplementary group not listed in the command. On Debian, adduser alex sudo (run as root) does the same thing and is the form the Debian Wiki shows.

This works because the default /etc/sudoers already contains a rule for the group. On Ubuntu 22.04 the uncommented lines are:

Defaults	env_reset
Defaults	mail_badpass
Defaults	secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
Defaults	use_pty
root	ALL=(ALL:ALL) ALL
%admin ALL=(ALL) ALL
%sudo	ALL=(ALL:ALL) ALL
@includedir /etc/sudoers.d

The %sudo line means "members of the group sudo may run any command, as any user and any group, on any host". You never need to edit the file for the basic case.

Add a user to sudo on Fedora, RHEL, Rocky and AlmaLinux

sudo usermod -aG wheel alex

Red Hat family distributions use the group wheel instead of sudo. The default sudoers file shipped in the CentOS Stream 9 sudo package, for example, has the matching rule active: %wheel ALL=(ALL) ALL, under the comment ## Allows people in group wheel to run all commands. If that line is commented out on your system, uncomment it with sudo visudo.

Make the change take effect

The user's running sessions keep their old group list. Log out completely and back in, then check:

groups
sudo -v

groups should list sudo or wheel. sudo -v asks for the user's own password (not root's) and succeeds silently if access works. To test without logging out, open a fresh login shell with su - alex. To see exactly what a user may run, an administrator can use sudo -l -U alex.

Add a sudoers rule with visudo (more control)

Group membership gives full root access. For a narrower rule, write a sudoers entry instead, and always do it through visudo. It locks the file against simultaneous edits and checks the syntax before saving, so a typo can't leave you without sudo.

Put your rule in its own file under /etc/sudoers.d instead of editing the main file, so package upgrades don't conflict with your change:

sudo visudo -f /etc/sudoers.d/alex

Then add one of these lines:

# Full sudo access, same as the group
alex ALL=(ALL:ALL) ALL

# Only allow restarting one service
alex ALL=(root) /usr/bin/systemctl restart nginx

The sudoers manual describes the format as "who where = (as_whom) what". In alex ALL=(root) /usr/bin/systemctl restart nginx: alex is who, ALL is any host, (root) is the user the command runs as, and the rest is the only command allowed. Use full paths for commands. A % in front of the first field means a group, as in %developers. For more on managing services, see systemctl commands.

Two filename rules catch people out. sudo reads files in /etc/sudoers.d in sorted order, and it skips any file whose name contains a . or ends in ~. A file called alex.conf is silently ignored; call it alex.

Check a sudoers file before it locks you out

sudo visudo -c

Example output on a healthy system:

/etc/sudoers: parsed OK
/etc/sudoers.d/README: parsed OK

A broken file shows the line and column of the problem. This is real output from checking a test file with a missing bracket (visudo -cf bad):

bad:1:19: syntax error
alex ALL=(ALL:ALL ALL
                  ^~~

When you save a broken edit from inside visudo, it asks What now?. Press e to edit again or x to exit without saving. Avoid Q, which saves anyway: the visudo manual points out that if visudo found a syntax error, sudo will hit it too.

Rule order and passwordless sudo

When several rules match the same user, sudo applies the last match, not the most specific one. A broad rule placed after a narrow one wins. Files in /etc/sudoers.d are read where the @includedir line sits, which on Ubuntu is the end of the file, so they override the group rules above it.

To skip the password prompt, add the NOPASSWD: tag:

alex ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx

Keep it limited to specific commands. The Debian Wiki calls %sudo ALL=(ALL) NOPASSWD: ALL "usually a bad idea", because anything running as that user can then become root silently.

"alex is not in the sudoers file"

This error means none of the sudoers rules match the account. Depending on the sudo version and configuration, it reads alex is not in the sudoers file. or adds This incident will be reported. after it. Check, in order:

  1. Did you log out and back in after adding the group? Run groups.
  2. Is it the right group for the distribution: sudo on Debian and Ubuntu, wheel on Fedora and RHEL?
  3. Is the group rule in /etc/sudoers present and uncommented? Check with sudo visudo from an admin account.
  4. If you used a drop-in file, does its name contain a dot? Rename it.

If no account on the machine has sudo, you need root another way: the root password with su -, recovery mode from the boot menu, or on WSL, wsl -u root from Windows, which opens a root shell in the distribution. For background on users, groups and file permissions, see Linux file permissions explained.

Frequently asked questions

Why does sudo still say I'm not in the sudoers file after adding the group?
Group membership is read when you log in, so your current session still has the old groups. Log out completely and log back in (or reboot, or use su - username to start a fresh login shell), then run groups to confirm the sudo or wheel group is listed.
What happens if I break /etc/sudoers?
sudo refuses to run until the file parses, which can lock you out of administrator access. That is why you edit it with visudo, which checks the syntax before saving. If it is already broken, you need another route to root: the root password with su -, a root shell from recovery mode, or wsl -u root on WSL, and then run visudo to fix it.
How do I remove sudo access from a user?
Remove the user from the group with sudo gpasswd -d alex sudo (or wheel on Fedora), or delete the user's file from /etc/sudoers.d. The change applies at the user's next login.
Is it safe to give a user NOPASSWD sudo?
It means anyone who gets access to that account, or any program running as that user, can become root without a password prompt. The Debian Wiki calls passwordless sudo usually a bad idea. If you need it for automation, limit it to the specific commands the automation runs.

Sources