I am a new Ubuntu Linux 16.04.xx LTS server user. How do I create a new sudo user on my server? How do I add a new user as sudoer file using the command line option on Ubuntu?
In Linux (and Unix in general), there is a SuperUser named root. The root user can do anything and everything, and thus doing daily work as the root can be very dangerous. You could type a command incorrectly and destroy the system. The sudo command allows a permitted user to run a command as the superuser (root user) or another user, as specified by the security policy. Often the sudo used on servers to give admin permissions and privileges to ordinary users. In this quick tutorial, you will learn how to create a sudo user on Ubuntu.
Steps to create a new sudo user on Ubuntu
- First add the user, run: sudo adduser <UserNameHere>
- Add the user to sudo group by typing the command in terminal for Ubuntu version 12.04 and above: sudo adduser <UserNameHere> sudo
- In older version of Ubuntu (version 12.04 and older), run: sudo adduser <UserNameHere> admin
- Verify it: id <UserNameHere>
More about admin and sudo group on Ubuntu server
The members of the admin group may gain root privileges. All members of group sudo run any command on Ubuntu server. So just add the user to the sudo group on Ubuntu server. The admin group has been deprecated since Ubuntu version 12.04 and above. Hence, no longer exists or used in Ubuntu 12.04 or above. The reason it works:
# grep -B1 -i '^%sudo' /etc/sudoers
$ sudo grep -B1 -i '^%sudo' /etc/sudoers
# Allow members of group sudo to execute any command %sudo ALL=(ALL:ALL) ALL
Let us see some practical examples.
How to add a new user named vivek to sudo using the command line?
How to add the user named vivek
# adduser vivek
$ sudo adduser vivek
How to create a sudo user on Ubuntu for vivek account
Type the following command:
# adduser vivek sudo
OR use the usermod command to add user to group on Linux:
# usermod -aG sudo vivek
$ sudo usermod -aG sudo vivek
$ sudo adduser vivek sudo
How to print user account info
Verify new user and group membership with the [nicmd name=”id”]:
$ id vivek
How to test sudo access for vivek user account
Now user vivek can login using the ssh command command as follows:
$ ssh [email protected]
Verify that vivek can use sudo command:
$ sudo cat /etc/sudoers
The first time you use sudo command, you will be asked for the password of the vivek account. So type the vivek’s password to gain root access. Any command type with sudo should run with root privileges for vivek account. To gain root shell, enter:
$ sudo -s
And there you have it. Allowing other users to run sudo on Ubuntu server and granting users administrator privileges.