SSHwatch Insights Blog

5 Advanced SSH Hardening Techniques That Security Professionals Actually Use

In the constant battle against unauthorized access, SSH remains your first line of defense for server security. While most administrators implement basic SSH protections, truly securing your infrastructure requires going beyond default configurations. This guide explores five advanced SSH hardening techniques that security professionals rely on but rarely discuss in typical documentation.

1. Implement Certificate-Based Authentication with Principals

Password-based authentication—even with strong passwords—introduces unnecessary risk. While key-based authentication offers significant improvements, certificate authentication with principals provides enterprise-grade control over user access.

# On your CA (Certificate Authority) server
# Create a CA key if you don't have one
ssh-keygen -t ed25519 -f /etc/ssh/ca_key -C "SSH CA Key"

# Sign user keys with specific principals (roles)
ssh-keygen -s /etc/ssh/ca_key -I [email protected] -n "admin,developer" -V +52w /path/to/user_key.pub

# On each target server, configure trusted CA
echo "@cert-authority * $(cat /etc/ssh/ca_key.pub)" >> /etc/ssh/trusted_user_ca_keys
echo "TrustedUserCAKeys /etc/ssh/trusted_user_ca_keys" >> /etc/ssh/sshd_config
echo "AuthorizedPrincipalsFile /etc/ssh/auth_principals/%u" >> /etc/ssh/sshd_config

# Create principal files that control which roles can access which accounts
mkdir -p /etc/ssh/auth_principals
echo "admin" > /etc/ssh/auth_principals/root
echo "developer" > /etc/ssh/auth_principals/deploy

What’s happening here? Instead of managing individual authorized_keys files across your infrastructure, this approach establishes a Certificate Authority (CA) that signs user SSH keys. Each signed key includes “principals” (like admin or developer) that determine what accounts the key can access. When a user connects, the server verifies the certificate against its trusted CA list and checks if the principal in the certificate matches what’s allowed for the target account.

The key benefits are centralized management (revoke access everywhere by removing the certificate), time-based expiration (the -V +52w sets a 52-week validity period), and precise access control without modifying individual server configurations when permissions change.

This approach provides centralized access control, automatic key rotation, and fine-grained authorization that far exceeds traditional methods.

2. Deploy Port Knocking with Single Packet Authorization

Rather than leaving your SSH port continuously exposed, implement port knocking with Single Packet Authorization (SPA) to make your server virtually invisible to reconnaissance scans.

# Install fwknop
apt-get install fwknop-server fwknop-client

# Configure the server
cat > /etc/fwknop/access.conf << EOF
SOURCE: ANY
KEY: $(dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64)
OPEN_PORTS: tcp/22
FW_ACCESS_TIMEOUT: 30
EOF

# On client side
fwknop -A tcp/22 -a $(curl ifconfig.me) -D server_ip
ssh user@server_ip

Unlike traditional port knocking which requires sending connection attempts to a sequence of ports, Single Packet Authorization (SPA) uses a single encrypted packet containing authentication credentials and your request. This approach is superior because it’s not vulnerable to passive network monitoring that could reveal knocking sequences.

When you run the client command, fwknop sends an encrypted UDP packet to the server. If authenticated successfully, the server temporarily opens the SSH port (for 30 seconds in this example) only to your specific IP address. To any other observer or scanner, port 22 appears completely closed—making your server essentially invisible to automated scanning tools and dramatically reducing your attack surface.

This technique ensures your SSH port remains closed and invisible to automated scanning tools until a properly authenticated SPA packet opens it temporarily for your specific IP address.

3. Configure Session Multiplexing with Restricted Command Sets

For operational accounts that need specific access patterns, implement ControlMaster configurations with command restrictions:

# On the server, create a restricted key for specific commands
echo 'command="sudo service nginx reload",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-ed25519 AAAA....' >> /home/deploy/.ssh/authorized_keys

# On the client, set up multiplexing for efficiency
cat >> ~/.ssh/config << EOF
Host production
    HostName server.example.com
    ControlMaster auto
    ControlPath ~/.ssh/sockets/%r@%h:%p
    ControlPersist 10m
    User deploy
EOF

This technique combines two powerful concepts. First, the command restriction in authorized_keys ensures that even if this particular key is compromised, an attacker can only execute the specific command defined—in this case, reloading Nginx. They cannot get an interactive shell or run any other commands, significantly limiting the potential damage.

The second part implements SSH multiplexing, which keeps a single connection open and reuses it for subsequent SSH commands. This provides performance benefits (avoiding repeated authentication) while also reducing the number of authentication attempts visible in logs, making it easier to spot unusual patterns. The ControlPersist 10m keeps the master connection open for 10 minutes after your last SSH command, allowing rapid successive operations without new connection attempts.

