Skip to content

Linux user management — humans, services, and the lifecycle

A clean fleet has named human users, dedicated service accounts, and no one logging in as root. Here is the model and the commands that keep it that way.

A clean fleet has named human users (one per real operator), dedicated service accounts (one per long-running process), and no one logging in as root. Achieving and maintaining that separation is a matter of a few useradd/usermod patterns plus the discipline to remove what is no longer needed. This article documents the model and the commands.

How to verify

Inventory the existing users and groups on a host.

getent passwd | awk -F: '$3 >= 1000 && $3 < 65534 { print }'        # human users
getent passwd | awk -F: '$3 >= 100 && $3 < 1000 { print }'           # system accounts
getent group | grep -E ':(sudo|wheel|docker|adm):'                   # privilege groups
sudo cat /etc/passwd | grep -v 'nologin\|false' | head
sudo lastlog -u "$(whoami)"                                          # last login per user
sudo last -F | head -20                                              # login history

getent passwd reads from every NSS source — local /etc/passwd plus LDAP/SSSD if configured. The UID ranges separate human users (≥1000 on most distros) from system users (typically 100–999).

What’s happening

Linux users come in two flavours by convention:

  • Human users have a UID ≥ 1000 (the UID_MIN in /etc/login.defs), a home directory under /home/, a real shell (/bin/bash), and a password (or SSH keys). They are intended for interactive login.
  • System users have a UID below 1000 (often between 100 and 999), no real home directory or one under /var/lib/<service>, a nologin shell (/usr/sbin/nologin), and no password. They exist solely as the identity under which a daemon runs. nginx runs as www-data, postgres as postgres, etc.

The kernel does not care about the convention — every process simply runs as some UID. The convention exists so audit, logging, and access control can speak the same language.

Group membership is the access-control unit. Adding a user to sudo (or wheel) grants privilege escalation. Adding to docker grants the ability to launch containers as root (because the Docker daemon runs as root). Adding to adm lets the user read system logs. Group hygiene is half of user hygiene.

The lifecycle problem: users get added, projects end, people leave, the user accounts remain. A quarterly audit + a lock-after-N-days-of-inactivity policy keeps the rot bounded.

The procedure

  1. Create a human user. adduser (Debian/Ubuntu) is friendlier than useradd (the lower-level cross-distro command).

    sudo adduser --gecos "Alice Operator" alice
    sudo usermod -aG sudo alice                # if Alice is an operator
    sudo install -d -m 0700 -o alice -g alice /home/alice/.ssh

    Then install the key per ssh-key-authentication.

  2. Create a system (service) user. Use --system so it gets a UID below 1000, no home directory, and a nologin shell.

    sudo useradd --system --shell /usr/sbin/nologin \
                 --home-dir /var/lib/myapp --create-home \
                 --user-group myapp

    --user-group creates a group of the same name. The service then runs as myapp:myapp, and its data lives under /var/lib/myapp/ with sane ownership.

  3. Group operations.

    sudo groupadd developers                 # create a group
    sudo gpasswd -a alice developers         # add alice to developers
    sudo gpasswd -d bob developers           # remove bob from developers
    sudo gpasswd -A alice developers         # make alice an administrator of developers

    gpasswd is the right tool for membership changes; usermod -aG works but rewrites the user’s secondary groups list, which is error-prone if you forget the -a.

  4. Set or change a password (only when keys are not the access method).

    sudo passwd alice                        # interactive prompt
    echo 'alice:newpass' | sudo chpasswd     # non-interactive (use cautiously)
    sudo passwd -e alice                     # expire password — force change on next login

    For SSH-key-only operators, set a passphrase via passwd so the account can use sudo, but never enable password SSH.

  5. Lock and disable an account. When someone leaves, the order is: revoke SSH keys → lock the account → archive the home directory → remove after retention period.

    # Step 1: revoke SSH access by emptying authorized_keys (via configuration management ideally)
    sudo truncate -s 0 /home/bob/.ssh/authorized_keys
    # Step 2: lock the password so sudo will not work either
    sudo passwd -l bob
    sudo usermod -L bob                      # equivalent
    # Step 3: optionally set the shell to nologin to prevent any login at all
    sudo usermod -s /usr/sbin/nologin bob
    # Step 4: archive the home directory
    sudo tar -czf /var/backups/bob-home-$(date +%F).tar.gz -C /home bob
    # Step 5: after retention, remove
    sudo deluser --remove-home bob

    deluser (Debian/Ubuntu) or userdel --remove (cross-distro) removes the user and the home directory. Removing before archiving is unrecoverable.

  6. Audit who has shell access. A periodic check that should be automated.

    awk -F: '$7 !~ /(nologin|false)$/ { print $1 }' /etc/passwd

    Anything in that output beyond root + your operator users is suspect. Service accounts should have nologin shells.

  7. Expire idle accounts. Set inactive-account locking globally via useradd defaults or per-user with chage.

    sudo chage -I 90 alice                   # lock if password unchanged for 90 days after expiry
    sudo chage -M 365 alice                  # max password age 365 days
    sudo chage -l alice                      # list aging info

    For key-only accounts, password aging is irrelevant; rotate keys via the SSH layer.

  8. Find files owned by a user before deleting them. Removing a user leaves their files behind with a numeric UID. Find them first.

    sudo find / -uid 1003 -ls 2>/dev/null | head -50

    Decide: archive, reassign ownership (sudo chown -R newowner: /path), or delete.

  9. For fleets, manage users in configuration management. The source of truth is not /etc/passwd — it is the Ansible / Terraform / Pulumi state. Define users there; let the tool apply.

    - name: Operators present on every host
      ansible.builtin.user:
        name: "{{ item }}"
        state: present
        shell: /bin/bash
        groups: sudo
        append: yes
      loop:
        - alice
        - bob
        - carol
    - name: Departed operators absent
      ansible.builtin.user:
        name: "{{ item }}"
        state: absent
        remove: yes
      loop:
        - dave
  10. Sync usernames with the identity provider. If you have an LDAP or SSO system, hosts should authenticate against it (via sssd or nss-ldapd), and local accounts should be the fallback only.

Operational notes

  • useradd does not create a home directory by default; pass -m. adduser does by default. This is the most common cross-distro surprise.
  • usermod -aG is the only safe way to add to a group; without -a, the user’s secondary groups are replaced.
  • A user removed without --remove-home leaves a home directory orphan; future users with the same name will not own it.
  • UID re-use after deletion is a footgun — if Alice (UID 1003) leaves and Bob (newly added) gets UID 1003, Bob now owns any leftover files from Alice. Always sweep first.
  • Containers can run as a UID that does not exist on the host. From the host’s perspective, ls -l shows just the numeric UID. Map carefully if you bind-mount directories.
  • The nobody user (UID 65534) is a fallback for unmapped users in NFS / containers. Real services should never run as nobody.

In our managed engagements, the source of truth for users is a single Git repo per environment, applied by configuration management. A quarterly sweep catches any drift between the source of truth and /etc/passwd on each host. The result: an answer to “who has shell access to this host” that is one query, not an audit — and that auditability is part of the Managed Operations access posture.