How to Secure SSH Access on Linux Servers

Secure SSH access on Linux servers by using key-based authentication, disabling password login, restricting root access, hardening sshd config, implementing firewall rules, enabling fail2ban, monitoring logs, and using 2FA where possible.

How to Secure SSH Access on Linux Servers

How to Secure SSH Access on Linux Servers

Understanding the Critical Importance of SSH Security

Every single day, thousands of automated attacks probe Linux servers searching for vulnerable SSH connections. Your server, regardless of its purpose or the sensitivity of its data, represents a potential target for malicious actors who continuously scan the internet for weaknesses. The consequences of a compromised server extend far beyond data lossβ€”they include service disruptions, financial damages, legal liabilities, and irreparable harm to your professional reputation. SSH security isn't just a technical checkbox; it's the foundational layer protecting everything you've built on your infrastructure.

Secure Shell protocol serves as the primary gateway for remote server administration, providing encrypted communication channels between clients and servers. While SSH offers robust security features by default, its default configuration leaves numerous vulnerabilities that attackers actively exploit. This comprehensive guide examines the multifaceted approach required to transform your SSH implementation from a potential liability into an impenetrable access control system, covering everything from basic configuration adjustments to advanced authentication mechanisms and monitoring strategies.

Throughout this exploration, you'll discover practical, immediately actionable techniques that system administrators and security professionals rely on to protect production environments. Whether you're managing a single virtual private server or orchestrating access across enterprise infrastructure, these strategies scale to meet your specific requirements. You'll gain insights into the reasoning behind each security measure, enabling you to make informed decisions about which protections best suit your operational needs while understanding the trade-offs between convenience and security.

Fundamental SSH Configuration Hardening

The SSH daemon configuration file, typically located at /etc/ssh/sshd_config, contains dozens of directives that control how your server accepts and processes connection requests. Many administrators overlook this file entirely, leaving default settings that were designed for maximum compatibility rather than maximum security. Before implementing any advanced security measures, you must establish a hardened baseline configuration that eliminates the most common attack vectors.

Begin by disabling root login entirely, which represents one of the most critical security improvements you can implement. Attackers universally know that the root account exists on every Linux system, eliminating half the challenge of unauthorized access. By requiring users to connect with individual accounts and escalate privileges through sudo, you create an audit trail and add an additional authentication layer. Modify your sshd_config file to include PermitRootLogin no, then restart the SSH service to apply this change.

Password-based authentication, while convenient, exposes your server to brute-force attacks where automated tools attempt thousands of login combinations per minute. Transitioning to key-based authentication fundamentally changes the security landscape, replacing guessable passwords with cryptographic keys that are computationally infeasible to crack. Generate SSH key pairs using the command ssh-keygen -t ed25519 -a 100 for modern, highly secure keys, or ssh-keygen -t rsa -b 4096 for maximum compatibility with older systems.

"The overwhelming majority of successful SSH compromises result from weak or default configurations that administrators simply never changed after initial server deployment."

After distributing your public key to the server using ssh-copy-id, disable password authentication entirely by setting PasswordAuthentication no in your configuration file. This single change eliminates entire categories of attacks, though it requires careful key management to avoid locking yourself out of your own systems. Always maintain backup access methods or console access before disabling password authentication on production servers.

Port Configuration and Network Exposure

SSH listens on port 22 by default, a fact universally known to attackers whose automated scanners specifically target this port. While changing the default port doesn't provide genuine security through obscurity, it dramatically reduces the volume of automated attack traffic your server must process. Consider moving SSH to a non-standard port above 1024, such as 2849 or 7822, by modifying the Port directive in your configuration file.