This configuration improves both security and efficiency by limiting what commands can run through specific keys while reducing authentication overhead.

4. Implement Time-Based Access Controls

Restrict SSH access to specific time windows to reduce the attack surface during off-hours:

# Install pam_time module
apt-get install libpam-modules

# Configure time-based restrictions
echo "sshd;*;admin;Al0800-1700" >> /etc/security/time.conf

# Update PAM configuration
echo "account required pam_time.so" >> /etc/pam.d/sshd

This configuration uses Linux’s PAM (Pluggable Authentication Modules) system to restrict when users can log in. The line added to time.conf breaks down as:

  • sshd – The service being restricted
  • * – Applies to all terminals
  • admin – The user account being restricted
  • Al0800-1700 – Allow access only from 8:00 AM to 5:00 PM on all days

This simple approach is effective because most legitimate administrative access occurs during business hours, while attacks often target systems during off-hours when monitoring might be reduced.

For more granular control, you can also use fail2ban with custom time-based rules:

# Create custom jail in fail2ban
cat > /etc/fail2ban/jail.d/sshd-timeaccess.conf << EOF
[sshd-timeaccess]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
timeban = 86400
bantime = 86400
findtime = 600
maxretry = 1
datepattern = %%a %%H:%%M:%%S
EOF

# Add custom filter to block access during specific times
cat > /etc/fail2ban/filter.d/sshd-timeaccess.conf << EOF
[Definition]
failregex = .* Connection from <HOST> port .*
datepattern = {^LN-BEG}%%ExY(?P<_sep>[-/.])%%m(?P=_sep)%%d[T ]%%H:%%M:%%S(?:[.,]%%f)?(?:\s*%%z)?
[Init]
journalmatch = _SYSTEMD_UNIT=sshd.service
EOF

This fail2ban configuration creates a more dynamic approach by establishing a custom jail that can automatically ban IP addresses that attempt connections outside permitted hours. The bantime = 86400 setting imposes a 24-hour ban on violating IPs. This can be particularly effective against automated scanning and brute-force tools that don’t respect business hours.

5. Implement Host-Based Intrusion Detection with SSH Behavioral Analysis

Monitor SSH sessions for anomalous behavior using automated analysis tools:

# Install OSSEC for host-based intrusion detection
apt-get install ossec-hids-server

# Configure SSH-specific rules
cat >> /var/ossec/etc/ossec.conf << EOF
<rule id="100001" level="7">
  <if_sid>5710</if_sid>
  <match>^Failed password</match>
  <description>Multiple failed logins in a short period</description>
</rule>
EOF

# Enable session recording and behavior analysis
apt-get install auditd

cat > /etc/audit/rules.d/ssh-session.rules << EOF
-a exit,always -F arch=b64 -S execve -F path=/usr/bin/ssh -k SSH_CLIENT
-w /var/log/auth.log -p wa -k SSH_LOG
EOF

# Restart audit daemon
service auditd restart

This approach focuses on detecting unusual patterns that might indicate compromise even after successful authentication. OSSEC is a powerful host-based intrusion detection system that can correlate events across multiple log sources to identify attack patterns. The custom rule shown here monitors for failed password attempts, but OSSEC can be configured with much more sophisticated detection logic.

The second part uses Linux’s audit system to track SSH activity at a deeper level. The first audit rule (-a exit,always -F arch=b64 -S execve -F path=/usr/bin/ssh -k SSH_CLIENT) records every SSH command execution with its parameters, allowing you to detect unusual commands. The second rule (-w /var/log/auth.log -p wa -k SSH_LOG) monitors for any modifications to the SSH log files, which attackers often attempt to alter to cover their tracks.

When combined with log analysis tools like ELK Stack (Elasticsearch, Logstash, Kibana) or Splunk, this data allows you to establish baseline SSH usage patterns and detect anomalies that could indicate compromised credentials or lateral movement within your network.

For larger environments, consider implementing a centralized log analysis system with SSH-specific behavioral patterns to detect credential theft, unusual access patterns, or data exfiltration attempts.

Conclusion

While implementing these advanced techniques requires more effort than basic SSH hardening, the security benefits are substantial. The most sophisticated attacks target SSH precisely because it provides such valuable access. By implementing certificate-based authentication, hidden services with SPA, command restrictions, time-based controls, and behavioral monitoring, you create a defense-in-depth approach that significantly raises the bar for attackers.

Remember that SSH security is never a “set and forget” operation—regularly audit your configurations, rotate certificates, and update your security posture as new threats and techniques emerge.

Secure Your Infrastructure Today!

Sign up now to gain comprehensive insights into your SSH access logs. Start monitoring, alerting, and analyzing your entire infrastructure effortlessly.
Get started for free

Book a demo

Fill in the form below to book a demo without obligation.
Request a demo