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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Harden SSH on Linux: 8 Essential Security Steps

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 exampleroot

Then edit /etc/ssh/sshd_config to add:

# Authentication:
#LoginGraceTime 2m
PermitRootLogin no
AllowUsers exampleroot

Restart the SSH service:

sudo systemctl restart ssh

2. 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 22099

Restart SSH and adjust firewall rules accordingly.

3. Disallow empty passwords

Prevent users without passwords from logging in by setting:

PermitEmptyPasswords no

4. Limit login attempts

Reduce the number of allowed authentication attempts to mitigate brute‑force attacks.

MaxAuthTries 3

5. Use SSH protocol version 2

Enable the more secure SSH‑2 protocol:

Include /etc/ssh/sshd_config.d/*.conf 
Protocol 2

6. Disable TCP and X11 forwarding

Prevent attackers from tunneling through SSH by disabling forwarding:

X11Forwarding no 
AllowTcpForwarding no

7. 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-keygen

8. 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxSecuritySysadminServer HardeningSSH
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.