This approach offers several practical benefits beyond reduced log noise. Fewer connection attempts mean lower CPU utilization from authentication processes, reduced bandwidth consumption, and cleaner logs that make legitimate security events easier to identify. However, remember that determined attackers can still discover your SSH port through port scanning, so this measure complements rather than replaces other security controls.

  • Change the default SSH port to reduce automated attack traffic and improve log clarity
  • Implement firewall rules that restrict SSH access to specific IP addresses or network ranges
  • Configure TCP wrappers using /etc/hosts.allow and /etc/hosts.deny for additional access control layers
  • Enable connection rate limiting to prevent rapid-fire authentication attempts
  • Disable SSH protocol 1 entirely by ensuring your configuration specifies Protocol 2 exclusively

User Access Control and Account Management

Not every user account requires SSH access, yet many systems grant this capability indiscriminately. Implement the principle of least privilege by explicitly defining which users can authenticate through SSH. The AllowUsers directive in sshd_config accepts a space-separated list of usernames, restricting SSH access to only those accounts. Alternatively, use AllowGroups to manage access based on group membership, which scales more effectively in environments with numerous users.

For organizations with multiple administrators, consider creating a dedicated group such as "ssh-users" and adding only authorized personnel to this group. This approach centralizes access management and creates clear documentation of who possesses remote access capabilities. Regularly audit group membership to ensure former employees or contractors no longer retain access credentials.

Configuration Directive Recommended Value Security Impact Compatibility Considerations
PermitRootLogin no Eliminates attacks targeting the root account directly Requires sudo configuration for administrative tasks
PasswordAuthentication no Prevents brute-force password attacks entirely Requires key-based authentication setup before disabling
PermitEmptyPasswords no Prevents authentication with accounts that have no password Should always be disabled; no legitimate use cases exist
X11Forwarding no Reduces attack surface by disabling unnecessary features Disable unless you specifically require graphical application forwarding
MaxAuthTries 3 Limits authentication attempts per connection May require adjustment for users prone to typos
ClientAliveInterval 300 Automatically terminates idle sessions after specified seconds Balance between security and user convenience
ClientAliveCountMax 2 Number of keepalive messages before disconnection Works in conjunction with ClientAliveInterval

Advanced Authentication Mechanisms

While key-based authentication provides substantial security improvements over passwords, sophisticated environments benefit from implementing multi-factor authentication (MFA) that requires multiple independent credentials for access. Two-factor authentication for SSH typically combines something you have (your private key) with something you know (a time-based one-time password) or something you are (biometric data, though rarely used for server access).

πŸ” Google Authenticator integration represents one of the most accessible MFA implementations for SSH. After installing the libpam-google-authenticator package on Debian-based systems or google-authenticator on Red Hat variants, each user runs the google-authenticator command to generate their unique secret key and QR code. The system then requires both a valid SSH key and a current TOTP code from their authenticator application for successful authentication.

Configure PAM (Pluggable Authentication Modules) to enforce this additional authentication factor by editing /etc/pam.d/sshd and adding the line auth required pam_google_authenticator.so. Additionally, modify your sshd_config to include ChallengeResponseAuthentication yes and AuthenticationMethods publickey,keyboard-interactive. This configuration ensures that users must present both their private key and a valid one-time password.

"Multi-factor authentication transforms SSH from a single point of failure into a layered defense system where compromising one credential provides no access to the attacker."

Certificate-Based Authentication

SSH certificates provide an alternative to the traditional public key infrastructure that offers significant advantages for large-scale deployments. Rather than distributing individual public keys to every server, you establish a certificate authority (CA) that signs user keys, and servers trust any key signed by that CA. This approach dramatically simplifies key management, enables centralized revocation capabilities, and supports time-limited access grants.

Generate a CA key pair using ssh-keygen -t ed25519 -f ca-key, protecting this key with the highest security measures since it controls access to your entire infrastructure. Sign user keys with ssh-keygen -s ca-key -I user_identifier -n username -V +52w user-key.pub, creating a certificate valid for 52 weeks. Configure servers to trust this CA by adding TrustedUserCAKeys /etc/ssh/ca-key.pub to sshd_config.

Certificates support sophisticated access control through principals and critical options. The -n parameter specifies which usernames the certificate holder can access, while options like force-command restrict certificate holders to specific commands. For temporary contractor access, issue certificates with short validity periods, eliminating the need to manually revoke access when engagements conclude.

