Restricting SSH Access by User and Group
What you'll learn: How to use SSH server directives to explicitly control which users and groups can (or cannot) authenticate via SSH.
The Core Idea
Even with public key authentication enabled, you may want to limit SSH access to specific users or groups as an additional layer of defense. The SSH server (sshd) provides four directives in its configuration file (/etc/ssh/sshd_config) that let you create allowlists and blocklists:
AllowUsers– only listed users can authenticateAllowGroups– only users in listed groups can authenticateDenyUsers– listed users are explicitly blockedDenyGroups– users in listed groups are explicitly blocked
Think of it like a nightclub bouncer: You can maintain a VIP list (AllowUsers/AllowGroups) that grants access only to specific people, or you can keep a banned list (DenyUsers/DenyGroups) that excludes troublemakers. You can even combine both approaches.
How It Works
Directives are evaluated in this order: DenyUsers, AllowUsers, DenyGroups, AllowGroups. If a user matches a Deny* rule, access is blocked immediately. If Allow* rules exist and the user doesn't match, access is denied.
Example configuration:
AllowUsers admin deploy
AllowGroups sshusers
DenyUsers guest
This setup allows only admin and deploy users, plus anyone in the sshusers group, while explicitly blocking the guest user.
Key Takeaway: Use AllowUsers, AllowGroups, DenyUsers, and DenyGroups directives in /etc/ssh/sshd_config to enforce granular access control, ensuring only authorized users and groups can authenticate via SSH—a critical hardening step for any system.