How to Create Sudo Users and Manage Permissions on a VPS

Running everything as root on a VPS is convenient until one typo deletes the wrong directory. A dedicated non-root user with sudo access gives you a safety layer: you work as a normal user day to day, and you type sudo only when a command needs elevated rights. It also creates an audit trail of exactly which commands ran with root privileges, which matters the moment you share server access with anyone else.

This guide covers creating sudo users, granting and restricting permissions, and locking down SSH so your VPS stays usable and secure. Every command was tested on Ubuntu 22.04 and 24.04; the same steps apply to Debian with minor differences noted below.

Why You Need a Non-Root Sudo User

Most VPS providers hand you root credentials by default. Logging in and working as root all day means there is no confirmation step before destructive commands, and a compromised SSH key gives an attacker unrestricted access immediately.

A sudo user solves both problems. Normal commands run unprivileged, sudo prompts for your password before each elevated action, and you can revoke a single user’s access without changing root’s password. On a shared server you can also see who did what in the auth log, because sudo records every privileged command.

Step 1: Create the New User

Log in to your VPS as root over SSH, then create the user with adduser. On Debian-based systems adduser is the interactive, recommended wrapper; it creates the home directory, copies skeleton files, and prompts for a password in one step.

adduser deploy

Replace deploy with the username you want. You will be asked to set a password and optionally fill in user details. The password is important: sudo asks for it, and SSH password login uses it too.

To create the user without interactive prompts, for example inside a script:

adduser --disabled-password --gecos "" deploy
passwd deploy

On RHEL-based systems (CentOS, AlmaLinux, Rocky) adduser is just a symlink to useradd, so the same command works but behaves non-interactively: you must set the password with passwd afterward.

Step 2: Grant Sudo Access

On Ubuntu and Debian, members of the sudo group can run any command as root after entering their own password. Adding the user to that group grants full sudo access:

usermod -aG sudo deploy

The -aG flags matter: -G sets the list of supplementary groups, and -a appends to it. Without -a, usermod -G sudo deploy would remove the user from every other group they belong to. This is the most common permission-management mistake on new servers.

On RHEL-based systems the equivalent group is wheel:

usermod -aG wheel deploy

Verify the group membership with:

groups deploy

Step 3: Test That Sudo Works

Log out of root, log back in as the new user, and run a harmless privileged command:

ssh deploy@YOUR_VPS_IP
sudo whoami

The first sudo asks for your password. If the output is root, sudo is working. Run one more check to confirm you can actually escalate:

sudo -i
whoami
exit

sudo -i opens a root login shell, and exit drops you back to the normal user. Do your daily work as the user and reserve the root shell for real maintenance.

Step 4: Manage Fine-Grained Permissions with visudo

Group membership gives all-or-nothing sudo access. If a user should only run specific commands, for example restarting Nginx, edit the sudoers rules with visudo. It validates the syntax before saving, which keeps a typo from locking everyone out of sudo.

visudo

Add a line like this at the bottom:

deploy ALL=(ALL) /usr/bin/systemctl restart nginx, /usr/bin/systemctl reload nginx

This lets deploy restart and reload Nginx, and nothing else. Always use full paths to the binaries; sudo matches on the exact path. To let the user run one specific command without being prompted for a password:

deploy ALL=(ALL) NOPASSWD: /usr/bin/systemctl reload nginx

Use NOPASSWD sparingly. It is handy for automated deployment scripts, but it removes the password checkpoint that makes sudo safer than a permanent root shell. A cleaner alternative for scripts is placing one-off rules in a file under /etc/sudoers.d/, for example:

echo "deploy ALL=(ALL) NOPASSWD: /usr/bin/systemctl reload nginx" | sudo tee /etc/sudoers.d/deploy-nginx
sudo chmod 440 /etc/sudoers.d/deploy-nginx

Files in /etc/sudoers.d/ must have 0440 permissions or sudo ignores them. You can test any sudoers change without risking a lockout: open a second SSH session before logging out of the current one, and verify sudo -l lists the expected privileges.

Step 5: Harden SSH for the New User

Once the sudo user works, disable direct root login so attackers have one fewer account to target. Edit the SSH daemon configuration:

sudo nano /etc/ssh/sshd_config

Set this line:

PermitRootLogin no

While you are there, confirm these two lines are present for key-based auth with password fallback:

PubkeyAuthentication yes
PasswordAuthentication yes

Reload SSH to apply the change:

sudo systemctl reload sshd

Copy your public key into the new user’s authorized_keys before you rely on key login:

ssh-copy-id deploy@YOUR_VPS_IP

Do not set PasswordAuthentication no until you have verified that SSH key login works as the new user. Changing both settings at once is the fastest way to lock yourself out of a VPS. Those keys are also what you will use to manage self-hosted services later, from a Vaultwarden password vault to a Jellyfin media server.

Step 6: Revoke Access and Remove Users

To demote a sudo user back to a normal account, remove them from the group. On Debian-based systems:

sudo gpasswd -d deploy sudo

On RHEL-based systems use the group wheel instead. To delete the user entirely, including their home directory and mail spool:

sudo deluser --remove-home deploy

On RHEL-based systems:

sudo userdel -r deploy

Deleting a user does not delete files they owned outside their home directory; find them with sudo find / -user deploy and reassign ownership with chown or delete them.

Quick Reference: Common User Management Commands

  • adduser deploy: create a new user with home directory
  • passwd deploy: set or change a user’s password
  • usermod -aG sudo deploy: grant sudo access (Debian/Ubuntu)
  • usermod -aG wheel deploy: grant sudo access (RHEL family)
  • groups deploy: list the groups a user belongs to
  • getent group sudo: list all users with sudo access
  • sudo -l: show your own allowed sudo commands
  • sudo deluser deploy: demote a user (Debian/Ubuntu)
  • sudo deluser --remove-home deploy: delete user and home directory
  • lastlog -u deploy: show a user’s most recent login

Frequently Asked Questions

What is the difference between root and a sudo user?

Root is the administrator account with unlimited privileges; every command runs elevated, which makes mistakes catastrophic. A sudo user is a normal account that can run specific commands with root rights by typing sudo and entering a password. Best practice is to work as a sudo user and touch the root account as little as possible.

What is the difference between adduser and useradd?

On Debian and Ubuntu, adduser is a friendlier wrapper around useradd: it creates the home directory, sets up default config files, and prompts for a password interactively. useradd is the lower-level tool and does none of that by default, so on these systems prefer adduser unless you need scripting-level control.

How do I see which users have sudo access?

Run getent group sudo on Debian-based systems or getent group wheel on RHEL-based ones. The output lists every account in the group. Audit this list whenever someone leaves your team or a project ends.

I forgot to use -a with usermod and lost my groups. How do I fix it?

Log in as root (or boot into recovery if you locked root out), then re-add the groups explicitly: usermod -aG sudo,adm,cdrom,dialout,plugdev deploy. This is why keeping one active root SSH session open while testing permission changes is worth the effort.

Next Steps

With a sudo user in place and root SSH disabled, your VPS has a basic but solid access layer. Keep the number of sudo accounts small, audit the sudo group regularly, and store each user’s SSH public key rather than relying on passwords alone. For a production server, the follow-up worth doing this week is setting up key-only SSH login and installing a login monitor like Fail2ban to rate-limit brute-force attempts. If this is your first server, it also helps to understand what a VPS is and how it works under the hood.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.