242

In /etc/sudoers I see this:

# Allow members of group sudo to execute any command after they have
# provided their password
# (Note that later entries override this, so you might need to move
# it further down)
%sudo ALL=(ALL) ALL

So how do I add a user to that sudo group?

Jackspace
  • 133

8 Answers8

321
sudo usermod -aG sudo <username>

The a is very important. Without it they'll be removed from all other groups. You will need to either restart your shell/terminal or log out and back in for this to take effect.

See also:

maco
  • 15,892
  • I did what u recommended, but terminal told me bash: sudo: command not found, why? – Teifi Jan 21 '13 at 12:23
  • @Teifi, that means you probably don't have sudo installed or, for some weird reason, it's not in your path. What happens when you run which sudo? – nopcorn Jan 22 '13 at 16:33
  • New terminals were not enough. Ended up rebooting for another reason and now it works. Probably log-out/in. – deed02392 Jul 08 '14 at 15:35
  • I know a is for append, but i think it will be more usefull to have append as default behavior. Creating an alias is easy so that will be the way to have groupadd group then just append it. I did the command without -a once today as i didn't remember the -a argument. – m3nda Apr 12 '17 at 18:59
  • Although this is an Ubuntu forum, For CentOS I had to do sudo usermod -aG sudo <username>. Because there was no easy way for me to find this out. – akki May 25 '19 at 14:03
  • "The a is very important." This would have been nice to know before I rebooted my machine. – Code-Guru Jun 17 '21 at 18:40
55

You can either use the user management GUI for it (same place where you create users), or use sudo adduser <username> sudo in the command line.

txwikinger
  • 28,462
24

You can also use a graphical interface. Click on the gear on the top right of the panel, then select "System Settings" and then "User Accounts"

You need to click the little unlock button to be able to edit things in this window. Then click on the person's account and select the proper dropdown for "Account Type"

enter image description here

Jorge Castro
  • 71,754
qbi
  • 19,125
  • 1
    I have Ubuntu 10.10 but my graphic interface is someway locked. It opens but I when buttons are pressed nothing happens. I suppose is something related to the privileges (I'm actually sudoer). How can I solve this issue? – linello Jan 18 '13 at 09:49
  • Did you click the 'unlock' button? – daboross Jun 10 '13 at 02:22
  • Nice animation! I'd like to do such a gif animation to show new features of my web app ? I'm on Lubuntu 16.04... – Stephane Sep 21 '17 at 14:41
22

Its really simple if you are using Unity as your desktop environment.

If you have created a user already then you can simply change it from Standard to Administrator, else make sure that you selected Administrator when creating a new one.

Don't forget to unlock before trying to change it

enter image description here

Bruno Pereira
  • 73,643
4

sudo gpasswd -a $USER sudo

joschi
  • 235
3

Here's how I setup a non-root user with the base image of ubuntu:18.04:

RUN \
    groupadd -g 999 foo && useradd -u 999 -g foo -G sudo -m -s /bin/bash foo && \
    sed -i /etc/sudoers -re 's/^%sudo.*/%sudo ALL=(ALL:ALL) NOPASSWD: ALL/g' && \
    sed -i /etc/sudoers -re 's/^root.*/root ALL=(ALL:ALL) NOPASSWD: ALL/g' && \
    sed -i /etc/sudoers -re 's/^#includedir.*/## **Removed the include directive** ##"/g' && \
    echo "foo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers && \
    echo "Customized the sudoers file for passwordless access to the foo user!" && \
    echo "foo user:";  su - foo -c id

What happens with the above code:

  • The user and group foo is created.
  • The user foo is added to the both the foo and sudo group.
  • The uid and gid is set to the value of 999.
  • The home directory is set to /home/foo.
  • The shell is set to /bin/bash.
  • The sed command does inline updates to the /etc/sudoers file to allow foo and root users passwordless access to the sudo group.
  • The sed command disables the #includedir directive that would allow any files in subdirectories to override these inline updates.
  • Oh, gheeze. Yours is the kinda place I used to dream about back when my hat was a darker shade of midnight. Where do you wor- actually? Never mind. Neither of us need that. – NerdyDeeds May 17 '23 at 17:57
3

I am late to the party, but this answer might help someone that uses Ubuntu inside a Docker container.

I recently created a Docker container based on Ubuntu 16.04.1.

By default, the Docker Ubuntu image is a stripped down version of Ubuntu, which does not have a vast majority of common tools including sudo.

Besides, by default, the user is logged in to the Docker container as root.

Therefore, I started the container with the docker run command, and installed the 'sudo' package:

root@default:/# apt-get install sudo

Running the command adduser myuser sudo reported error adduser: The user 'myuser' does not exist.. After reading this answer, I first ran the command to create the user:

root@default:/# adduser myuser

Then ran the following command:

root@default:/# adduser myuser sudo
Adding user `myuser' to group `sudo' ...
Adding user myuser to group sudo
Done.

The user myuser was successfully added to the sudo group.

HelloWorld101
  • 191
  • 1
  • 4
1

Use usermod. Add the sudo permission with the following command:

usermod -aG sudo <your username>

Please note that you'll have to use the root account to do this or use another account that has sudo permissions. If you don't have access to another account for some reason and you don't know the root password, you'll need an Ubuntu (or another Linux distro) live CD and then you'll need to chroot into your Ubuntu filesystem and run the above command from inside the chroot.

Charlie
  • 131