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.
- Updated Nov 27, 2026
- Markdown
Create a non-root user with sudo rights and log in as that user with your SSH key.
- 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):
adduser deploy
usermod -aG sudo deployadduser 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#
rsync --archive --chown=deploy:deploy ~/.ssh /home/deployThis 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:
ssh deploy@203.0.113.10
sudo whoamiWhat 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 deployas 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
sudowhile logged in, the current session does not see it. Log out and in again, or runnewgrp sudoin a shell. - Always use
-aG, not-G.usermod -G sudo deployreplaces all other supplementary groups, which can remove access you needed, such as thedockergroup later. - A user created by
useraddhas no home folder by default. Useadduseron Debian and Ubuntu, which does the setup.useraddneeds-mand a shell. - Editing
/etc/sudoersdirectly can break sudo. A syntax error stops sudo for everyone. If you must change it, usesudo visudo, which checks the file before saving. - A different distribution uses a different group. On some systems admins are in the
wheelgroup, notsudo. 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#
| Symptom | Likely cause | Fix |
|---|---|---|
Permission denied (publickey) for deploy | Key not copied, or wrong owner on .ssh | Run ls -la /home/deploy/.ssh and fix with chown -R deploy:deploy /home/deploy/.ssh |
deploy is not in the sudoers file | The user is not in the sudo group | As root run usermod -aG sudo deploy, then log in again |
sudo: a password is required in a script | Scripts cannot answer the prompt | Run interactively, or set a narrow rule with visudo for that one command |
adduser: The user 'deploy' already exists | The name is taken | Pick another name or reuse the account |
sudo: command not found | A minimal image without sudo | As root run apt install sudo |
New user cannot read a file in /root | /root is private to root | Move shared files to a path the user owns |
sudo: unable to resolve host warning | The hostname is missing from /etc/hosts | Add the hostname to the 127.0.1.1 line, or ignore if sudo still works |
Sorry, try again. after typing the password | Wrong password or caps lock | Retype it, or reset with passwd deploy as root |
| Locked out after editing sudoers | A syntax error in /etc/sudoers | Use 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#
| Task | Command |
|---|---|
| Create a user | adduser deploy |
| Add to sudo group | usermod -aG sudo deploy |
| Show groups | id deploy |
| Test sudo | sudo whoami |
| Edit sudoers safely | sudo visudo |
| Set a password | sudo passwd deploy |
Security and safety notes#
- Keep your root session open until the new user logs in and
sudo whoamiworks. - 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.
Related lessons#
- Log in to a server with SSH keys: the key you copy over
- Initial server setup checklist: the full first hour
- Linux file permissions explained: why ownership of .ssh matters
- Disable SSH password login: the next hardening step
Next#
Try it: Create a sudo user
Add the deploy user, give it sudo rights and copy your key to it.
Want a real machine? Free browser labs: Killercoda
Check what you learned
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.
Keep going: guides and tutorials
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.