NewHalo Edge 3 is here: HTTP/3 everywhere and 42 edge locations.Learn more
Docs / Getting started with your VPS

Create a non-root user with sudo

Getting started with your VPS6 min readUbuntu 24.04 LTS

Working as root for everything is risky, because one typo can damage the whole system. In this guide you will create a regular user who can run administrative commands with sudo only when needed.

Before you begin

  • An SSH session on your new Ubuntu 24.04 LTS server, logged in as root. See the SSH connection guide if you have not connected yet.
  • A username you like. This guide uses alex; choose your own, using lowercase letters and numbers only.
  • A strong password for the new account, which you will type when using sudo.

Step 1: Create the user

On Ubuntu, adduser is the friendly way to create an account. It creates a home folder, a matching group and asks you a few questions:

adduser alex

Enter a password when asked, then press Enter to skip the optional fields such as full name and room number. Expected output looks like this:

Adding user `alex' ...
Adding new group `alex' (1001) ...
Adding new user `alex' (1001) with group `alex' ...
Creating home directory `/home/alex' ...
New password:
Retype new password:
passwd: password updated successfully

Step 2: Give the user sudo rights

On Ubuntu, members of the sudo group may run administrative commands. The usermod command adds the user to that group. The -aG flags mean "append to the group list," which keeps the user's existing groups intact:

usermod -aG sudo alex
groups alex

The groups command lists the groups the user belongs to. You should see sudo in the output:

alex : alex sudo users

Step 3: Test the new account in a second session

Do not close your root session yet. Open a second terminal on your computer and log in as the new user:

ssh alex@203.0.113.10

Next, check that sudo works. The first time, it asks for the password of alex, not root's password:

sudo whoami

The output should be root. That shows the command ran with administrative rights. sudo remembers your password for a short time (about 15 minutes), so you will not be asked again immediately. To refresh the timer manually, run sudo -v.

[sudo] password for alex:
root

Step 4: Understand the difference between su and sudo

The sudo command runs a single command with elevated rights, and asks for your own password. The su command switches to another user entirely. If you ever need a full root shell from your own account, you can run sudo -i. Use it sparingly: the idea is to run admin commands one at a time, deliberately.

Tip: Keep a root session open until your new account is confirmed working in a second terminal. If something goes wrong, the open session lets you fix it.
Careful: Do not delete the root account or lock it yet. The next guide, on SSH hardening, disables direct root logins over SSH, which is safe only after your sudo user is working.

Troubleshooting

  • "alex is not in the sudoers file. This incident will be reported." The user is not in the sudo group. Log in as root and run usermod -aG sudo alex, then log out and back in, because group changes apply at login.
  • Forgot the password for the new user. Log in as root and run passwd alex to set a new one.
  • "Sorry, try again" when typing the password. Check Caps Lock and keyboard layout. Passwords are case-sensitive, and nothing shows on screen while you type.
  • Mistyped the username. As root, run deluser alex to remove the account, then start again. Add --remove-home only if you are sure you want to delete its files.

Next steps

Your everyday account is ready. Next, set up SSH key authentication so this account logs in with a key instead of a password, which is both faster and much harder to attack.

Something not working? Chat or email our support team and we will help you get unstuck.

Contact support