{ "id": "ssh-access-controls", "title": "SSH Server Access Controls", "category": "access", "tags": ["ssh", "sshd_config", "AllowUsers", "AllowGroups", "security", "hardening"], "updated": "2025-10-29", "summary": "Restricting who can SSH in using sshd_config directives.", "sections": [ { "heading": "The Config File", "body": "
SSH server configuration lives in /etc/ssh/sshd_config. Drop-in overrides can go in /etc/ssh/sshd_config.d/*.conf.
Always test your config before reloading:
", "code": "sshd -t\n# If it prints nothing and exits 0, the config is valid.\nsystemctl reload ssh" }, { "heading": "AllowUsers and AllowGroups", "body": "These are whitelist directives. If either is set, only matching users or group members can log in. If neither is set, all users may try.
", "code": "# Only these users may log in\nAllowUsers alice bob deploy\n\n# Only members of these groups may log in\nAllowGroups sshusers ops\n\n# Combining: user must match AllowUsers AND (if AllowGroups is set) be in an allowed group\n# These are independent filters—if both are set, a user must satisfy both." }, { "heading": "DenyUsers and DenyGroups", "body": "Blacklist alternatives. DenyUsers and DenyGroups are checked before Allow rules.
Prefer AllowUsers/AllowGroups over Deny lists—it is safer to enumerate who can in rather than who cannot.
You can apply different rules to specific users, groups, or source addresses:
", "code": "# Allow password auth only from the management network\nMatch Address 10.42.0.0/24\n PasswordAuthentication yes\n\n# Give one user a restricted shell\nMatch User backup-agent\n ForceCommand /usr/local/bin/backup-only\n AllowTcpForwarding no" }, { "heading": "Checking Who Has Access", "body": "There is no built-in command to list all users who currently satisfy the access rules. Check manually:
", "code": "# Current AllowUsers/AllowGroups settings\ngrep -iE '(AllowUsers|AllowGroups|DenyUsers|DenyGroups)' /etc/ssh/sshd_config\n\n# Members of a group\ngetent group sshusers\n\n# All users with a valid shell (can SSH in if no restrictions)\ngrep -v '/nologin\\|/false' /etc/passwd" } ] }