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

By RunRacks Team · Published

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):

Terminal
adduser alex

On AlmaLinux and Rocky Linux, adduser does not prompt, so set the password separately:

Terminal
adduser alex
passwd alex

2. Grant sudo rights

Ubuntu and Debian use the sudo group:

Terminal
usermod -aG sudo alex

AlmaLinux and Rocky Linux use the wheel group:

Terminal
usermod -aG wheel alex

Check 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:

Terminal
rsync --archive --chown=alex:alex ~/.ssh /home/alex

No key yet? Follow Set up SSH keys and add the key for alex instead of root.

4. Test before you log out

Open a second terminal on your computer and run:

Terminal
ssh [email protected]
sudo whoami

sudo 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:

Terminal
sudo apt update
sudo systemctl restart nginx

For 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:

Terminal
sudo visudo -f /etc/sudoers.d/deploy
Text
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp

Troubleshooting

  • "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/.ssh is owned by alex, the folder is 700 and authorized_keys is 600.
  • 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.

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

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