Sub topic 8
Create an Admin User and Use `sudo`
Assume you can log in as root or as an existing account with administrative access. You also need a working terminal session and know which Linux distribution runs on the server. The commands differ because Debian-based systems commonly use the sudo group, while RHEL-family systems commonly use the wheel group.
A non-root administrative account limits mistakes. The account performs ordinary work without unrestricted privileges. The sudo command temporarily runs one command with root privileges after checking the user's permissions and, usually, the user's password.
Create the account
On Debian or Ubuntu, use adduser. It creates the home directory and asks for a password:
sudo adduser admin
sudo usermod -aG sudo admin
id adminThe -aG options append the user to a supplementary group. The -a matters. Without it, usermod -G can replace the user's existing supplementary groups.
On Rocky, Alma, Fedora, or another RHEL-family system, use useradd, set a password, and add the user to wheel:
sudo useradd --create-home --shell /bin/bash admin
sudo passwd admin
sudo usermod -aG wheel admin
id adminThese group memberships normally come from an existing rule in /etc/sudoers. Do not edit that file with a normal text editor. The visudo command checks syntax before installing a change.
| Distribution family | Administrative group | Typical account command |
|---|---|---|
| Debian or Ubuntu | `sudo` | `adduser admin` |
| RHEL family | `wheel` | `useradd --create-home admin` |
Verify access
Group membership is evaluated when a login session starts. Log out and log back in as admin, or open a new SSH session. Then check the account:
whoami
id
sudo -v
sudo idsudo -v validates or refreshes your authorization without running another command. sudo id should report uid=0(root). If id admin shows the expected group but sudo still denies access, start a new login session and check the distribution's sudo configuration.
A user with sudo or wheel membership generally has full root access. Treat that account as highly privileged. Use sudo only for the command that needs it, and avoid making routine files owned by root unnecessarily.
Use narrower permissions when needed
Full administrative access is convenient for a server operator, but it is broader than an application account needs. For a narrowly delegated task, create a separate sudoers rule with visudo:
sudo visudo -f /etc/sudoers.d/90-backupAdd a rule such as:
backupadmin ALL=(root) /usr/bin/systemctl restart backup.serviceThe command path and arguments must match the intended permission. Test the rule as that user. Avoid NOPASSWD unless automation requires it and the account is otherwise well protected.
Permission flow
Exercise
Why should usermod -aG sudo admin use the -a option?
Show answer
The -a option appends the group without replacing the user's other supplementary groups. Omitting it can remove existing group memberships.