aoitcloud

Guides and How Tos

Creating a new sudo user on Rocky Linux

A step-by-step guide to creating a non-root user with sudo privileges on Rocky Linux, and verifying it actually works before you log out of root.

29 October 2024 3 min read

Logging in as root for everyday administration is a bad habit – one mistyped command can do a lot of damage with no safety net. The standard fix on Rocky Linux, as with any RHEL-family distribution, is to create a personal account and grant it sudo privileges via the wheel group. Here’s how to do it properly.

1. Create the new user

Connect to your VPS as root (or an existing sudo user) and run:

useradd yourusername

Replace yourusername with whatever you want the account to be called. This creates the user, their home directory at /home/yourusername, and a default shell.

2. Set a password

The account has no password yet, so it can’t log in. Set one with:

passwd yourusername

You’ll be prompted to enter and confirm a new password. Use something strong – this account is about to gain root-equivalent power via sudo, so treat the password with the same seriousness you’d treat the root password itself. If you’re planning to use SSH key authentication instead of passwords (recommended), you can set this now and lock down password login later.

3. Add the user to the wheel group

On Rocky Linux, as on AlmaLinux and RHEL itself, members of the wheel group are permitted to use sudo. Add your new user to it:

usermod -aG wheel yourusername

The -aG flags matter: -a appends the group rather than replacing existing group memberships, and -G specifies the group to add. Leaving out -a is a common mistake that strips a user’s other group memberships.

4. (Optional) Set up SSH key authentication

If you want to log in as this user via SSH key rather than password, create the .ssh directory and authorized_keys file with the correct permissions:

mkdir -p /home/yourusername/.ssh
chmod 700 /home/yourusername/.ssh
nano /home/yourusername/.ssh/authorized_keys

Paste your public key into the file, save, then fix the permissions and ownership:

chmod 600 /home/yourusername/.ssh/authorized_keys
chown -R yourusername:yourusername /home/yourusername/.ssh

5. Verify sudo access before you log out of root

This is the step people skip, and the one that saves you a support ticket. In a separate terminal session (don’t close your existing root session yet), log in as the new user:

ssh yourusername@your-server-ip

Once logged in, confirm sudo works:

sudo whoami

You’ll be prompted for the user’s password (not root’s), and the command should return root. If that works, you’ve got a properly configured sudo user and it’s safe to disable direct root SSH login if that’s part of your hardening plan.

From here, package management with dnf, firewall configuration with firewalld, and service management with systemd all work exactly the same as on any other RHEL-family distribution – see our guides on managing packages with dnf, firewalld basics, and managing services with systemd.

Get in touch

Drop our team a message today