Create a Sudo User on Ubuntu

One of the best ways to ensure security while doing administrative work on your server is to run as a non-root user. After successfully purchasing a VPS from Hostry, the first task as root should be to set up a non-root user with a valid sudo.
This guide is applicable for Ubuntu distribution, for versions: 20.04 LTS, 18.04 LTS, 16.04 LTS.

Add a New User Account

To create a user account, you need to use the adduser command. We recommend using a strong password for the user! An example of a strong password is aoQ7w52!OKvA$Pfz. Next, you can enter values ​​for user information, or press ENTER to leave these fields blank.

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

Add User to Sudo Group

You can add a new user to the sudo group using the usermod command

# usermod -aG sudo example_user

Test

Switch to a New user

# su - example_user

Make sure you are the new whoami user, then test sudo access with sudo whoami which should return root

$ whoami
example_user
$ sudo whoami
[sudo] password for example_user:
root

Done!

Was this article helpful?

Related Articles

Leave A Comment?