Create a sudo user on Ubuntu, Debian, AlmaLinux and Rocky Linux
Add a personal admin account with sudo rights, give it your SSH key, and test it properly before you stop logging in as root.
- Beginner
- Linux VPS
- 3 min read
Logging in as root for everyday work means every typo runs with full power. A personal user with sudo rights asks for confirmation before admin commands and leaves a clear record in the logs of what was run.
Replace alex in the commands below with the username you want.
1. Create the user
On Ubuntu and Debian, adduser asks for a password and optional details (press Enter to skip them):
adduser alexOn AlmaLinux and Rocky Linux, adduser does not prompt, so set the password separately:
adduser alex
passwd alex2. Grant sudo rights
Ubuntu and Debian use the sudo group:
usermod -aG sudo alexAlmaLinux and Rocky Linux use the wheel group:
usermod -aG wheel alexCheck the result with groups alex.
3. Copy your SSH key to the new user
If you already log in as root with an SSH key, copy it across with the right owner and permissions:
rsync --archive --chown=alex:alex ~/.ssh /home/alexNo key yet? Follow Set up SSH keys and add the key for alex instead of root.
4. Test before you log out
Keep your root session open until the new user is fully tested. Closing it too early is the most common way to get locked out.
Open a second terminal on your computer and run:
ssh [email protected]
sudo whoamisudo asks for alex's password (not root's), then prints root. If it does, the account is ready.
Run admin commands as your user
Put sudo in front of any command that needs root:
sudo apt update
sudo systemctl restart nginxFor a longer admin session, sudo -i opens a root shell. Type exit to leave it.
Passwordless sudo: think twice
You can let a user run sudo without a password, but if that account's key is ever stolen, the attacker gets root instantly. If you need it for automation, give it to a dedicated user and limit it with a rule edited through visudo, which checks the syntax before saving:
sudo visudo -f /etc/sudoers.d/deploydeploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myappTroubleshooting
- "alex is not in the sudoers file": the group change did not apply. Re-run step 2, then log out and back in, because group membership is read at login.
- Permission denied (publickey): check that
/home/alex/.sshis owned byalex, the folder is700andauthorized_keysis600. - Locked out: use the browser console from Open Control Panel on your service page and log in as root there.
Next steps
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Security
Check the Windows Firewall rule for RDP port 12230
Find and verify the inbound TCP 12230 rule that lets Remote Desktop reach your RunRacks server, recreate it if it was deleted and scope it without locking yourself out.
- Intermediate
- Windows VPS · Admin RDP
- 5 min read
Security
Linux Security: Using aide for File Integrity Monitoring on VPS
AIDE builds a checksum database of your system files and reports anything that changed. This guide installs it, creates a baseline, tunes the rules and schedules daily checks on a Linux VPS.
- Advanced
- Linux VPS
- 6 min read
Security
iptables basics: list, allow, drop and save rules
Learn how iptables chains and rule order work, then build a small ruleset that allows SSH and web traffic, drops everything else and can be edited by line number.
- Intermediate
- Linux VPS
- 6 min read