15

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.

5 Answers 5

18

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
5
  • where should I place the youruser ALL line? the %sudo ALL=(ALL:ALL) ALL is already there Commented Jun 28, 2016 at 10:26
  • after the line: "%sudo ALL=(ALL:ALL) ALL" You should replace "youruser" with your username in the example.
    – Ivan P
    Commented Jun 28, 2016 at 10:27
  • but I do want the user to be prompted for a password Commented Jun 28, 2016 at 10:30
  • use the following code: youruser ALL=(ALL) ALL
    – Ivan P
    Commented Jun 28, 2016 at 10:33
  • If there is an (syntax) error in Your /etc/sudoers you may loose your sudo access at all.- Use visudo instead.
    – sneaky
    Commented Jul 19, 2021 at 8:21
16

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

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

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
1
  • If you've already manually created /etc/sudoers, an interactive prompt will ask you if you'd like to replace it with the default installed from Apt.
    – vhs
    Commented Aug 29, 2019 at 6:43
2

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 !!
0
0

From Debian Wiki

Add existing user from commandline  

To add an existing user with id=foo to group=sudo:

$ sudo adduser foo sudo

Alternatively, you can first get root (e.g., sudo su -) and then run the same commands without prefix=sudo:

# adduser foo
# adduser foo sudo

After being added to a new group the user must log out and then log back in again for the new group to take effect. Groups are only assigned to users at login time. A most common source of confusion is that people add themselves to a new group but then do not log out and back in again and then have problems because the group is not assigned; be sure to verify group membership.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .