How to Harden SSH on Linux: 8 Essential Security Steps
This guide explains why the default SSH configuration is risky and provides eight practical measures—including disabling root login, changing the default port, restricting password use, and employing key‑based authentication—to secure SSH access on Linux servers.
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 accessible over a public IP.
The following steps show how to protect SSH connections on Linux.
1. Disable root login
Create a new user with sudo privileges and prevent the root account from logging in via SSH.
useradd -m exampleroot
passwd exampleroot
usermod -aG sudo examplerootThen edit /etc/ssh/sshd_config to add:
# Authentication:
#LoginGraceTime 2m
PermitRootLogin no
AllowUsers examplerootRestart the SSH service:
sudo systemctl restart ssh2. Change the default port
The default SSH port (22) is well‑known to attackers. Change it to a non‑standard port, e.g., 22099.
Include /etc/ssh/sshd_config.d/*.conf
Port 22099Restart SSH and adjust firewall rules accordingly.
3. Disallow empty passwords
Prevent users without passwords from logging in by setting:
PermitEmptyPasswords no4. Limit login attempts
Reduce the number of allowed authentication attempts to mitigate brute‑force attacks.
MaxAuthTries 35. Use SSH protocol version 2
Enable the more secure SSH‑2 protocol:
Include /etc/ssh/sshd_config.d/*.conf
Protocol 26. Disable TCP and X11 forwarding
Prevent attackers from tunneling through SSH by disabling forwarding:
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 or specific addresses, denying all others.
After applying these settings, restart the SSH service to apply changes.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
