Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 8Security · Linux

How to create a user with sudo rights

Every admin should have a personal account and use sudo for admin work — never share root. This how-to creates a user on RHEL and Ubuntu, gives full or limited sudo rights the safe way, adds an SSH key, and checks the result. It also covers removing access when someone leaves.

  • 5 Lessons
  • 17 Commands

$ run as a normal user# run as root (or with sudo)

8.1

The quick answer

RHEL, Rocky, Alma, Fedora (admin group wheel):

useradd -m -c "Alice Admin" -G wheel alice
passwd alice

Ubuntu, Debian (admin group sudo):

adduser alice
usermod -aG sudo alice

8.2

Step by step

  1. Create the user with a home folder (-m) and a comment. On Ubuntu, adduser is the friendly wrapper (it asks for the password and details).
  2. Give admin rights by adding the user to the admin group — wheel on RHEL, sudo on Ubuntu. Always use -aG (append) — without -a the user loses its other groups.
  3. Or give only some rights (better for service accounts and junior admins). Write a rule in its own file with visudo, which checks the syntax before saving:
visudo -f /etc/sudoers.d/webops
/etc/sudoers.d/webops
%webops ALL=(root) /usr/bin/systemctl restart nginx, /usr/bin/systemctl reload nginx, /usr/bin/journalctl -u nginx *
deploy  ALL=(root) NOPASSWD: /usr/local/bin/deploy.sh
groupadd webops
usermod -aG webops bob
chmod 440 /etc/sudoers.d/webops
  1. Add the user's SSH key so they can log in without a password:
install -d -m 700 -o alice -g alice /home/alice/.ssh
echo 'ssh-ed25519 AAAAC3Nza…alice-laptop' > /home/alice/.ssh/authorized_keys
chown alice:alice /home/alice/.ssh/authorized_keys && chmod 600 /home/alice/.ssh/authorized_keys
restorecon -Rv /home/alice/.ssh

(restorecon on RHEL only.)

  1. Password rules (optional): force a new password at the first login and set an expiry:
chage -d 0 alice
chage -l alice
  1. When someone leaves: lock the account at once, remove the keys, then delete it later:
usermod -L -e 1 alice
rm -f /home/alice/.ssh/authorized_keys
userdel -r alice

8.3

Check that it worked

id alice
uid=1001(alice) gid=1001(alice) groups=1001(alice),10(wheel)
sudo -l -U alice
User alice may run the following commands on web1:
    (ALL) ALL
su - alice
sudo whoami
root

The new group works only in a new login (log out and in, or su - alice).

8.4

Common problems

ProblemCauseFix
alice is not in the sudoers file. This incident will be reported.Not in wheel/sudo, or no rule.usermod -aG wheel alice (RHEL) / sudo group (Ubuntu); log in again.
Group added, still no sudoThe session started before the change.Log out and in (or su - alice).
sudo: parse error in /etc/sudoers.d/... — sudo is brokenA rule was edited without visudo.pkexec visudo -f FILE, or the root console; always use visudo.
User lost other groupsusermod -G without -a.Add them back with usermod -aG.
SSH key login fails for the new userRights or owner of .ssh.700 for .ssh, 600 for authorized_keys, owner the user; restorecon on RHEL.
Too broad rights (NOPASSWD: ALL)Convenience over safety.Limit to the exact commands; keep passwords for full admin.

8.5

Go further

1Which group gives full sudo rights on RHEL, and which on Ubuntu?

wheel on RHEL, sudo on Ubuntu.

2Why edit sudo rules only with visudo?

It checks the syntax before saving; a broken sudoers file can lock everyone out of sudo.

All commands in this chapter