How to Harden SSH on Linux: 8 Essential Security Steps
This guide explains why the default SSH configuration on Linux is risky and provides eight concrete hardening measures—including disabling root login, changing the default port, enforcing key‑based authentication, limiting login attempts, and restricting access by IP—to secure your server against common attacks.
SSH is a widely used protocol for securely accessing Linux servers, but its default configuration can expose several security risks, especially when the root account is allowed remote access.
1. Disable root login
Create a new user with sudo privileges and prevent direct root SSH access.
useradd -m exampleroot
passwd exampleroot
usermod -aG sudo examplerootThen edit /etc/ssh/sshd_config to include:
# PermitRootLogin no
AllowUsers exampleroot2. Change the default port
The default SSH port (22) is well‑known to attackers. Change it to a non‑standard value, e.g., 22099, and restart the service. Port 22099 After editing the config, restart SSH:
sudo systemctl restart ssh3. Disallow empty passwords
Prevent accounts without passwords from logging in by setting:
PermitEmptyPasswords no4. Limit login attempts
Reduce brute‑force risk by limiting authentication retries:
MaxAuthTries 35. Enforce SSH protocol version 2
Protocol 2 is more secure than the legacy version. Add the following line to the config:
Protocol 26. Disable TCP and X11 forwarding
Prevent attackers from tunneling other services through SSH:
X11Forwarding no
AllowTcpForwarding no7. Use SSH key authentication
Generate a key pair with ssh-keygen, keep the private key secure, and place the public key on the server. This eliminates password‑based logins.
ssh-keygen8. Restrict SSH access by IP
Configure /etc/hosts.allow to allow only trusted IP ranges and deny all others.
After applying the above changes, restart the SSH service to apply the new settings.
Securing SSH is a fundamental part of protecting a Linux server; combined with other hardening measures, it significantly reduces the attack surface.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
