Fix `Username Is Not In The Sudoers File. This Incident Will Be Reported` On Debian


This article explains how to "fix" sudo not working on Linux, resulting in this message when trying to use it: "your-username is not in the sudoers file. This incident will be reported." on Debian (and Debian-based Linux distributions like Ubuntu). sudo allows system admins to execute commands as root (administrator) or another user.

Example from a fresh Debian 10 (10.1) Buster installation on which sudo doesn't work:

$ sudo apt update
[sudo] password for logix:
logix is not in the sudoers file.  This incident will be reported.

sudo doesn't work by default on a Fresh Debian installation because your username is not automatically added to the sudo group (it does work on Ubuntu by default). But you may also see this if you created a new user but you forgot to add it to the sudo group, or if another user from your system removed the username from the sudo group.

You can check if the currently logged in user belongs to the sudo group by using the groups command. If the groups command does not return sudo on Debian-based Linux distributions, then that username can't run commands with sudo. Example with output of a Debian user that's not in the sudo group:

$ groups
logix cdrom floppy audio dip video pugdev netdev scanner lpadmin

You might like: How To Install The Latest Firefox (Non-ESR) On Debian 10 Buster (Stable) Or Bullseye (Testing)

The solution to this is to add that user to the sudo group. But how do you get root in that case, since you can't modify or add users as a regular user? Use su - (or sudo su -), then add the user to the sudo group.

So to get root, then add your user to the sudo group, use:

su -
usermod -aG sudo YOUR_USERNAME
exit

Where:

  • su switches to the root user, while - runs a login shell so things like /etc/profile, .bashrc, and so on are executed (this way commands like usermod will be in your $PATH, so you don't have to type the full path to the executable). You may also use sudo su - instead of su -
  • You need to replace YOUR_USERNAME with the username that you want to add to the sudo group.
  • I have used usermode to add a group to an existing user because it should work on any Linux distribution. adduser or useradd can also be used for this (adduser USERNAME -G sudo) but they may not work across all Linux distributions. Even though this article is for Debian, I wanted to make it possible to use this on other Linux distributions as well (I noticed that adduser doesn't work on Solus OS for example).
  • exit exists the root shell, so you can run commands as a regular user again.

After this, sudo still won't work! You will need to logout from that user, then relogin, and sudo will work.

This fixes the "Username is not in the sudoers file.  This incident will be reported" issue on your Debian machine, but you may run into another problem in some cases - sudo might not be installed at all by default. This is the case for example on a minimal Debian installation. In that case you'll see an error like this when trying to run a command with sudo:

$ sudo apt update
bash: sudo: command not found

In that case, install sudo on Debian like this:

su - #or 'sudo su -'
apt install sudo
exit

A few more Debian-related articles you might like: