Add user or group to sudoers file

I had this issue for my Ubuntu and Pidora, so in this article you can have both solutions:

In Ubuntu

The problem I had was to add a user on an Ubuntu 14.04 at my work, so that someone else from the company can help me do some basic maintenance of the system (it was requested by my employee!), so this is how I did it after creating a user (new_user):

 gpasswd -a new_user sudo

This command will add the user new_user to the group sudo.

In Pidora

I was working with Pidora (which is a Fedora remix for Raspberry Pi) and I was about to run a command using sudo that I faced the following error:

[NixSOS@pidora ~]$ sudo iptables -L -n
[sudo] password for NixSOS:
NixSOS is not in the sudoers file. This incident will be reported.

and I was confused for a while and then I searched for a way to add my user in the “sudoers file” and I’m going to explain how to add a user or a group to sudoers file. This should work all major linux distributions.

Before everything I should mention that in order to perform this action we should act as root user and we should be extra careful.

Follow these steps:

  1. Open a terminal (if you haven’t already)
  2. Login as root user:
  3. You will be asked to type root password and you should type it (mind that you may not see the password you are typing, but continue typing and at the end press enter)
  4. Use the following command to edit sudoers file
  5. The sudoers file will open in Vim editor. at this point we should navigate to the end of the file either by arrow keys or by pressing j (you can see the Vim cheatsheet here or here)
  6. Enter the “insert mode” by pressing i from keyboard
  7. Append the following line to add a user in the sudoers file (replace NixSOS with your desired username):
    NixSOS  ALL=(ALL)       ALL
  8. Or append the following line to add a group in the sudoers file (replace NixSOSGroup with your desired group name):
    %NixSOSGroup  ALL=(ALL)       ALL
  9. Press Esc button from keyboard to exit the “insert mode” of Vim
  10. Start type the following without pressing any extra keys and then press enter:
  11. If you have done correctly you will not see any error, but in case of error, you will see the following:

    >>> /etc/sudoers: syntax error near line 101 <<<
    What now?

    and then you should type


    to proceed editing your file again (from step 5 of this tutorial) and correct your mistake.

  12. If everything went correctly and you didn’t see any error, type the following to exit the root user:

Now you should be able to use sudo for your user or group.

Leave a Reply

Your email address will not be published. Required fields are marked *

This is new type of CAPTCHA, make the puzzle please !WordPress CAPTCHA