How to create a new sudo user on Ubuntu Linux server
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
OR
$ sudo grep -B1 -i ^%sudo /etc/sudoers
Sample outputs:
# 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?
Open the terminal or login to your remote server:
$ ssh root@server-name-IP-here
$ ssh root@server1.cyberciti.biz
{ root@server:/root} #
How to add the user named vivek
# adduser vivek
OR
$ sudo adduser vivek
Sample outputs:
Fig.01: How to add the new user on Ubuntu
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
OR
$ sudo usermod -aG sudo vivek
OR
$ sudo adduser vivek sudo
Sample outputs:
Fig.02: Add user vivek to sudo to get administrator privileges
How to print user account info
Verify new user and group membership with the [nicmd name=”id”]:
$ id vivek
Sample outputs:
Fig.03: Show user and group information
How to test sudo access for vivek user account
Now user vivek can login using the ssh command command as follows:
$ ssh vivek@server1.cyberciti.biz
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
Sample outputs:
Fig.03: Testing sudo access for vivek user account
And there you have it. Allowing other users to run sudo on Ubuntu server and granting users administrator privileges.