Sub topic 8
Create an admin user and use sudo
This chapter assumes you can log in to a freshly installed server, edit files in a terminal editor, and know that Debian-family and RHEL-family systems use different package managers and defaults. By the end you will have a non-root administrative user that can run privileged commands through sudo, and you will know how to check that it works and fix the usual mistakes.
Why not work as root
The root account (user ID 0) can do anything on the system. A typo in a root shell, such as rm -rf /var/lib /old, runs with no safety net. Root logins also leave a poor audit trail, because every action is attributed to the same name no matter who typed it.
The usual pattern is a named account for each administrator. The account works as an ordinary user and borrows root's power only for single commands, using sudo ("superuser do"). Each use is logged with the real user's name, and you can limit or remove one person's access without touching the others.
Note that many installers already create such a user. Ubuntu's installer does, and it locks the root password. Debian's installer asks for a root password and a first user, and it adds that user to the sudo group only if you left the root password empty. Cloud images usually ship with a default user such as ubuntu, admin or rocky. Check what you have before creating anything new.
id
getent group sudo wheelid shows your user ID and groups. getent group prints the members of the named groups. A group that does not exist on your distribution is simply omitted from the output.
How sudo decides who may run what
The sudo program reads /etc/sudoers and every file in /etc/sudoers.d/. Each rule says which user or group may run which commands, as which target user, and on which host. The default distribution files already contain a rule granting full access to one group:
| Family | Admin group | Default rule |
|---|---|---|
| Debian, Ubuntu | `sudo` | `%sudo ALL=(ALL:ALL) ALL` |
| RHEL, Rocky, Alma, Fedora | `wheel` | `%wheel ALL=(ALL) ALL` |
The leading % marks a group name. Reading the Debian rule left to right: members of group sudo, on any host, may run commands as any user and any group, and may run any command. Adding a person to the right group is therefore all that is needed in most cases.
Notice that sudo asks for your own password, not root's. This proves that the person at the keyboard is the account owner. After a successful check, sudo caches the approval for a short time (15 minutes by default), so repeated commands do not prompt again.
Create the admin user
Run these steps as root or as an existing sudo-capable user. The commands below match the ones in the plan for this tutorial.
# Debian/Ubuntu: create the user and add it to the sudo group
sudo adduser admin
sudo usermod -aG sudo admin
# RHEL family (Rocky, Alma, Fedora): the administrative group is wheel
sudo useradd -m admin
sudo passwd admin
sudo usermod -aG wheel adminThe pieces work as follows:
adduseron Debian-family systems is an interactive wrapper. It creates the home directory, copies default shell files and prompts for a password.useraddon RHEL-family systems is the low-level tool. The-moption creates the home directory, and you set the password separately withpasswd.usermod -aG group useradds the user to a supplementary group. The-a(append) option is essential. Without it,-Greplaces the whole supplementary group list, which silently removes the user from every other group.
Pick a name that identifies a person or role, such as alice, rather than a generic name that attackers guess. The example uses admin for clarity. Use a long, unique password for now. Later chapters replace password logins over SSH with keys, but the sudo password stays in use.
Test the new user before relying on it
Group membership is read at login. If you are logged in as the user while adding the group, the current shell does not know about it. Test in a fresh session instead.
su - admin
id
sudo whoami
sudo -lsu - adminstarts a login shell asadmin. The dash gives it a clean environment.idshould listsudoorwheelamong the groups.sudo whoamishould printrootafter you enter the admin user's password.sudo -llists what the user may run. Expect(ALL : ALL) ALLor similar.
Do not close your root or original session until this passes. If the test fails, you still have a way in to repair it.
Everyday sudo habits
Prefix a single command with sudo rather than opening a root shell. This limits how long you hold elevated rights and gives each command its own log line.
sudo systemctl status ssh
sudo less /var/log/auth.log
sudo -u www-data id
sudo -ksudo -u user commandruns the command as another user instead of root.sudo -kdiscards the cached approval so the next call prompts again.sudo -iopens a root login shell, andsudo -sopens a root shell that keeps more of your environment. Use them sparingly.
A common trap involves redirection. In sudo echo text > /etc/file, only echo runs as root. The shell opens /etc/file for writing first, as your ordinary user, so the redirect fails. Use echo text | sudo tee /etc/file instead, adding -a to tee to append.
Logs record each use. On Debian-family systems the entries are in /var/log/auth.log. On RHEL-family systems look in /var/log/secure. On either, journalctl _COMM=sudo reads them from the journal.
Customizing sudo rules safely
Avoid editing /etc/sudoers directly. Put your own rules in a file under /etc/sudoers.d/, which survives package upgrades, and edit it with visudo. The visudo tool locks the file and checks the syntax before saving. A syntax error in sudoers can disable sudo for everyone.
sudo visudo -f /etc/sudoers.d/90-deployA small example rule lets the user deploy restart one service without a password:
# /etc/sudoers.d/90-deploy
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp.serviceKeep these rules narrow. NOPASSWD: ALL removes the barrier that makes sudo a meaningful check, and a compromised account would then have full root with no further step. File names in sudoers.d must not contain a dot or end with ~, because sudo skips those. The file permissions should be 0440, which visudo sets for you. Validate the whole configuration at any time with:
sudo visudo -cCommon errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `admin is not in the sudoers file` | The user is not in the admin group, or the session predates the change | Add the group with `usermod -aG`, then log out and back in |
| `sudo: command not found` | Minimal install without the package | As root, run `apt install sudo` or `dnf install sudo` |
| User lost other groups | `usermod -G` was used without `-a` | Re-add the groups with `usermod -aG` |
| `sudo: parse error in /etc/sudoers.d/...` | Hand-edited file with a syntax error | Fix it as root with `visudo -f`, or remove the file |
| Redirect gives `Permission denied` | Redirection runs as your user | Use `sudo tee` |
Locking down root
Once your admin user works, you can stop using root directly. Ubuntu already locks the root password. On other systems you can lock it, which blocks password logins to root while still allowing sudo -i from an admin account.
sudo passwd -l rootUnlock it with passwd -u root if you need it back. A later chapter also disables root login over SSH, which covers the remote route to the same account.
Exercise
You ran sudo usermod -G sudo admin on a Debian server. The user can now use sudo, but later finds they cannot read files that need the adm group they belonged to before. What went wrong, and what is the correct command to restore the group?
Show answer
Without -a, usermod -G replaces the whole list of supplementary groups, so admin lost adm and any others. Restore it with sudo usermod -aG adm admin, and use -aG from the start next time. The user must log in again for the change to take effect.
Exercise
Why does sudo echo "net.ipv4.ip_forward=1" >> /etc/sysctl.d/99-forward.conf fail with "Permission denied", and how do you write it correctly?
Show answer
The shell performs the >> redirection as your own user before sudo starts, so the file is opened without root rights. Use echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.d/99-forward.conf, where tee runs as root and appends to the file.