Create a non-root user with sudo
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.
Troubleshooting
- "alex is not in the sudoers file. This incident will be reported." The user is not in the
sudogroup. Log in as root and runusermod -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 alexto 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 alexto remove the account, then start again. Add--remove-homeonly 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