VPS/Servers management guides

How to Create a Sudo User on AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9

Create a sudo user on the RHEL 9 family: add it to the wheel group, test it, and grant limited rights with a visudo drop-in file in /etc/sudoers.d.

This guide shows you how to create a user with sudo rights on AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9, first by adding the account to the wheel group and then, where full root access is too much, by writing a narrow rule in /etc/sudoers.d with visudo. At the end you will have a personal administrative account and no reason to log in as root.

CentOS 7 reached end of life in June 2024 and no longer receives security updates. The commands below are the same ones you would have used there, but run them on a supported release of the RHEL 9 family.

Requirements

  • A server running AlmaLinux 9, Rocky Linux 9, CentOS Stream 9 or RHEL 9.
  • Access as root, or as an existing user who already has sudo. The commands below are written with sudo; if you are logged in as root, leave it out.

Create the user

Create the account. This guide uses the name deploy; replace it with your own.

sudo useradd -m deploy

Set a password for it. You will be asked to type it twice. sudo asks for this password later, so the account needs one even if you log in with SSH keys.

sudo passwd deploy

Add the user to the wheel group

On the RHEL family, members of the wheel group may run any command through sudo. The default /etc/sudoers file contains this rule:

%wheel  ALL=(ALL)       ALL

The % prefix means "a group", so the line reads: members of wheel, on any host, may run any command as any user. Add your user to the group:

sudo usermod -aG wheel deploy

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

Confirm the membership:

id deploy

The output should list wheel among the groups. Group changes apply to new sessions only, so if deploy is already logged in, that session has to log out and back in.

Test sudo access

Switch to the new account with a full login shell:

su - deploy

Run a command that needs root:

sudo whoami

Enter the password of deploy (not the root password). The command should print root. After a successful authentication sudo remembers you for five minutes by default, so the next few commands do not ask again.

To see exactly what the account is allowed to do:

sudo -l

If you get "deploy is not in the sudoers file", the session predates the group change (log out and in again) or the %wheel line is commented out in /etc/sudoers. Check it with sudo visudo from an account that still has access.

Grant limited rights with a drop-in file

Membership of wheel is all or nothing. For an account that should only run a few commands, leave it out of wheel and write a rule in /etc/sudoers.d instead. The main sudoers file includes every file from that directory, so you never have to edit the main file, and package updates will not overwrite your rules.

Always edit sudoers files with visudo. It locks the file, checks the syntax when you save and refuses to install a broken file silently. A syntax error in a sudoers file can lock everyone out of sudo.

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

The file opens in your editor (vi unless EDITOR or VISUAL says otherwise). Add a rule such as:

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

This lets deploy restart and reload one service as root and nothing else. Use full paths to commands. If a script or automated job has to run these commands without a password prompt, add the NOPASSWD: tag:

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

Save and quit. If visudo finds an error it shows the line number and asks "What now?". Press e to go back and fix it, or x to exit without saving.

Two rules about file names in /etc/sudoers.d catch people out:

  • Files whose name contains a dot or ends in ~ are skipped. deploy works; deploy.conf is ignored.
  • Files are read in lexical order, and when several rules match a command the last one wins. Prefix names with numbers (10-deploy, 20-backup) if the order matters.

Check all sudoers files in one go:

sudo visudo -c

Then list the resulting rights for the user:

sudo -l -U deploy

Remove sudo access

To take full rights away again, remove the user from the group:

sudo gpasswd -d deploy wheel

To withdraw a limited rule, delete its drop-in file and re-check the syntax:

sudo rm /etc/sudoers.d/deploy
sudo visudo -c

Red Hat documents the same workflow in the Managing sudo access chapter of the RHEL 9 documentation, and the full rule syntax is in the sudoers manual.

Frequently asked questions

Does this work on Ubuntu or Debian?

The mechanism is the same, but the administrative group there is called sudo rather than wheel. Use usermod -aG sudo deploy on those systems. Drop-in files in /etc/sudoers.d work identically.

Should I give an account NOPASSWD for everything?

Avoid it for accounts that people log in to. The password prompt is the only barrier left if someone gets hold of the session or its SSH key. Reserve NOPASSWD: for specific commands that automation has to run.

Why does sudo ask for my own password and not the root password?

sudo confirms that the person at the keyboard is the account owner; the right to run the command comes from the sudoers rules. That is why several administrators can have root rights without sharing a root password, and why each command is logged under the name of the person who ran it.

Can I disable root logins once the sudo user works?

Yes, and it is a sensible next step: confirm that you can log in over SSH as the new user and that sudo whoami prints root, then set PermitRootLogin no in the SSH server configuration and restart sshd. Keep one working session open while you test, so a mistake does not lock you out.

Share story:

Continue reading other guides on How VPS.