Hardware Security Keys

πŸ”‘ Physical security keys, such as YubiKeys or other FIDO2-compliant devices, provide the strongest possible authentication factor by storing private keys in tamper-resistant hardware that never exposes the key material to the host system. Modern SSH implementations support these devices through the FIDO/U2F protocol, combining the convenience of passwordless authentication with security that survives even complete system compromise.

Generate hardware-backed SSH keys using ssh-keygen -t ecdsa-sk for ECDSA keys or ssh-keygen -t ed25519-sk for Ed25519 keys, where the "-sk" suffix indicates security key storage. During key generation, you'll need to touch the physical device to confirm the operation. The resulting private key file contains only a reference to the hardware token, not the actual cryptographic material, meaning it's useless without the physical device.

This approach provides exceptional security for privileged accounts and high-value systems. Even if an attacker compromises your workstation and steals all your files, they cannot authenticate to your servers without physically possessing your security key. For organizations with compliance requirements or handling sensitive data, hardware security keys represent best-practice authentication.

Network-Level Security Controls

SSH security extends beyond the daemon configuration to encompass the network infrastructure that delivers connection requests to your server. Implementing defense-in-depth requires controlling which network sources can even attempt SSH connections, creating multiple barriers that attackers must overcome.

⚑ Firewall configuration provides your first line of defense, filtering traffic before it reaches the SSH daemon. Use iptables or its modern replacement nftables to create rules that explicitly allow SSH connections only from trusted IP addresses or networks. For dynamic environments where administrators connect from varying locations, consider implementing a VPN that provides a consistent source IP range, then restrict SSH access to VPN addresses exclusively.

A basic iptables rule allowing SSH only from a specific network might look like: iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 22 -j ACCEPT followed by iptables -A INPUT -p tcp --dport 22 -j DROP to reject all other SSH traffic. For servers requiring public SSH access, implement rate limiting with iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set and iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 -j DROP, which blocks sources attempting more than four connections per minute.

Port Knocking and Single Packet Authorization

Port knocking implements a "secret handshake" where the SSH port remains closed to all traffic until a client sends connection attempts to a specific sequence of ports. Only after receiving the correct knock sequence does the firewall temporarily open the SSH port for that source IP. While this adds complexity, it renders your SSH service invisible to port scanners and automated attacks.

Implement port knocking using the knockd daemon, defining a sequence in /etc/knockd.conf such as attempting connections to ports 7000, 8000, and 9000 in order. Upon receiving this sequence, the firewall adds a temporary rule allowing SSH access from that source. After a defined timeout period or a closing knock sequence, the rule removes itself, re-closing the port.

Single Packet Authorization (SPA) represents a more sophisticated evolution of port knocking, using encrypted, single-packet messages to authenticate connection requests before opening firewall rules. The fwknop implementation provides SPA capabilities, requiring clients to send properly encrypted authorization packets before the SSH port becomes accessible. This approach eliminates the timing considerations of traditional port knocking while providing stronger security through cryptographic authentication.

Virtual Private Networks and Bastion Hosts

🌐 For production environments, consider removing SSH from public internet exposure entirely by requiring connections through a VPN. This architecture change fundamentally alters your security posture, as attackers must first compromise your VPN infrastructure before even attempting SSH authentication. WireGuard, OpenVPN, or IPsec VPNs create encrypted tunnels that authenticate users before granting network access, adding a complete authentication layer before SSH enters the picture.

Alternatively, implement a bastion host (jump box) architecture where a single hardened server accepts SSH connections from the internet, and users then connect from the bastion to internal servers. This concentrates your security efforts on thoroughly hardening one system rather than maintaining consistent security across dozens or hundreds of servers. Configure SSH agent forwarding or ProxyJump directives to make multi-hop connections transparent to users while maintaining the security benefits of centralized access control.

"The most secure SSH configuration is one that never receives connection attempts from untrusted networks in the first place."

Monitoring, Logging, and Intrusion Detection

Even perfectly configured SSH security measures prove ineffective if you never examine logs or respond to suspicious activity. Comprehensive monitoring transforms your SSH infrastructure from a static defense into an active security system that detects and responds to threats in real-time.

SSH logs, typically found in /var/log/auth.log on Debian-based systems or /var/log/secure on Red Hat variants, record every authentication attempt, successful connection, and session termination. Regular log review reveals attack patterns, identifies compromised credentials, and provides forensic evidence for security incidents. However, manual log review quickly becomes impractical as connection volume increases, necessitating automated analysis tools.

πŸ“Š Fail2ban provides automated intrusion prevention by monitoring log files for suspicious patterns and temporarily blocking offending IP addresses. After installing fail2ban, configure a jail for SSH by editing /etc/fail2ban/jail.local to include parameters like maxretry = 3 (number of failures before banning) and bantime = 3600 (seconds to maintain the ban). Fail2ban automatically adds firewall rules blocking sources that exceed your defined thresholds, dramatically reducing the effectiveness of brute-force attacks.

Monitoring Tool Primary Function Deployment Complexity Best Use Case
Fail2ban Automated IP blocking based on log patterns Low - simple installation and configuration Individual servers or small deployments
OSSEC Host-based intrusion detection system Medium - requires agent deployment and central server Environments requiring comprehensive security monitoring
Logwatch Log analysis and summarization Low - minimal configuration needed Daily email summaries of system activity
Elastic Stack Centralized logging and analysis platform High - requires dedicated infrastructure Large-scale deployments with multiple servers
Auditd Linux kernel-level auditing framework Medium - requires careful rule configuration Compliance environments requiring detailed audit trails

Centralized Logging and SIEM Integration

As your infrastructure grows beyond a handful of servers, centralized logging becomes essential for maintaining security visibility. Rather than connecting to individual servers to review logs, configure all systems to forward authentication events to a central logging server using rsyslog or syslog-ng. This approach provides several critical benefits: logs survive even if an attacker compromises and wipes a server, analysis tools can correlate events across multiple systems, and you maintain a comprehensive audit trail for compliance purposes.

For enterprise environments, integrate SSH logs into your Security Information and Event Management (SIEM) system. Solutions like Splunk, Elastic Security, or open-source alternatives like Wazuh provide sophisticated analysis capabilities that identify attack patterns humans might miss. Configure alerts for events such as successful authentications from new geographic locations, authentication attempts for disabled accounts, or unusual connection times that deviate from established patterns.

Session Recording and Audit Trails

πŸŽ₯ Beyond logging authentication events, consider implementing session recording that captures everything users type during SSH sessions. Tools like tlog or asciinema record complete terminal sessions, providing invaluable forensic evidence when investigating security incidents or troubleshooting configuration changes. This capability proves particularly valuable for privileged accounts where understanding exactly what commands were executed becomes critical.

Configure sudo to log all commands executed with elevated privileges, creating a comprehensive audit trail of administrative actions. Edit /etc/sudoers using visudo to include Defaults log_output and Defaults log_input, which records both the commands executed and their output. Combined with SSH session logging, this provides complete visibility into administrative activities.

"Security monitoring isn't about distrusting your team; it's about maintaining the evidence you'll need to understand and respond to incidents when they inevitably occur."

SSH Key Management Best Practices

The security of key-based authentication depends entirely on how carefully you manage those keys throughout their lifecycle. Poor key management practices can completely undermine otherwise excellent SSH security configurations, transforming your authentication system from a strength into a vulnerability.

Generate unique key pairs for each device you use to connect to servers, rather than copying a single private key across multiple workstations. This practice limits the impact of a compromised device to only that specific key, rather than exposing access from all your systems. When you replace a laptop or workstation, simply remove its public key from your servers' authorized_keys files, confident that your other devices retain access.

