Перейти к основному содержимому
Linux Users, Groups & Sudo Basics
Урок 3 из 10

Linux Users, Groups & Sudo Basics

Understand how Linux tracks user and group identities in /etc/passwd, /etc/shadow, and /etc/group, manage accounts safely with useradd and usermod, and audit or use sudo under least privilege.

Начальный 20 мин Linux и Windows
Этого урока пока нет на выбранном языке — показана версия на другом языке. Перевод готовится.
Только в разрешённой среде — Описанные здесь приёмы применяются только в учебных лабораториях и в системах, которые принадлежат вам или на которые есть письменное разрешение.

Цели обучения

  • Explain the roles of UID, GID, /etc/passwd, /etc/shadow, and /etc/group in Linux access control
  • Inspect current user identity, group memberships, and allowed sudo rules using id and sudo -l
  • Create accounts and append supplementary groups safely using useradd and usermod -aG
  • Distinguish between su and sudo and apply least-privilege rules when granting administrative access

Рекомендуется заранее: Терминал Linux и структура каталогов FHSВладение файлами и права доступа rwx в Linux

#Why this matters

File ownership and rwx permissions only protect a Linux system if people and background services run under separate, least-privileged identities. If every engineer logs in as root or every service shares one account, a single mistake exposes the entire host. Understanding users, groups, and sudo lets you read account records, grant group access cleanly, and audit who can elevate privileges.

#Users, UIDs, and account files

Inside the kernel, permissions are checked against numeric User IDs (UIDs) and Group IDs (GIDs), not names. Every Linux host reserves UID 0 for root, the superuser who bypasses standard file permission checks. System and service accounts (such as www-data or sshd) usually hold low UIDs (typically 1-999), while normal human logins start at UID 1000.

Two files in /etc hold user account data:

File Typical permissions What it stores
/etc/passwd 644 (root:root) Username, placeholder x, UID, GID, comment, home path, login shell
/etc/shadow 640 (root:shadow) or 000 Salted password hashes and password expiration settings

Notice that /etc/passwd does not store actual passwords; the x in field two tells the system to look up the hash in /etc/shadow. Service accounts often end with /usr/sbin/nologin or /bin/false as their shell so nobody can open an interactive login session as that service.

#Groups and managing accounts

Every user belongs to one primary group (recorded in /etc/passwd) and zero or more supplementary groups (listed in /etc/group). Groups are the cleanest way to share directories or logs without resorting to chmod 777.

Common commands for inspecting and managing accounts:

  • whoami and id: show your current username, UID, primary GID, and all active group memberships.
  • useradd -m -s /bin/bash analyst: creates user analyst, builds /home/analyst (-m), and sets Bash as the shell (-s).
  • usermod -aG auditors analyst: appends (-a) the auditors group (-G) to analyst. Always include -a; omitting it replaces all existing supplementary groups!
Checking identity, account records, and group access
$ id analyst
uid=1001(analyst) gid=1001(analyst) groups=1001(analyst),1005(auditors)
$ grep -E "^(analyst|www-data):" /etc/passwd
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
analyst:x:1001:1001::/home/analyst:/bin/bash
$ sudo -l
Matching Defaults entries for analyst on lab:
    env_reset, mail_badpass
User analyst may run the following commands on lab:
    (ALL : ALL) ALL

Note that after adding an account to a new group with usermod -aG, the user's running terminal session still holds the old group list until they start a new login session (or open a fresh shell via su - analyst).

#Switching users and sudo

Two tools let you act as another user:

  • su - targetuser switches to targetuser (or root if omitted) and asks for the target user's password. The - flag loads a clean login environment (HOME, PATH) for that account.
  • sudo command runs a single command as root (or another specified user with sudo -u), asks for your own password, checks /etc/sudoers (and /etc/sudoers.d/), and logs the invocation to /var/log/auth.log or journalctl.
  1. Run sudo commandVerify user in /etc/sudoers
  2. Prompt passwordUser enters own password
  3. Log and executeRecord event in auth log and run

Because sudo enforces per-command rules and ties every privileged action to an individual account, defenders strongly prefer sudo over sharing the root password with su. Always edit /etc/sudoers with visudo, which checks syntax before writing so a typo cannot lock out administration.

#Common mistakes

  • Forgetting -a in usermod -aG: Using -G alone overwrites a user's supplementary groups, often stripping sudo or wheel access.
  • Working permanently as root: Opening a root shell with sudo su for routine browsing removes per-command accountability and increases the blast radius of a typo.
  • Overly broad sudoers rules: Granting NOPASSWD: ALL or allowing sudo on interactive tools like vim, less, or python3 effectively grants a full root shell.
  • Editing /etc/sudoers without visudo: A single syntax mistake in /etc/sudoers breaks sudo for every administrator on the host.

#Summary

Linux separates users and services by numeric UID and GID, storing public metadata in /etc/passwd, password hashes in /etc/shadow, and group memberships in /etc/group. Tools like id, useradd, and usermod -aG keep accounts clean and least-privileged, while sudo provides auditable, command-level elevation instead of shared root logins. Auditing sudo -l, group memberships, and service login shells keeps local privilege boundaries intact.

Проверка знаний

Войдите, чтобы проходить тест, завершать уроки и сохранять прогресс. Войти в систему

Практика: проверьте свои знания