Scope: Debian-based and RHEL-based Linux distributions
Topics: Regular user creation, system user creation, sudo privileges, verification, and usage scenarios.
Part 1: Debian-Based Systems#
Examples: Debian, Ubuntu
1. Install sudo#
Run as root:
apt update
apt install sudo2. Create a Regular User#
Create a user interactively:
adduser <USERNAME>Alternatively, use useradd:
useradd -m -s /bin/bash <USERNAME>
passwd <USERNAME>Options:
-m: Creates the user’s home directory.-s /bin/bash: Sets Bash as the default shell.passwd: Sets or changes the user’s password.
3. Grant Sudo Privileges#
Add the user to the sudo group:
usermod -aG sudo <USERNAME>Verify group membership:
id <USERNAME>
groups <USERNAME>Confirm that sudo appears in the output.
Validate the sudoers configuration:
visudo -cThe default configuration should authorize the sudo group. If necessary, inspect it using:
visudoThe relevant rule is typically:
%sudo ALL=(ALL:ALL) ALLDo not enable this rule without understanding its implications: it grants group members broad administrative privileges.
4. Create a System User#
System accounts are intended for services and applications rather than interactive human logins.
Create a system account with its own group, no home directory, and a non-login shell:
adduser --system --group --no-create-home --shell /usr/sbin/nologin <SERVICE_USER>Verify the account:
id <SERVICE_USER>
getent passwd <SERVICE_USER>
command -v nologinCheck that the account has the expected shell and that it cannot normally open an interactive login shell.
System user usage scenarios#
Scenario A: Application service
adduser --system --group --no-create-home --shell /usr/sbin/nologin myappA systemd service can run under this account:
[Service]
User=myapp
Group=myapp
ExecStart=/opt/myapp/bin/myappThe executable and required files must be accessible to myapp.
Scenario B: Backup service
adduser --system --group --no-create-home --shell /usr/sbin/nologin backupsvcGrant backupsvc access only to the files and directories required for its backup operations.
Scenario C: Monitoring exporter
adduser --system --group --no-create-home --shell /usr/sbin/nologin exporterGrant the account only the read permissions needed to collect monitoring data.
5. Test Sudo Privileges#
Switch to the regular user:
su - <USERNAME>Inspect the permitted operations:
sudo -lTest administrative execution:
sudo whoamiExpected output:
rootIf group membership was changed after login, start a new login session before testing.
Part 2: RHEL-Based Systems#
Examples: RHEL, Rocky Linux, AlmaLinux
1. Install sudo#
Run as root:
dnf install sudoIf sudo is already installed, this step is unnecessary.
2. Create a Regular User#
Create the user and home directory:
useradd -m -s /bin/bash <USERNAME>Set the password:
passwd <USERNAME>Options:
-m: Creates the user’s home directory.-s /bin/bash: Sets Bash as the default shell.
3. Grant Sudo Privileges#
RHEL-based systems commonly use the wheel group for administrative access.
Add the user to the group:
usermod -aG wheel <USERNAME>Verify group membership:
id <USERNAME>
groups <USERNAME>Validate the sudoers configuration:
visudo -cIf necessary, inspect or edit the configuration using:
visudoThe following rule is commonly used to authorize the wheel group:
%wheel ALL=(ALL) ALLVerify that the rule is enabled and is not overridden by another applicable configuration rule.
4. Create a System User#
Create a system account with its own group, no home directory, and a non-login shell:
useradd --system --user-group --no-create-home --shell /sbin/nologin <SERVICE_USER>Verify the account:
id <SERVICE_USER>
getent passwd <SERVICE_USER>
command -v nologinCheck that the account has the expected shell and that it cannot normally open an interactive login shell.
System user usage scenarios#
Scenario A: Application service
useradd --system --user-group --no-create-home --shell /sbin/nologin myappExample systemd service configuration:
[Service]
User=myapp
Group=myapp
ExecStart=/opt/myapp/bin/myappScenario B: Backup service
useradd --system --user-group --no-create-home --shell /sbin/nologin backupsvcGrant only the permissions required by the backup process.
Scenario C: Monitoring exporter
useradd --system --user-group --no-create-home --shell /sbin/nologin exporterGrant read access only to the resources the exporter needs to monitor.
5. Test Sudo Privileges#
Switch to the regular user:
su - <USERNAME>Check sudo permissions:
sudo -lTest administrative execution:
sudo whoamiExpected output:
rootIf group membership was changed after login, start a new login session before testing.
Part 3: Important Notes#
Regular Users vs. System Users#
| Property | Regular user | System user |
|---|---|---|
| Intended purpose | Human login and administrative work | Application or service execution |
| Home directory | Usually present | Often omitted |
| Interactive shell | Usually enabled | Commonly disabled |
| Sudo privileges | Only when explicitly granted | Not granted by default |
| Example | <USERNAME> | myapp, backupsvc, exporter |
Command Reference#
| Operation | Debian-based | RHEL-based |
|---|---|---|
| Install sudo | apt install sudo | dnf install sudo |
| Create regular user | adduser <USERNAME> | useradd -m -s /bin/bash <USERNAME> |
| Set password | passwd <USERNAME> | passwd <USERNAME> |
| Grant sudo access | usermod -aG sudo <USERNAME> | usermod -aG wheel <USERNAME> |
| Create system user | adduser --system --group --no-create-home --shell /usr/sbin/nologin <SERVICE_USER> | useradd --system --user-group --no-create-home --shell /sbin/nologin <SERVICE_USER> |
| Check account | id <USERNAME> | id <USERNAME> |
| Check sudo configuration | visudo -c | visudo -c |
| Test sudo | sudo whoami | sudo whoami |
Security Guidelines#
- Replace placeholders such as
<USERNAME>and<SERVICE_USER>with actual account names. Do not include the angle brackets in commands. - Always use
usermod -aGwhen adding supplementary group membership to avoid replacing existing supplementary groups. - System accounts should not be added to
sudoor `wheel unless the service has a specific, justified requirement for administrative privileges. - Use a dedicated account for each application or service where practical.
- Grant only the file permissions and capabilities required for the account’s purpose.
- Use
visudoto edit sudoers configuration safely. - When logged in as root, the user-management commands above generally do not require the
sudoprefix. - Use a regular account for everyday administration and elevate privileges with
sudowhen necessary.