241

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?

3
  • Is anyone else here trying to get sudo to work with no password on ubuntu 17? May 4, 2017 at 17:27
  • @AdamF look here. Next time ask a new question if you want a speedier response. ;-)
    – Fabby
    May 3, 2018 at 22:27
  • Coffe-grinder: would you mind changing your acceptance to the most highly upvoted answer as that's a better way to do it? (no password at all)
    – Fabby
    May 3, 2018 at 22:28

8 Answers 8

320
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:

6
  • I did what u recommended, but terminal told me bash: sudo: command not found, why?
    – Teifi
    Jan 21, 2013 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, 2013 at 16:33
  • New terminals were not enough. Ended up rebooting for another reason and now it works. Probably log-out/in.
    – deed02392
    Jul 8, 2014 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, 2017 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, 2019 at 14:03
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.

3
  • 3
    This is also referred to in the ubuntu documentation on RootSudo albeit they refer to the admin group instead of the sudo group
    – icc97
    Mar 5, 2012 at 8:27
  • 10
    +1: this is the most easy-to-remember, simple and intuitive way to add a user to a group! Dec 28, 2012 at 17:14
  • 1
    This gives me an error saying Usage: adduser [options] LOGIN\n adduser -D\n adduser -D [options]\n
    – akki
    May 25, 2019 at 13:58
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

3
  • 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, 2013 at 9:49
  • Did you click the 'unlock' button?
    – daboross
    Jun 10, 2013 at 2: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, 2017 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

2
4

sudo gpasswd -a $USER sudo

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.

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.
1
  • 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, 2023 at 17:57
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.

You must log in to answer this question.

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