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

  1. First add the user, run: sudo adduser <UserNameHere>
  2. Add the user to sudo group by typing the command in terminal for Ubuntu version 12.04 and above: sudo adduser <UserNameHere> sudo
  3. In older version of Ubuntu (version 12.04 and older), run: sudo adduser <UserNameHere> admin
  4. 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
Sample outputs:

# Allow members of group sudo to execute any command

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@server:/root} #

How to add the user named vivek

# adduser vivek
$ 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
$ sudo usermod -aG sudo vivek
$ 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
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.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...