⚠️ Protect private keys with strong passphrases, treating them as the critical secrets they represent. A passphrase-protected key remains secure even if an attacker gains access to your file system, as they cannot use the key without also knowing the passphrase. Use ssh-agent to cache decrypted keys in memory, providing convenient passwordless authentication while maintaining the security benefits of passphrase protection.

Key Rotation and Revocation

Establish a key rotation policy that requires generating new key pairs periodically, such as annually or when personnel changes occur. While SSH keys don't expire by default (unlike certificates), regular rotation limits the window of vulnerability if a key becomes compromised without your knowledge. Document your rotation schedule and implement reminders to ensure compliance.

Maintain an inventory of all authorized keys across your infrastructure, documenting which keys belong to which users and devices. This inventory becomes critical when you need to revoke access, whether due to employee departures, lost devices, or suspected compromises. Implement automation to deploy and remove keys across multiple servers simultaneously, avoiding the error-prone process of manually editing authorized_keys files on dozens of systems.

For immediate revocation needs, consider implementing the AuthorizedKeysCommand directive in sshd_config, which executes a script to dynamically retrieve authorized keys from a central database rather than reading a static file. This approach enables instant key revocation by updating the central database, after which the change takes effect on all servers within seconds.

Secure Key Storage and Distribution

Never store private keys in cloud storage services, email them to yourself, or otherwise transmit them across networks. Private keys should remain exclusively on the devices where they were generated, protected by file system permissions (chmod 600) that prevent other users from reading them. For backup purposes, store encrypted copies on offline media in physically secure locations.

When distributing public keys to servers, use secure channels such as existing SSH connections, configuration management systems, or physically accessing the server console. Avoid sending public keys through unencrypted email or messaging systems where they might be intercepted and replaced with attacker-controlled keys. While public keys aren't secret, ensuring their authenticity prevents man-in-the-middle attacks during initial setup.

"The strongest SSH security configuration becomes worthless the moment an attacker obtains a valid private key through poor key management practices."

Configuration Management and Automation

Maintaining consistent SSH security configurations across multiple servers manually invites configuration drift, where some systems receive security updates while others remain vulnerable. Configuration management tools like Ansible, Puppet, Chef, or SaltStack ensure every server maintains identical security settings, automatically correcting any deviations from your defined security baseline.

Create an Ansible playbook that defines your complete SSH security configuration, including daemon settings, firewall rules, fail2ban configuration, and authorized keys. Execute this playbook against all servers whenever you make security policy changes, ensuring immediate, consistent deployment. This approach eliminates the possibility of forgetting to update a server or making typos in configuration files.

Configuration management also enables rapid response to security vulnerabilities. When security researchers discover a new SSH vulnerability, you can update your configuration management templates, test the changes in a development environment, and deploy the fix across hundreds of servers in minutes rather than hours or days of manual work.

Infrastructure as Code

Treat your SSH security configuration as code, maintaining it in version control systems like Git. This practice provides several critical benefits: you maintain a complete history of all configuration changes with explanations of why changes were made, you can review proposed changes before deployment through pull requests, and you can instantly roll back problematic changes by reverting to previous versions.

Document your SSH security policies in README files alongside your configuration code, explaining the reasoning behind each setting and any trade-offs you've accepted. This documentation proves invaluable when onboarding new team members or revisiting configuration decisions months or years later. Include information about testing procedures and rollback plans in case deployments cause unexpected issues.

Specialized Security Considerations

Different environments face unique security challenges that require specialized approaches beyond general best practices. Understanding these specific scenarios helps you tailor your SSH security implementation to your actual risk profile and operational requirements.

Container and Cloud-Native Environments

Container platforms like Docker and Kubernetes introduce unique SSH security considerations. Rather than enabling SSH access to individual containers, implement proper logging and debugging tools that work within the container paradigm. Use kubectl exec for Kubernetes or docker exec for standalone containers, which provide access through the orchestration platform's authentication and authorization systems rather than managing separate SSH credentials.

