skip to content

On an OpenSSH server, how do AllowUsers and AllowGroups limit who may log in, and how do DenyUsers and DenyGroups interact with them?

level: middleimportance: should knowfreq 38%

answer

  1. an allow list, not a method
  2. names, never numeric IDs
  3. primary or supplementary group
  4. USER@HOST with a CIDR range
  5. a deny match ends it

basics

~20 s

AllowUsers and AllowGroups turn sshd_config into an allow list: once set, only matching user names, or members of matching groups, may log in, whatever method they use. A user matched by DenyUsers or DenyGroups is refused without consulting the allow lists.

solid answer

~40 s

By default sshd accepts any account that authenticates. `AllowUsers` takes user-name patterns, optionally `USER@HOST` where HOST may be a CIDR range such as `[email protected]/24`; `AllowGroups` takes group-name patterns and matches a user's primary or supplementary groups. Once either is set, an account outside it is refused whatever credential it presents, so a valid key in a service account's `authorized_keys` stops working. Only names are recognised, not numeric IDs, and repeated lines append to the list. `DenyUsers` and `DenyGroups` take precedence: the matching allow list is not consulted for a user a deny pattern matches. In practice one `AllowGroups` line naming an SSH-login group scales better than listing people, and both keywords can also sit inside a `Match` block.

go deeper

for a junior

Recall that AllowUsers and AllowGroups turn sshd into an allow list for every login method, and that a DenyUsers or DenyGroups match always wins over them.

for a middle

Explain the pattern syntax, USER@HOST with CIDR, primary versus supplementary groups, names only, appending lines, and that the keywords also work inside Match blocks.

for a senior

Roll it out without breaking automation: inventory service accounts first, prefer one login group, and know that switches like AllowAgentForwarding only bite on accounts without a shell.

for a principal

Decide where login policy lives: a group fed from the identity source scales across a fleet, while host-local name lists drift; pick the model joiners and leavers update automatically.

## The default: anyone who can authenticate Out of the box, OpenSSH's sshd lets **any account** on the host log in if it can complete an enabled authentication method. On a server with dozens of system and application accounts, that means every one of them is a possible login target, and a forgotten account with a weak password or a stale key is a way in. The four access-list keywords in `sshd_config` narrow that set before any credential matters. | Directive | Matches | Notes from sshd_config(5) | |---|---|---| | `AllowUsers` | user-name patterns, optionally `USER@HOST` | HOST may be a name pattern or an address in CIDR form; lines append | | `AllowGroups` | group-name patterns against the user's primary or supplementary groups | lines append | | `DenyUsers` | the same syntax as `AllowUsers` | `AllowUsers` is not consulted for a user it matches | | `DenyGroups` | group-name patterns | `AllowGroups` is not consulted for a group it matches | Key facts that interviewers probe: - **Names only.** A numerical user ID or group ID is not recognised in these lists. - **Every method.** The lists decide whether the account may log in at all, so they apply to keys, certificates and passwords alike. - **Deny wins.** A user matched by `DenyUsers` is refused, and listing the same user in `AllowUsers` does not rescue them. - **Appending, not replacing.** Each additional `AllowUsers` or `AllowGroups` line adds patterns to the list. - **Patterns.** The pattern syntax (wildcards `*` and `?`, negation with `!`) is the one described under PATTERNS in ssh_config(5). - **Scoping.** `AllowUsers`, `AllowGroups`, `DenyUsers` and `DenyGroups` are all permitted inside a `Match` block. ## USER@HOST: tying an account to a source The `USER@HOST` form checks the two parts separately. `[email protected]/24` means the account `deploy` may log in only from that range; a pattern without a host part, such as `alice`, allows that user from anywhere. This is the cleanest way to pin an automation account to the network its jobs run from. ```ini # Option A: people by name, one service account pinned to its network AllowUsers alice bob [email protected]/24 # Option B: membership in one login group decides # AllowGroups sshlogin ``` ## A worked decision Take a server configured with `AllowGroups sshlogin` and `DenyUsers olduser`: | Account | Groups | Outcome | |---|---|---| | `alice` | `sshlogin` (supplementary) | may log in with any enabled method | | `deploy` | `deploy` only | refused, even with a valid key | | `olduser` | `sshlogin` | refused: a `DenyUsers` match is final | The table is the whole mental model: deny matches are final, an allow list that is present must match, and the credential only matters once the account is permitted. ## Rolling it out without an outage The usual failure is not a security hole but a lockout of something nobody remembered. A safe sequence: 1. Inventory every account that logs in over SSH today, people and automation alike (deploy jobs, backup pulls, monitoring). 2. Create a login group such as `sshlogin` and add the people and the service accounts that genuinely need a shell. 3. Add `AllowGroups sshlogin` (or an `AllowUsers` list for a small host). 4. Keep an existing session open while you reload, and verify the effective configuration; the safe-reload procedure itself belongs to the material on OpenSSH operations. A group fed from the central identity source means joiners and leavers change SSH access without anyone editing `sshd_config` on each host, which is why `AllowGroups` usually wins over long `AllowUsers` lists. `AllowUsers` stays useful for pinning one account to a source range. ## What these lists do not do - **They do not switch passwords off.** They shrink the set of accounts an attacker can usefully target; `PasswordAuthentication` and `KbdInteractiveAuthentication` decide which methods exist. - **They do not confine what an allowed user does.** Restricting port forwarding belongs with the forwarding switches, and file-transfer-only accounts with their chroot and forced `internal-sftp` are a separate design. - **Per-feature switches for shell users have limits.** `X11Forwarding` already defaults to `no`. `AllowAgentForwarding` defaults to `yes`, and sshd_config(5) notes that disabling it does not improve security unless users are also denied shell access, because a user with a shell can install their own forwarder. Set it for accounts without a shell; for shell users the meaningful control is who may log in at all. ## Summary for the interview Allow lists are the "who" of sshd hardening: name-based, method-independent, with deny entries taking precedence and `USER@HOST` adding a source condition. Pair them with key-only authentication and you have reduced both the set of accounts and the ways into them.

  • Does AllowAgentForwarding no stop a shell user on the server from forwarding an agent?
    Not reliably. sshd_config(5) notes that disabling agent forwarding does not improve security unless users are also denied shell access, because a user with a shell can install their own forwarder. It is worth setting for accounts without a shell; for shell users the real control is who may log in at all.
  • Why might AllowGroups be preferred over AllowUsers on a server with many administrators?
    Group membership is managed once, usually in the identity source, so joiners and leavers gain or lose SSH access without editing `sshd_config` on every host. `AllowUsers` lists names in the file itself and drifts as people move. It stays useful for pinning one account to a source range with `USER@HOST`.
  • A deploy job's key login fails right after AllowGroups sshlogin was added, though the key is unchanged. Why?
    `AllowGroups` applies whatever the authentication method, so an account that is not a member of `sshlogin` is refused even with a valid key in its `authorized_keys`. Add the deploy account to the group before rolling the change out, and inventory automation accounts first next time.

  • AllowUsers only affects password logins, so key logins are unaffected.
  • Listing a user in AllowUsers overrides a matching DenyUsers entry.
  • AllowGroups accepts numeric group IDs as well as group names.
  • A second AllowUsers line replaces the first one.
  • AllowAgentForwarding no stops shell users from forwarding an agent.