How to Create a Sudo User on Ubuntu

I wrote this post because I wanted to simplify the setup of sudo users, and because I always have to look up the exact commands too.
There we go:

Prerequisites: Log into the system as root either via ssh or locally. In this example I use ssh, replace the ip address 192.168.0.xx by your server address

 ssh root@192.168.0.xx

1. Create the user

Now let’s create the user foo.

adduser foo

In the next step you’ll need to set a password for your new user foo in the upcoming prompt.

Adding user `foo' ...
Adding new group `foo' (1001) ...
Adding new user `foo' (1001) with group `foo' ...
Creating home directory `/home/foo' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully

Subsequently you may set additional information for the user foo. Confirm by entering Y(es).

Changing the user information for foo
Enter the new value, or press ENTER for the default
    Full Name []:
    Room Number []:
    Work Phone []:
    Home Phone []:
    Other []:
Is the information correct? [Y/n]

2. Add the user to the sudo group

usermod -aG sudo foo

Once you have added the account to the sudoers, you can test if it worked properly.

3. Check if sudo user was configured correctly

Switch to the user

su - username

Confirm that the output shows root when entering:

sudo whoami

Now the user foo can use sudo commands e.g. sudo apt install package_name

Leave a Reply

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