For cloud instances, leverage cloud provider features like AWS Systems Manager Session Manager or Google Cloud's OS Login, which provide SSH-like access without requiring open SSH ports or managing SSH keys. These services integrate with your cloud provider's identity and access management systems, enabling centralized access control and comprehensive audit logging through cloud-native tools.

Compliance and Regulatory Requirements

Organizations subject to compliance frameworks like PCI DSS, HIPAA, or SOC 2 face specific SSH security requirements. PCI DSS, for example, explicitly requires multi-factor authentication for all remote access to systems handling cardholder data. Ensure your SSH configuration meets your applicable compliance requirements, and document how your implementation satisfies each control.

Implement comprehensive logging that captures all required audit events, including authentication attempts (successful and failed), privileged command execution, and session durations. Configure log retention periods that meet your compliance requirements, typically ranging from 90 days to several years depending on the framework. Ensure logs are tamper-evident through cryptographic signing or write-once storage systems.

High-Security Environments

For systems handling extremely sensitive data or operating in hostile threat environments, consider additional hardening measures beyond standard best practices. Implement SSH honeypots on unused ports to detect and track attackers, providing early warning of targeted attacks. Use kernel-level security modules like SELinux or AppArmor to confine the SSH daemon, limiting the impact of potential vulnerabilities.

Consider implementing time-based access controls where SSH access is only permitted during specific hours, automatically blocking connections outside maintenance windows. This approach dramatically reduces your attack surface, as the SSH port remains completely closed except during scheduled access periods. Combine this with alerting systems that notify security teams of any SSH activity outside approved timeframes.

Testing and Validation

Security configurations only provide protection if they're correctly implemented and functioning as intended. Regular testing validates that your SSH security measures work correctly and identifies configuration errors before attackers exploit them.

Use SSH auditing tools like ssh-audit or Lynis to automatically analyze your SSH configuration and identify security weaknesses. These tools check for weak cryptographic algorithms, insecure configuration directives, and outdated SSH versions. Run these audits regularly, incorporating them into your routine security maintenance schedule.

Conduct penetration testing exercises where you attempt to compromise your own SSH infrastructure using common attack techniques. Try brute-force attacks against your fail2ban configuration to verify it blocks attackers effectively. Attempt connections from unauthorized IP addresses to confirm your firewall rules work correctly. This hands-on testing reveals gaps that theoretical configuration review might miss.

Continuous Security Monitoring

Implement continuous monitoring that alerts you to potential security issues in real-time rather than discovering them during periodic audits. Configure alerts for events such as SSH configuration file modifications, new authorized keys added to user accounts, or SSH daemon restarts. These events might indicate legitimate administrative actions or could signal an attacker attempting to establish persistence.

Monitor for indicators of compromise such as connections from unexpected geographic locations, authentication attempts for accounts that should never connect via SSH, or unusual connection patterns that deviate from established baselines. Machine learning-based anomaly detection systems can identify subtle attack patterns that rule-based systems miss, providing an additional layer of security monitoring.

Recovery and Incident Response

Despite your best security efforts, incidents will eventually occur. Whether through zero-day vulnerabilities, social engineering, or simple human error, you must prepare for the possibility of SSH security compromises. Having a well-defined incident response plan enables rapid, effective response that minimizes damage.

Maintain out-of-band access to your servers through console connections, bastion hosts with separate authentication systems, or cloud provider management interfaces. This backup access ensures you can respond to incidents even if SSH itself becomes compromised or misconfigured. Test these backup access methods regularly to ensure they work when needed.

Document your SSH security incident response procedures, including steps for identifying compromised keys, revoking access, analyzing logs to determine the scope of compromise, and restoring secure access. Practice these procedures through tabletop exercises or simulated incidents, identifying gaps in your response capabilities before facing real emergencies.

"The time to develop your incident response plan is before an incident occurs, not while frantically responding to an active security breach."

Emerging Technologies and Future Considerations

SSH security continues evolving as new threats emerge and technologies advance. Staying informed about developing trends helps you anticipate future security requirements and evaluate new tools that might improve your security posture.

