SSH Hardening on Linux — 7 Steps to Lock Down Remote Access

SSH Hardening on Linux — 7 Steps to Lock Down Remote Access

Derrk SamuelJune 6, 20267 min read
Default SSH settings are a security risk. This guide walks you through the essential hardening steps: disabling root login, using key-based auth, changing the default port, and more.

SSH Hardening on Linux

The default SSH configuration on most Linux servers is functional but not secure. Bots and automated scanners probe port 22 constantly. These 7 steps significantly reduce your attack surface.


Step 1 — Generate an SSH Key Pair (On Your Local Machine)

Before hardening, set up key-based auth so you don't lock yourself out when you disable password login.

On your local machine:

ssh-keygen -t ed25519 -C "your-label"

This creates two files in ~/.ssh/:

  • id_ed25519 — private key (keep this safe, never share it)
  • id_ed25519.pub — public key (this goes on the server)

Step 2 — Copy Your Public Key to the Server

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@your-server-ip

Or manually append it to the server's ~/.ssh/authorized_keys:

cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Test that key-based login works before disabling password auth:

ssh -i ~/.ssh/id_ed25519 user@your-server-ip

Step 3 — Edit the SSH Config File

Open the SSH daemon config on the server:

sudo nano /etc/ssh/sshd_config

Step 4 — Disable Root Login

Find and set:

PermitRootLogin no

Root is the most targeted account. Never allow direct root SSH access.


Step 5 — Disable Password Authentication

Once key-based auth works, disable passwords entirely:

PasswordAuthentication no
ChallengeResponseAuthentication no

This makes brute-force attacks impossible — there's no password to guess.


Step 6 — Change the Default Port

Change from port 22 to something less targeted:

Port 2222

This won't stop determined attackers but eliminates noise from mass scanners targeting port 22.

Important: Also update your firewall:

sudo ufw allow 2222/tcp
sudo ufw delete allow 22/tcp

Step 7 — Restrict Login to Specific Users

Limit SSH access to specific usernames:

AllowUsers yourusername

Or restrict to a specific group:

AllowGroups sshusers

Apply the Changes

After editing sshd_config, reload SSH:

sudo systemctl reload sshd

Test the new connection in a new terminal window before closing your current session.


Bonus: Fail2Ban (Auto-Block Attackers)

Install Fail2Ban to automatically ban IPs that fail login attempts:

sudo apt install fail2ban
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

Check its status:

sudo fail2ban-client status sshd

Hardening Checklist

StepSettingValue
Root loginPermitRootLoginno
Password authPasswordAuthenticationno
Default portPortNon-22 value
Allowed usersAllowUsersYour username only
Key typeKey paired25519
Brute forceFail2BanEnabled

These steps combined make your SSH setup resistant to the vast majority of automated attacks.

D

Derrk Samuel

Web3, crypto, and Linux PC insights — practical guides, security tips, mining setups, blockchain tools, and decentralized tech strategies for techs.

Discussion

Leave a comment

No comments yet. Be the first to start the discussion!