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
Warning: 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 alex@203.0.113.25
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.
