3

After a long search on the web, trying everything I can find I came to ask you guys, how can I add an existing user to be a sudoer?

I've tried

usermod -a -G sudo user

and also

adduser user

both while in root... the first one didn't work at all and the second one supposedly added 'user' to sudoers but when I try to run sudo with that user it says: user is not in the sudoers file. This incident will be reported.

When I run adduser again, it says the user 'user' is already a member of 'sudo'.

what can I do???

-EDIT: for clarification, I do want the user to be prompted for a password when trying to run sudo. currently when the user is running sudo he is being prompted for a password and then he gets "user is not in sudoers file...." I wand him to be able to run sudo, be prompted and then escalate the privilege.

4

As root edit /etc/sudoers and place the following line:

youruser    ALL=(ALL) NOPASSWD:ALL

after

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

In this way you will be capable to execute all commands that require sudo privileges passwordless.

In order to use sudo and be prompted for a password you need to remove

NOPASSWD:ALL
  • where should I place the youruser ALL line? the %sudo ALL=(ALL:ALL) ALL is already there – ValarMorghulis Jun 28 '16 at 10:26
  • after the line: "%sudo ALL=(ALL:ALL) ALL" You should replace "youruser" with your username in the example. – Ivan P Jun 28 '16 at 10:27
  • but I do want the user to be prompted for a password – ValarMorghulis Jun 28 '16 at 10:30
  • use the following code: youruser ALL=(ALL) ALL – Ivan P Jun 28 '16 at 10:33
7

Following command is correct and enough but you need to completely logout the user and relogin to work .

usermod -a -G sudo user
  • 1
    I've found that logging out and logging in again often isn't enough, but a full restart is required. – Michael Platings Nov 2 '17 at 15:25
1

In addition, in Debian if it doesn't work you need to install sudo package:

apt install sudo

Then you should have /etc/sudoers file and proceed as follows:

usermod -a -G sudo user
0

You need to run the following command as root: (where USERNAME is replaced with your actual username)

usermod -aG sudo USERNAME

It won't work if you try to run this command under an unprivileged user.
So to gain root rights and to make sure that the necessary packages are installed, do this:

su
[enter password for your root user]
apt update
apt upgrade
apt install sudo
apt install usermod
usermod -ag sudo USERNAME
reboot

Don't forget to reboot at the end!
After the reboot the user should be able to use sudo commands.

Tip: if you tried to run a long command which failed because it actually requires sudoer rights, you can use the following trick to run the command again but this time with the correct privilege:

sudo !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.