Why
SSH is a door into your server. This is especially true if you are opening ports on your router so you can SSH to your server from outside your home network. If it is not secured properly, a bad-actor could use it to gain unauthorized access to your system.How It Works
/etc/ssh/sshd_config is the default configuration file that the SSH server uses. We will use this file to tell what options the SSH server should use.
Goals
- A secure SSH configuration following industry best practices
Steps
Backup the SSH configuration
Make a backup of OpenSSH server’s configuration file
/etc/ssh/sshd_config and remove comments to make it easier to read:Add base security settings
Edit
/etc/ssh/sshd_config and add these settings that should be applied regardless of your configuration/setup:Note on OpenSSH 9.1+: If you are running OpenSSH 9.1 or later, uncomment the
RequiredRSASize 3072 line. This enforces a minimum RSA key size of 3072 bits and will reject smaller RSA keys during authentication. This only affects RSA keys. If you use ED25519 or ECDSA keys, you are not affected. You can check your key type and size with ssh-keygen -l -f ~/.ssh/id_rsa. On older OpenSSH versions, leave the line commented out as it will prevent sshd from starting.Add environment-specific settings
Add these settings and set values as per your requirements:
Check
| Setting | Valid Values | Example | Description |
|---|---|---|---|
| AllowGroups | local UNIX group name | AllowGroups sshusers | Group to allow SSH access to |
| ClientAliveCountMax | number | ClientAliveCountMax 3 | Maximum number of client alive messages sent without response |
| ClientAliveInterval | number of seconds | ClientAliveInterval 15 | Timeout in seconds before a response request |
| ListenAddress | local addresses | ListenAddress 0.0.0.0 or ListenAddress 192.168.1.100 | Local addresses sshd should listen on |
| LoginGraceTime | number of seconds | LoginGraceTime 30 | Time in seconds before login times-out |
| MaxAuthTries | number | MaxAuthTries 2 | Maximum allowed attempts to login |
| MaxSessions | number | MaxSessions 2 | Maximum number of open sessions |
| MaxStartups | number | MaxStartups 2 | Maximum number of login sessions |
| PasswordAuthentication | yes or no | PasswordAuthentication no | If login with a password is allowed |
| Port | port number | Port 22 | Port that sshd should listen on |
man sshd_config for more details on what these settings mean.Check for duplicate settings
Make sure there are no duplicate settings that contradict each other. The below command should not have any output:If there is output, you have duplicate settings that need to be resolved.
Verify the configuration
You can verify the configurations worked with You should see output like:
sshd -T:Key Security Settings Explained
Authentication & Access Control
- AllowGroups: Only users in the specified group can SSH in
- PermitRootLogin no: Root cannot login directly via SSH
- PasswordAuthentication: Controls if password login is allowed (recommend setting to
noafter setting up key-based auth) - PermitEmptyPasswords no: Accounts without passwords cannot login
Cryptography
- HostKey: Specifies which host key algorithms to use (Ed25519 is preferred)
- KexAlgorithms: Key exchange algorithms to use
- Ciphers: Encryption algorithms to use
- MACs: Message authentication code algorithms to use
Logging & Monitoring
- LogLevel VERBOSE: Logs key fingerprints for better audit tracking
Feature Restrictions
- X11Forwarding no: Disables X11 forwarding (security risk)
- AllowTcpForwarding no: Disables TCP port forwarding
- PermitTunnel no: Disables tunnel device forwarding
- AllowAgentForwarding no: Disables SSH agent forwarding