On this page
- Why work through a sudo account instead of root?
- Is sudo installed on my image?
- How do I create the user on Ubuntu or Debian?
- How do I create the user on Rocky Linux or AlmaLinux?
- How do I give the new user my SSH key?
- How do I test the account before logging out of root?
- How do I write my own sudoers rules safely?
- How do I manage groups, lock or remove users?
- Questions
Why work through a sudo account instead of root?
A personal account with sudo rights gives you the same power as root, but only when you ask for it, and every elevated command lands in the system log with your name next to it. Logging in as root means every typo runs with full privileges, and it also means attackers scanning port 22 can aim their guesses at a user name they already know.
The first SSH session tutorial shows the two line version of this task. This guide goes further: choosing the right tool per distribution, copying keys correctly, writing your own sudoers rules without breaking the file, limiting what a helper account may run, and removing users cleanly when they are no longer needed. Everything below is run as root (or with sudo) on a fresh server deployed from the console.
Is sudo installed on my image?
On Ubuntu 24.04, Ubuntu 22.04, Rocky Linux 9 and AlmaLinux 9 the sudo package is part of the base image. Minimal Debian 12 and 13 installs can come without it, so check first and install it if the command is missing:
command -v sudo || apt install -y sudo
sudo --version | head -n 1On the RHEL family the equivalent is dnf install -y sudo, although you will rarely need it. The version line confirms the binary works. Sudo reads its rules from /etc/sudoers and from every file in /etc/sudoers.d/, and both Debian family systems and RHEL family systems already contain a rule that grants full rights to one group: %sudo on Ubuntu and Debian, %wheel on Rocky and Alma. You can confirm the rule with:
grep -E '^%(sudo|wheel)' /etc/sudoersHow do I create the user on Ubuntu or Debian?
Use adduser, the friendly wrapper that creates the home directory, copies the skeleton files from /etc/skel, sets bash as the shell and asks for a password in one go:
adduser deploy
usermod -aG sudo deploy
id deployThe -aG pair matters: -a appends the group, while -G on its own would replace every secondary group the user already has. The output of id should list 27(sudo) among the groups. Pick a password even if you plan to use keys only, because by default sudo asks for the account password before running anything.
The answers adduser asks for (full name, room number and so on) are optional; press Enter to skip them.
How do I create the user on Rocky Linux or AlmaLinux?
On the RHEL family adduser is only a link to useradd, which does not prompt for anything. Give it the flags you need and set the password in a second step:
useradd -m -s /bin/bash -c "Deploy user" deploy
passwd deploy
usermod -aG wheel deploy
id deploy-m creates /home/deploy, -s sets the login shell and -c stores a comment. Look for 10(wheel) in the output of id. Rocky and Alma ship the line %wheel ALL=(ALL) ALL enabled, so membership is all it takes. On these systems SELinux is enforcing; files you create under the new home keep the right labels automatically, but if you ever move a home directory by hand run restorecon -Rv /home/deploy afterwards.
How do I give the new user my SSH key?
Copy the key that root already trusts, then hand ownership to the new account. If you added a key on the Deploy page it sits in /root/.ssh/authorized_keys:
install -d -m 700 -o deploy -g deploy /home/deploy/.ssh
install -m 600 -o deploy -g deploy /root/.ssh/authorized_keys /home/deploy/.ssh/authorized_keys
ls -la /home/deploy/.sshThe install command creates the directory and the file with the right mode and owner in one step, which avoids the classic mistake of a root owned key file that sshd silently ignores. If root has no key yet, run this from your own computer instead and it appends your public key for you:
ssh-copy-id deploy@198.51.100.24Keys saved on the SSH keys page of the console are only written at deploy time, so for an existing server this manual copy is the way to add them. The SSH keys and hardening tutorial explains how to create a key pair if you do not have one.
How do I test the account before logging out of root?
Keep the root session open and start a new terminal on your computer. Log in as the new user and ask sudo to validate:
ssh deploy@198.51.100.24
sudo -v && echo "sudo is ready"
sudo -l
sudo journalctl -n 5 _COMM=sudosudo -l lists exactly what the account may run, and the journal query shows the log entries your test produced. Only when all of this works should you close the root session. There is no browser console for our instances, so a broken sudo setup combined with disabled root login would leave reinstalling as the only way back in. Once you trust the account, the guide on disabling root login takes the next step.
How do I write my own sudoers rules safely?
Never edit /etc/sudoers with a plain editor. Use visudo, which locks the file and refuses to save a version with a syntax error. Even better, keep your changes in a small drop-in file so package updates never touch them:
sudo visudo -f /etc/sudoers.d/deployA few useful patterns for that file:
# full rights, password required (same as group membership)
deploy ALL=(ALL:ALL) ALL
# a CI account that may only restart one service, no password
ci ALL=(root) NOPASSWD: /usr/bin/systemctl restart app.service
# ask for the password again only after 30 minutes
Defaults:deploy timestamp_timeout=30Check every file before you log out, and keep the mode at 0440:
sudo visudo -c
sudo chmod 0440 /etc/sudoers.d/deployFile names in /etc/sudoers.d/ must not contain a dot or end with a tilde, otherwise sudo skips them without a warning. Be careful with NOPASSWD: ALL: it turns a stolen key into instant root, so keep passwordless rules narrow and tied to full command paths.
How do I manage groups, lock or remove users?
Group membership changes take effect at the next login, so log out and back in after running usermod. These commands cover the day to day work:
getent group sudo wheel # who is an admin
sudo gpasswd -d deploy sudo # take sudo away (Debian family)
sudo gpasswd -d deploy wheel # take sudo away (Rocky, Alma)
sudo usermod -L deploy # lock the password
sudo chage -E 0 deploy # expire the account completely
sudo chage -l deploy # show password and expiry datesTo delete a user and its home directory, check first for running processes and cron entries, then remove it:
sudo pkill -u olduser
sudo deluser --remove-home olduser # Ubuntu, Debian
sudo userdel -r olduser # Rocky, AlmaLocking with usermod -L stops password logins but not key logins, so pair it with chage -E 0 when someone leaves. Remove their sudoers drop-in as well if you created one.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
| Plan | Fits because | Size | Monthly | Order from |
|---|---|---|---|---|
| A1-1 | enough for learning user and sudo administration | 1 vCPU, 1 GB RAM, 25 GB NVMe | $3.60/mo | $43.20 for 12 months |
Questions about Create a sudo user
What is the difference between the sudo and wheel groups?
They do the same job on different families. Ubuntu and Debian grant full sudo rights to the sudo group, while Rocky Linux and AlmaLinux use wheel. Adding a user to a group that has no matching rule does nothing.
Why does sudo say the user is not in the sudoers file?
Usually the group change has not reached the session yet. Log out and back in, then run id. If the group is listed and the error stays, check that the %sudo or %wheel line in /etc/sudoers is not commented out, using sudo visudo from root.
Can I use the same SSH key for root and the new user?
Yes. One key pair can unlock as many accounts as you like, because the public key is simply listed in each authorized_keys file. Many admins keep one personal key and copy it into every account they manage.
Should my admin account use NOPASSWD?
For a person, usually not: the password prompt is a second barrier if your key leaks. For automation, grant NOPASSWD only for the exact commands the job needs, written with full paths in a drop-in file.
How do I see what someone did with sudo?
Every sudo call is logged. Run sudo journalctl _COMM=sudo --since today on any of our Linux images, or read /var/log/auth.log on Debian family systems and /var/log/secure on Rocky and Alma.
Does reinstalling the server keep my users?
No. A reinstall from the Settings tab erases the disk and generates a new root password on the Overview tab, so you create the sudo user again afterwards.
More in Server basics
- Add a swap file
- Change the hostname
- Time zone and time sync
- File and directory permissions
- Choose a Linux distribution
- Upgrade Ubuntu to the next LTS
- Upgrade Debian to a new major release
- Rocky or AlmaLinux to a new major version
- Automatic security updates
- Run an app as a systemd service
- Schedule jobs with cron and systemd timers
- Environment variables on Linux
- Check disk space and find what fills it
- Inspect processes and memory
- Read the hardware and system report
- Reboot and shut down safely
- Customise the login message (MOTD)
- Remote desktop with Xfce and xrdp
- SELinux on Rocky Linux and AlmaLinux
- Keep sessions alive with tmux
- SFTP-only users in a chroot
- apt and dnf package management
- First-hour checklist for a new server
- Upgrade FreeBSD to a new release