Chapter 1 · Lesson 3 of 19
Server basics

Create a non-root sudo user

A non-root sudo user does daily work with limited rights and uses sudo only for admin commands, which limits the damage a mistake or a stolen session can do.

By the end you will

Create a non-root user with sudo rights and log in as that user with your SSH key.

Time
12 min
Level
Beginner
Check
4 questions
You need
  • A fresh Ubuntu or Debian server with root SSH access
  • Your SSH key already on the server

Root can do anything, including deleting the whole system with one typo, and every attacker who finds a way in wants root. A normal user with sudo adds a speed bump: risky commands need an explicit sudo, and each one is logged. You will also find that many tools and deploy scripts expect a regular user.

How it works#

Every Linux user has an account with a user ID, a home folder and a set of groups. Root is user ID 0 and the kernel skips most permission checks for it. A normal user is limited to its own files and whatever groups allow.

sudo is a program that runs one command as another user, root by default. It first checks a policy file, /etc/sudoers. On Ubuntu and Debian that file lets members of the sudo group run anything, after the user types their own password. So giving someone admin rights means adding them to a group, not sharing the root password.

The log is useful. Each sudo use is recorded in the system log with the user and the command. If a mistake or an intrusion happens, you can see who ran what.

SSH keys belong to an account. Your root key does not work for the new user, which is why step 2 copies it across, with the right owner so sshd accepts it.

A worked example: you log in as root and run adduser deploy. The system creates a user, a group named deploy and /home/deploy. You run usermod -aG sudo deploy, which adds the user to the sudo group. You copy your key across. From your laptop you run ssh deploy@203.0.113.10, then sudo apt update. sudo asks for the password you set, checks /etc/sudoers, finds that the sudo group is allowed and runs the command as root.

Step 1: Create the user#

Logged in as root, create a user called deploy (pick any name):

bash
adduser deploy
usermod -aG sudo deploy

adduser asks for a password and a few optional details such as a full name. You can press Enter to skip the details. The second command adds the user to the sudo group. In -aG, -G names the group and -a means append, so existing groups are kept.

What you should see: lines such as Adding new user 'deploy' and Creating home directory '/home/deploy', then password prompts. usermod prints nothing when it works.

Step 2: Give the user your SSH key#

bash
rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy

This copies root’s .ssh folder, including authorized_keys, to the new user. --archive keeps permissions and --chown sets the new owner. Without the right owner and permissions, sshd ignores the key.

What you should see: no output. Run ls -la /home/deploy/.ssh to confirm authorized_keys is there, owned by deploy.

Step 3: Log in as the new user#

In a new terminal on your own computer:

bash
ssh deploy@203.0.113.10
sudo whoami

What you should see: you are logged in without a key password prompt, then [sudo] password for deploy:. After you type the password, the second command prints root. If it does, the user can administer the server.

Keep your root session open until this works. When it does, you can stop using root for daily work.

Check it worked#

Run id as deploy. The output should list sudo among the groups. Then run sudo whoami again: it must print root.

Edge cases and gotchas#

  • Typing the password for sudo is normal. SSH key login does not set a password for sudo. If you skipped it, run passwd deploy as root. Some providers set up passwordless sudo for a default user, which is convenient but means a stolen session has full rights.
  • Group changes apply at the next login. If you add yourself to sudo while logged in, the current session does not see it. Log out and in again, or run newgrp sudo in a shell.
  • Always use -aG, not -G. usermod -G sudo deploy replaces all other supplementary groups, which can remove access you needed, such as the docker group later.
  • A user created by useradd has no home folder by default. Use adduser on Debian and Ubuntu, which does the setup. useradd needs -m and a shell.
  • Editing /etc/sudoers directly can break sudo. A syntax error stops sudo for everyone. If you must change it, use sudo visudo, which checks the file before saving.
  • A different distribution uses a different group. On some systems admins are in the wheel group, not sudo. Check the distribution docs.
  • Disabling root login comes after this works. Once the new user logs in and sudo works, you can set PermitRootLogin no, as covered in the initial server setup checklist. Do it in the same careful way: second session first.
  • Usernames are case sensitive and have limits. Stick to lowercase letters, digits and hyphens to avoid surprises in scripts.
  • A service account is not a login account. Programs such as web servers run as their own users with no password. Do not give those users sudo.

Troubleshooting#

SymptomLikely causeFix
Permission denied (publickey) for deployKey not copied, or wrong owner on .sshRun ls -la /home/deploy/.ssh and fix with chown -R deploy:deploy /home/deploy/.ssh
deploy is not in the sudoers fileThe user is not in the sudo groupAs root run usermod -aG sudo deploy, then log in again
sudo: a password is required in a scriptScripts cannot answer the promptRun interactively, or set a narrow rule with visudo for that one command
adduser: The user 'deploy' already existsThe name is takenPick another name or reuse the account
sudo: command not foundA minimal image without sudoAs root run apt install sudo
New user cannot read a file in /root/root is private to rootMove shared files to a path the user owns
sudo: unable to resolve host warningThe hostname is missing from /etc/hostsAdd the hostname to the 127.0.1.1 line, or ignore if sudo still works
Sorry, try again. after typing the passwordWrong password or caps lockRetype it, or reset with passwd deploy as root
Locked out after editing sudoersA syntax error in /etc/sudoersUse the provider console as root and fix it with visudo

Real-world scenario#

Sam rents a server and does everything as root for a week. One evening he types a cleanup command with a stray space in the path, and a folder he needed disappears. Nothing asked him to confirm, because root never gets asked.

He rebuilds, and this time the first thing he does is create a deploy user, add it to the sudo group, copy his key and test sudo whoami in a second terminal. Daily work happens as deploy. When a command needs admin rights, he types sudo and thinks for a second about whether it should.

Two months later a script goes wrong again, but it runs as deploy and cannot touch system files. The damage is limited to his own home folder.

Quick reference#

TaskCommand
Create a useradduser deploy
Add to sudo groupusermod -aG sudo deploy
Show groupsid deploy
Test sudosudo whoami
Edit sudoers safelysudo visudo
Set a passwordsudo passwd deploy

Security and safety notes#

  • Keep your root session open until the new user logs in and sudo whoami works.
  • Do not use a weak password for a sudo user. It protects every admin action.
  • Do not give a user passwordless sudo for everything unless you accept that anyone who gets into that account gets root.

Next#

Set up a firewall with ufw.

Try it: Create a sudo user

Add the deploy user, give it sudo rights and copy your key to it.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda

Check what you learned4 questions
1Which command gives the user deploy sudo rights?
2Why test the new user in a second terminal before closing your root session?
3What does sudo whoami print for a user with sudo rights?
4You ran usermod -G sudo deploy without -a and deploy lost other groups. Why?

Frequently asked questions

Why should I stop using root on a server?

Root can change or delete anything with no confirmation. A normal user with sudo makes you type sudo for risky commands and leaves a log of what ran.

Do I still need a password if I log in with a key?

Yes, for sudo. Key login proves who you are to SSH, but sudo asks for the user's password by default. Set a strong one with adduser.

Can I delete or lock the root account?

Do not delete it. On Ubuntu root usually has no password and you reach it through sudo. Disabling root SSH login is the safer step, covered after your new user works.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios