How to Harden SSH on Linux: 8 Essential Security Steps

This guide explains why the default SSH configuration is risky and provides eight concrete actions—including disabling root login, changing the default port, limiting authentication attempts, and using key‑based authentication—to secure Linux servers against common attacks.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Harden SSH on Linux: 8 Essential Security Steps

SSH is a widely used protocol for securely accessing Linux servers, but its default settings can expose several security risks, especially when root login is allowed over a public IP address.

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 exampleroot

Then edit /etc/ssh/sshd_config to add:

# PermitRootLogin no
AllowUsers exampleroot

Restart the SSH service:

sudo systemctl restart ssh

2. Change the default port

Modify the port number in /etc/ssh/sshd_config to make automated scans harder. Port 22099 After saving, restart SSH and, if a firewall is used, adjust the rules accordingly. Verify the change with netstat -tlpn.

3. Disallow empty passwords

Set PermitEmptyPasswords no in sshd_config to block accounts without passwords.

PermitEmptyPasswords no

4. Limit authentication attempts

Reduce the number of allowed password tries to mitigate brute‑force attacks.

MaxAuthTries 3

5. Enforce SSH protocol version 2

Enable the more secure protocol by adding:

Protocol 2

6. Disable TCP and X11 forwarding

Prevent attackers from tunneling other services through SSH.

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

Edit /etc/hosts.allow (or firewall rules) to permit only trusted IP ranges and deny all others.

After applying these configurations, restart the SSH daemon to apply the changes.

Why Linux server security matters

Securing SSH is a fundamental step in protecting Linux servers, which host critical data and services. Proper hardening reduces the attack surface and helps prevent unauthorized access, complementing broader server‑hardening practices.

SSH configuration example
SSH configuration example
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.

ConfigurationlinuxSecurityServerSSHHardening
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.