Learn › Linux

Linux SSH Keys and Server Hardening Basics

Set up SSH key login, disable password and root login safely, and harden Linux servers with updates, firewall, fail2ban, sudo and auditing.

Advanced 11 min read
By Gopinath Karthikeyan

SSH is how you – and attackers – reach a Linux server. Locking it down with keys and a few config changes stops the automated password-guessing that hits every internet-facing server within minutes. Then a short list of habits covers most of the rest.

Create a key pair (on your machine)

Windows 10/11 has OpenSSH built in, so this works in PowerShell as well as Linux:

ssh-keygen -t ed25519 -C "alice-laptop"

Accept the default path, and set a passphrase – it protects the key if your laptop is stolen. You get:

  • id_ed25519 – the private key. Never share or copy it to servers.
  • id_ed25519.pub – the public key. Safe to put on servers.

Install the public key on the server

ssh-copy-id alice@srv01.contoso.local       # from Linux/WSL

Or manually: append the .pub line to ~/.ssh/authorized_keys on the server, then:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Test in a new terminal: ssh alice@srv01.contoso.local should log in without a password.

Harden sshd

Keep your current session open, then edit /etc/ssh/sshd_config (or add a file under /etc/ssh/sshd_config.d/):

PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowGroups ssh-users
MaxAuthTries 3
sudo groupadd -f ssh-users && sudo usermod -aG ssh-users alice
sudo sshd -t                     # test config – no output means OK
sudo systemctl reload ssh        # 'sshd' on RHEL

Now test a new login before closing your existing session. If it fails, you can still fix it from the open one.

Firewall and brute-force protection

sudo ufw allow from 10.0.5.0/24 to any port 22 proto tcp   # SSH only from admin network
sudo ufw enable
sudo apt install -y fail2ban      # bans IPs after repeated failures
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

Core hardening checklist

  1. Updates – sudo apt update && sudo apt upgrade regularly; enable unattended-upgrades for security patches.
  2. Minimal software – remove packages and stop services you don’t use (ss -tulpn shows what listens).
  3. No shared accounts – each admin has a named account; sudo rights through a group.
  4. Least-privilege sudo – service accounts get only the commands they need, e.g. in /etc/sudoers.d/jenkins:
    jenkins ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp
    
  5. Secrets – private keys and password files with 600 permissions, owned by the right user; never in Git.
  6. Logging – send auth.log/journal to central logging (see the Monitoring track); keep enough history to investigate.
  7. Time sync – correct time (timedatectl) makes logs trustworthy across servers.
  8. Benchmarks – compare against the CIS Benchmark for your distribution; tools like Lynis (sudo lynis audit system) give a quick score and to-do list.

Using keys in automation

Ansible and Jenkins connect with SSH keys too. Give automation its own key and account, store the private key in the tool’s credential store (see the Ansible guide), and limit what that account may do.

FAQ

Should I change the SSH port from 22? It reduces log noise from bots but isn’t real security. Keys, disabled passwords and firewall rules are what protect you.

What’s next

You’ve completed the Linux track. Next: Monitoring fundamentals.

LinuxSSHSecurityHardeningAdvanced

Gopinath Karthikeyan

Gopinath Karthikeyan – Windows Site Reliability Engineer with about 12 years in enterprise Windows Server infrastructure, and founder of WinOps Academy. YouTube · LinkedIn

More in Linux