Skip to content
ohsudo, Home
Linux

"user is not in the sudoers file": how to get sudo back, even without the root password

Fix "user is not in the sudoers file": add yourself to the sudo or wheel group with su, recover without a root password, use visudo safely and fix a broken sudoers file.

Beginner4 min readApplies to Ubuntu · Debian · RHEL / Rocky / AlmaLinux · Linux with sudo

by OhSudo ·

Checked on real software: sudo, Linux · November 2026How we check

Part of a learning path:Linux: from zero to fixing a server

$ sudo apt update
[sudo] password for alex:
alex is not in the sudoers file.

Older versions add This incident will be reported. That only means a line in the system log, so nobody is coming for you.

The system knows who you are, and your password was right. Your account just isn’t allowed to use sudo. To change that you need root, which raises the obvious problem: how do you get root without sudo? There are a few ways, depending on what you have.

TL;DR

If you know the root password, or another admin can log in:

Terminal window
su - # asks for the ROOT password
usermod -aG sudo alex # Debian / Ubuntu
usermod -aG wheel alex # RHEL / Rocky / AlmaLinux
exit

Then log out and back in. The new group only applies to new sessions.

1. Add yourself to the admin group

sudo allows one group by default, and which one depends on the distribution:

Distribution Group
Ubuntu, Debian sudo
RHEL, Rocky, AlmaLinux, Fedora wheel

Become root with su -. It asks for root’s password, not yours, and that’s the difference from sudo. Then add the user:

Terminal window
usermod -aG sudo alex

Keep the -a. Without it, -G replaces all of the user’s other groups rather than adding to them.

The Debian installer has a catch: if you set a root password during setup, it doesn’t put your first user in sudo, and that’s the usual way people first see this message. It may not even have sudo installed: apt install sudo as root.

2. Log out, then check

The groups of a session are fixed when it starts, so the running shell doesn’t know yet:

Terminal window
exit # or log out of the desktop

Log in again and check:

Terminal window
groups
sudo -l
User alex may run the following commands on server01:
(ALL : ALL) ALL

3. Without the root password

On Ubuntu, root has no password by default, so su - fails with Authentication failure. Your first user normally has sudo. If you’ve lost it, you need a way in from outside the running system.

A cloud server. Use the provider’s tools, as they can act as root: AWS Systems Manager Session Manager, the Azure Run Command or serial console, or Google Cloud’s serial console or gcloud compute ssh, which manages sudo access itself. As a last resort, detach the disk, attach it to another machine and edit the files from there.

A machine with a console. Boot into recovery from GRUB:

  1. Restart, and hold Shift (BIOS) or press Esc (UEFI) to see the GRUB menu.
  2. Highlight the normal entry, and press e.
  3. On the line starting with linux, add init=/bin/bash at the end. Press F10 or Ctrl+X.
  4. You get a root shell with no password:
Terminal window
mount -o remount,rw /
usermod -aG sudo alex
sync
exec /sbin/init

Instead of init=/bin/bash, Ubuntu also has Advanced options → recovery mode → root. Encrypted disks still ask for their passphrase, and some setups protect GRUB with a password. RHEL with SELinux needs one more step before rebooting, or files can be labelled wrongly: touch /.autorelabel.

4. Giving exact rights with sudoers

The group gives everything. For a specific user or limited rights, add a file in /etc/sudoers.d/, and always edit it through visudo, as it checks the syntax before saving:

Terminal window
sudo visudo -f /etc/sudoers.d/alex
alex ALL=(ALL:ALL) ALL

That’s everything again, as user alex. Limited to restarting one service, it would be:

deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp

Use full paths, and take care with commands like vim, less or tar: they can run a shell, so granting them means granting everything. File names in that folder can’t contain a . or end in ~, or sudo silently ignores them. Permissions must be 0440:

Terminal window
sudo chmod 440 /etc/sudoers.d/alex

5. You broke the sudoers file

Editing /etc/sudoers directly can lock everyone out:

>>> /etc/sudoers: syntax error near line 28 <<<
sudo: parse error in /etc/sudoers near line 28
sudo: no valid sudoers sources found, quitting

If you have the root password, su -, then visudo to fix it. On a machine using polkit, this often works without root’s password, for a user in the admin group:

Terminal window
pkexec visudo

Otherwise, use the recovery in section 3. If the error names a file in /etc/sudoers.d/, you can simply remove that file. To check everything at the end:

Terminal window
sudo visudo -c

Warning

On a server where you can’t reboot into recovery, keep a second session open as root while you change sudoers. If the change locks you out, that session is your way back. And don’t “fix” this with chmod on /etc/sudoers or /usr/bin/sudo: wrong permissions on them make sudo refuse to run at all.

Shell, we have a problem!

Similar, but different: sudo: unable to resolve host is about the machine’s name, not your rights: see that article. alex is not allowed to run sudo on server01 means your account is in sudoers, but not for this machine or command. sudo: a password is required appears when there’s no terminal to ask on, as in a script. To learn the basics, try the lesson Owners, chown and sudo. sudo: a terminal is required to read the password means sudo may run, but can’t ask for the password.

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles