$ run as a normal user# run as root (or with sudo)
The quick answer
Step by step
- Create the user with a home folder (
-m) and a comment. On Ubuntu,adduseris the friendly wrapper (it asks for the password and details). - Give admin rights by adding the user to the admin group —
wheelon RHEL,sudoon Ubuntu. Always use-aG(append) — without-athe user loses its other groups. - 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- 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.)
- Password rules (optional): force a new password at the first login and set an expiry:
- When someone leaves: lock the account at once, remove the keys, then delete it later:
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
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 sudo | The session started before the change. | Log out and in (or su - alice). |
sudo: parse error in /etc/sudoers.d/... — sudo is broken | A rule was edited without visudo. | pkexec visudo -f FILE, or the root console; always use visudo. |
| User lost other groups | usermod -G without -a. | Add them back with usermod -aG. |
| SSH key login fails for the new user | Rights 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. |
Go further
- Command pages: useradd, usermod, visudo, sudo, passwd, chage.
- Next how-tos: Log in with SSH keys, Change file permissions and owner, Lock or disable a user account, Set password rules. Harden SSH logins: OpenSSH guide, Write sudo rules safely.
- Learn it for RHCSA: RHCSA — Users and groups, RH124 — Users. Practise: Lab: users and groups.
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.