How do I grant sudo privileges to an existing user?

813,287

Solution 1

You need to add the user hduser to the sudo group (which is the "administrators" group in Ubuntu).

If you have already created the user, you can add the user to the sudo group by running the following command in a Terminal:

sudo usermod -a -G sudo hduser

Solution 2

Instead you can try,

sudo adduser hduser sudo

In Ubuntu you need to add the user only to the group sudo.

Solution 3

1) Become root. You can do this using sudo -i or becoming root the old fashioned way su -

2) Run visudo

3) I changed this portion of the sudoers file to have my chosen users become sudo users, and you can add users similarly (blank lines introduce to format cleanly):

## User Aliases
## These aren't often necessary, as you can use regular groups
## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname
## rather than USERALIAS
# User_Alias ADMINS = jsmith, mikem dbadmin 
ALL=(ALL) ALL
ics ALL=(ALL) ALL 
csm ALL=(ALL) ALL 
coa ALL=(ALL) ALL

4) Press : and x to write the changes to sudoers and exit vi.

Solution 4

Edit the sudoers file: sudo visudo

and add:

user    ALL=(ALL:ALL) ALL
Share:
813,287

Related videos on Youtube

saket
Author by

saket

Updated on September 18, 2022

Comments

  • saket
    saket over 1 year

    I want to grant a newly created user sudo privileges in Ubuntu.

    I tried

    sudo adduser hduser admin
    

    but it says no admin group exists. How can I do it?

    • Mitch
      Mitch almost 12 years
      What is the output of sudo visudo?
    • Adrian
      Adrian almost 12 years
      This was changed recently, which is the reason for the confusion.
    • Jamess
      Jamess over 11 years
      sudo adduser <username> sudo - did the trick for me. Second sudo is the group name instead of sudo
  • Jacob Foshee
    Jacob Foshee almost 11 years
    Note that the user must log off, then back on again for this change to take effect.
  • Dan Dascalescu
    Dan Dascalescu almost 10 years
    Why bother editing /etc/sudoers when the adduser command will work?
  • Dan Dascalescu
    Dan Dascalescu almost 10 years
    This seems needlessly complicated. Why bother running visudo when the adduser command will do what you want?
  • JBB
    JBB over 8 years
    @dan-dascalescu - $ sudo adduser username sudo won't work if the 'sudo' group does not exist. So adding the user manually is certainly an option. However, it's certainly more elegant to create a sudo group, add it to the sudoers file (e.g. via visudo), then add the appropriate user(s) to that user group.
  • Ashish Ratan
    Ashish Ratan over 8 years
    not working ubuntu 14
  • Ashish Ratan
    Ashish Ratan over 8 years
    Usage: usermod [options] LOGIN Options: -c, --comment COMMENT new value of the GECOS field -d, --home HOME_DIR new home directory for the user account -e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE -f, --inactive INACTIVE set password inactive after expiration to INACTIVE -g, --gid GROUP force use GROUP as new primary group -G, --groups GROUPS new list of supplementary GROUPS -a, --append append the user to the supplemental GROUPS
  • Marcel
    Marcel about 8 years
    @JarrettBarnett That was the case for me. Thank you for your answer.
  • vivekyad4v
    vivekyad4v over 7 years
    Yes , @JacobFoshee is right - "user must log off, then back on again for this change to take effect."
  • Jalal Sordo
    Jalal Sordo over 7 years
    still if i use visudo on the user i just added to sudo I will get permission denied, why is that ?
  • d a i s y
    d a i s y about 7 years
    @Zanna You have edited sudo visudo /etc/sudoers Is it correct? AFAIK, it is either sudo visudo or sudo nano /etc/sudoers
  • Zanna
    Zanna about 7 years
    ah @Lnux you're right, but you should always use visudo to edit /etc/sudoers because it stops you from making a fatal syntax error. I corrected
  • MrSmith42
    MrSmith42 about 7 years
    No problem here using ubuntu 16.
  • Nullpointer
    Nullpointer about 7 years
    How do i remove sudo access from existing user ?
  • jeffmcneill
    jeffmcneill almost 7 years
    FYI, In RHEL/CentOS it is: sudo usermod -aG wheel hduser
  • The Fool
    The Fool over 3 years
    @DanDascalescu maybe because for some people it doesnt work?