Post-quantum cryptography represents a significant upcoming change, as quantum computers threaten to break the cryptographic algorithms SSH currently relies upon. While practical quantum computers remain years away, organizations with long-term security requirements should begin planning their transition to quantum-resistant algorithms. The SSH protocol development community is actively working on post-quantum key exchange mechanisms that will eventually replace current methods.

Zero Trust architecture principles are increasingly being applied to SSH access, replacing perimeter-based security models with continuous verification of every connection. Rather than trusting connections from inside your network, Zero Trust implementations authenticate and authorize every SSH session individually, regardless of source network. This approach better aligns with modern cloud-native infrastructures where traditional network perimeters no longer exist.

Frequently Asked Questions
What is the most important single change I can make to improve SSH security?

Disabling password authentication and requiring key-based authentication provides the most significant security improvement with a single configuration change. This eliminates brute-force password attacks entirely, which represent the vast majority of SSH attack attempts. After properly implementing key-based authentication and verifying you can connect successfully, set PasswordAuthentication to no in your sshd_config file and restart the SSH service.

How do I recover access if I accidentally lock myself out after changing SSH configuration?

Always maintain alternative access methods before making SSH configuration changes. For cloud instances, use your provider's web console or serial console access. For physical servers or virtual machines, use direct console access through a KVM or virtualization platform. Before making significant SSH changes, consider keeping a second SSH daemon running on an alternate port with a more permissive configuration as a backup access method, removing it only after confirming your primary configuration works correctly.

Should I use RSA or Ed25519 keys for SSH authentication?

Ed25519 keys provide better security and performance than RSA keys with much smaller key sizes. Generate Ed25519 keys using ssh-keygen -t ed25519 -a 100 for modern systems. However, if you need compatibility with older systems or devices that don't support Ed25519, use RSA keys with a minimum length of 4096 bits generated with ssh-keygen -t rsa -b 4096. Never use DSA or ECDSA keys, as they have known security weaknesses.

How often should I rotate SSH keys?

Unlike passwords, SSH keys don't require frequent rotation if properly protected. Implement annual rotation as a baseline practice, or rotate keys immediately when specific events occur: employee departures, lost or stolen devices, suspected compromise, or changes in access requirements. For high-security environments, consider more frequent rotation such as quarterly. Certificate-based authentication simplifies rotation by enabling automatic expiration rather than manual key replacement.

Is changing the default SSH port an effective security measure?

Changing the SSH port reduces automated attack traffic and log noise but doesn't prevent determined attackers who can easily discover your SSH port through port scanning. Consider it a supplementary measure that improves operational efficiency rather than a primary security control. Implement port changes alongside substantive security measures like key-based authentication, firewall restrictions, and multi-factor authentication for meaningful security improvements.

What's the difference between AllowUsers and AllowGroups directives?

AllowUsers specifies individual usernames that can authenticate via SSH, while AllowGroups specifies system groups whose members can authenticate. AllowGroups scales better for environments with multiple users, as you can manage access by adding or removing users from the group rather than editing the SSH configuration file. Use AllowUsers for small deployments with few administrators, and AllowGroups for larger environments where access management needs to be delegated or frequently changed.

How do I implement SSH access for automated systems and service accounts?

Create dedicated service accounts with restricted shells that can only execute specific commands required for automation. Use the ForceCommand directive in authorized_keys files or sshd_config to restrict these accounts to predefined commands. Generate unique keys for each automated system, and implement strict key management including regular rotation and immediate revocation when systems are decommissioned. Consider using SSH certificates with short validity periods for automated access, enabling automatic expiration without manual intervention.

What should I monitor in SSH logs to detect security issues?

Monitor for failed authentication attempts from unusual sources, successful authentications from unexpected geographic locations or IP addresses, authentication attempts for disabled or non-existent accounts, connections outside normal business hours, and multiple failed attempts followed by successful authentication (indicating possible credential compromise). Implement automated alerting for these patterns rather than relying on manual log review, as the volume of SSH activity quickly becomes overwhelming to analyze manually.