How to Create Users and Manage Their Sudo Privileges on Ubuntu


create sudo user

The sudo command allows normal users to run programs which are only available to the root user. In this tutorial, we will show you how to create a system user and manage its sudo privileges on a Linux VPS running Ubuntu 16.04 as an operating system.

First of all, connect to your Linux server via SSH and add a new system user:

# adduser newuser

Feel free to replace newuser with the name of the system user you want to add. You will need to enter a password for your new user as well as you will need to enter some random user information. You can generate a strong password through the command line.

# adduser newuser
Adding user `newuser' ...
Adding new group `newuser' (1001) ...
Adding new user `newuser' (1001) with group `newuser' ...
Creating home directory `/home/newuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for newuser
Enter the new value, or press ENTER for the default
        Full Name []: New User
        Room Number []: 101
        Work Phone []: 123456
        Home Phone []: 123456
        Other []:
Is the information correct? [Y/n] y

Add sudo privileges to a system user in Linux

Once the system user is created go ahead and add it to the sudo group using the following command:

# usermod -aG sudo newuser

Then, use the su command to switch to the new user:

# su - newuser

Now you can try to run some command or program that is available only to the root user. For example, try to update the package index and to install the newest versions of all packages that are currently installed on your server.

sudo apt-get update
sudo apt-get upgrade

In case the sudo command is not installed on your server you will get the following error:

-su: sudo: command not found

This means you will need to install the sudo program in order to be able to run that command. Switch to your root account and run the following command:

# apt-get install sudo

You can verify that it is successfully installed using this command:

# dpkg -l | grep -i sudo

The output should be similar to the one below:

# dpkg -l | grep -i sudo
ii  sudo                          1.8.16-0ubuntu1.3                     amd64        Provide limited super user privileges to specific users

Once the sudo program is installed on your Ubuntu VPS, switch to the new user and try to run the same commands again. You should not see the error message now and instead, you will be prompted to enter the password of the new system user.

Remove sudo privileges for a system user in Linux

In some cases, you may want to remove the sudo privileges for a specific system user. For that purpose you can use this command:

# deluser newuser sudo

Feel free to replace newuser with the name of the system user you want to modify. If you get the following message that means the system user has been successfully removed from the sudo group.

# deluser newuser sudo
Removing user `newuser' from group `sudo' ...