NewCode CLOUD — 20% off your first month on all cloud VPSClaim

How to Create a Sudo User on Linux and Stop Using Root

New servers usually hand you a root login. Root can do anything, without asking, and every mistake runs with full power: a mistyped rm -rf, a compromised web script, a malicious package. Working as a regular user and switching to administrator rights only when needed with sudo gives you a pause before dangerous commands, a log of who did what, and a much smaller blast radius when something goes wrong.

This guide shows how to create an admin user, give it sudo, move your SSH key over, and then lock root out of SSH. It also covers the basics of users, groups and permissions you need for running websites and apps.

Root vs sudo at a glance

Logging in as root Regular user + sudo
Every command runs as administrator Yes Only with sudo
Password / confirmation before admin actions No Yes (configurable)
Audit trail of admin commands No Yes, in the auth log
Bots know the username Yes — “root” is tried first No

Step 1: Create the user

Pick a username that is not obvious — avoid admin, test or user, which bots also try.

Ubuntu and Debian

adduser deploy

adduser is interactive: it creates the home directory, copies default shell files and asks for a password. The name and phone fields can be left blank.

AlmaLinux, Rocky Linux and CentOS

useradd -m -s /bin/bash deploy
passwd deploy

-m creates /home/deploy and -s sets the shell.

Even if you plan to log in only with SSH keys, set a strong password — sudo will ask for it.

Step 2: Grant sudo rights

Each distribution family has a group whose members can use sudo:

usermod -aG sudo deploy     # Ubuntu / Debian
usermod -aG wheel deploy    # AlmaLinux / Rocky / CentOS

The -a (append) flag matters. Without it, usermod -G replaces the user’s groups instead of adding one.

On a minimal Debian install, sudo itself may be missing: apt install sudo.

Step 3: Copy your SSH key to the new user

If you already log in as root with a key, copy it across and fix ownership:

mkdir -p /home/deploy/.ssh
cp /root/.ssh/authorized_keys /home/deploy/.ssh/
chown -R deploy:deploy /home/deploy/.ssh
chmod 700 /home/deploy/.ssh
chmod 600 /home/deploy/.ssh/authorized_keys

If you have not set up keys yet, follow SSH key authentication first.

Step 4: Test before locking root out

In a new terminal, keeping the root session open:

ssh deploy@YOUR_SERVER_IP
sudo whoami

sudo whoami should ask for deploy’s password and then print root. If it says “deploy is not in the sudoers file”, the group change did not apply — log out and back in, or check groups deploy.

Step 5: Disable root login over SSH

Once the new user works:

echo "PermitRootLogin no" | sudo tee /etc/ssh/sshd_config.d/20-no-root.conf
sudo sshd -t && sudo systemctl reload ssh    # use "sshd" on AlmaLinux / Rocky

Root still exists and you can still become root with sudo -i; it simply cannot log in over the network. If you ever lock yourself out, the VNC console in the control panel logs in locally and is not affected by SSH settings.

Using sudo day to day

sudo apt update                  # run one command as root
sudo -i                          # open a root shell (exit with "exit")
sudo -u www-data php script.php  # run a command as another user
sudo !!                          # repeat the last command with sudo

By default sudo remembers your password for 15 minutes per terminal. Every sudo command is logged:

sudo grep sudo /var/log/auth.log | tail      # Ubuntu / Debian
sudo grep sudo /var/log/secure | tail        # AlmaLinux / Rocky

Editing sudoers safely

Never edit /etc/sudoers with a normal editor. A syntax error there can break sudo completely. Use visudo, which checks the file before saving, and prefer drop-in files:

sudo visudo -f /etc/sudoers.d/deploy

Examples of useful rules:

# Let deploy run sudo without a password (convenient, less safe — only with key-only SSH)
deploy ALL=(ALL) NOPASSWD:ALL

# Let a CI user restart one service and nothing else
ci ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp

Restrictive rules like the second one are the right way to give automation the access it needs without handing over the whole server.

Users, groups and permissions in two minutes

Every file has an owner, a group and permissions for owner, group and others:

ls -l /var/www/example.com/index.php
-rw-r----- 1 deploy www-data 1532 Nov 19 10:00 index.php
Part Meaning
rw- owner (deploy) can read and write
r-- group (www-data) can read
--- everyone else: nothing

Numeric modes add read (4), write (2) and execute (1): 640 = rw-r-----, 755 = rwxr-xr-x.

A typical web setup:

sudo chown -R deploy:www-data /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 750 {} \;
sudo find /var/www/example.com -type f -exec chmod 640 {} \;
# only the upload/cache folders need to be writable by the web server
sudo chmod -R 770 /var/www/example.com/wp-content/uploads

You edit the code as deploy; Nginx or PHP-FPM, running as www-data, can read it but cannot overwrite it. If a plugin is exploited, the attacker cannot rewrite your PHP files.

Avoid chmod 777. It lets every process on the server modify the file and is the usual first step of a website compromise.

Service accounts

Applications should run as their own unprivileged users, never as root:

sudo useradd --system --no-create-home --shell /usr/sbin/nologin myapp

A system user with nologin cannot be used to log in at all. Point the application’s systemd unit at it with User=myapp — see managing services with systemd.

Removing a user

When a team member leaves:

sudo deluser --remove-home alice     # Ubuntu / Debian
sudo userdel -r alice                # AlmaLinux / Rocky

Also remove any of their keys from other users’ authorized_keys files, and rotate shared secrets (database passwords, API keys) they had access to.

Checklist

  • Admin user created with a non-obvious name and strong password
  • User in the sudo or wheel group, tested with sudo whoami
  • SSH key copied and key login tested
  • PermitRootLogin no set and SSH reloaded
  • Web files owned by your user, readable by the web server group, no 777