Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 37 ยท Secure Protocols

SSH Hardening and Secure Remote Access

Secure SSH properly: key-based authentication, a hardened sshd_config, host key verification, bastion hosts and jump proxies, and an auditing script that checks a config against best practice.

SSH (Secure Shell) is the standard way to administer servers and network devices. It encrypts the session, authenticates both sides and can tunnel other traffic. It is also one of the most attacked services on the internet: any server with port 22 open to the world sees automated password-guessing within minutes. A badly configured SSH daemon is therefore among the most valuable things an attacker can find; a well-configured one is very hard to attack.

In this lesson you will learn how SSH authenticates, what to put in sshd_config, how bastion hosts shrink exposure, and how to audit a configuration automatically. Config files and shell sessions are read-only examples; the Python code audits configuration text you provide and touches no real server.

Changing sshd_config on a remote machine can lock you out. Always keep a second session open, test the file with sshd -t, and reload rather than restart until you have confirmed you can log in again.

How SSH builds trust

  1. Server authentication. On first contact the client sees the server's host key fingerprint and stores it in ~/.ssh/known_hosts. Later connections compare against the stored key; a mismatch triggers a loud warning because it may indicate a man-in-the-middle. Verify the fingerprint out of band on the first connection, or distribute known keys (or SSH certificates) in advance. Never answer "yes" to a changed-host-key warning without understanding why.
  2. Key exchange. A fresh session key is agreed (with forward secrecy), then all traffic is encrypted and integrity protected.
  3. User authentication. The client proves who the user is: public-key authentication (best), password, or keyboard-interactive (MFA prompts). With keys, the private key never leaves the client; the server holds only the public key in ~/.ssh/authorized_keys.
sequenceDiagram
    participant C as Client
    participant S as Server
    C->>S: Connect, offer algorithms
    S->>C: Host key + signature
    Note over C: Compare with known_hosts
    C->>S: Key exchange (forward secrecy)
    S->>C: Challenge for the user's public key
    C->>S: Signature made with the private key
    S->>C: Authenticated, session starts

Use keys, protect them

Generate a modern key and protect it with a passphrase (shown read-only):

TEXT
$ ssh-keygen -t ed25519 -C "alice@laptop" -f ~/.ssh/id_ed25519
$ ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]
  • Ed25519 keys are small, fast and strong; RSA needs 3072 bits or more.
  • A passphrase encrypts the private key on disk, so a stolen laptop does not mean a stolen identity. Use ssh-agent so you type it once per session.
  • For higher assurance use hardware-backed keys (FIDO2 ed25519-sk), which cannot be copied off the device.
  • For large fleets use SSH certificates: a small internal CA signs user keys with short lifetimes, so you manage trust in one place instead of thousands of authorized_keys files.
  • Remove keys of people who leave; stale keys are a classic backdoor.

A hardened sshd_config

The server configuration is /etc/ssh/sshd_config (read-only example):

TEXT
Port 22
Protocol 2
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AuthenticationMethods publickey
MaxAuthTries 3
LoginGraceTime 30
AllowUsers alice bob
X11Forwarding no
AllowAgentForwarding no
AllowTcpForwarding no
ClientAliveInterval 300
ClientAliveCountMax 2
LogLevel VERBOSE

What each choice buys you:

  • PermitRootLogin no forces attackers to guess a username and makes actions attributable to a real person; use sudo after login.
  • PasswordAuthentication no removes password guessing entirely. This is the single most effective setting.
  • AuthenticationMethods publickey can be extended to publickey,keyboard-interactive for key plus a one-time code.
  • MaxAuthTries and LoginGraceTime limit how long an unauthenticated connection may linger.
  • AllowUsers / AllowGroups is an allow-list of who may log in at all.
  • Disable forwarding features you do not need: they can be abused to pivot through a server.
  • LogLevel VERBOSE records key fingerprints for logins, valuable for investigations.

Changing the port (for example to 2222) reduces log noise from bots but is not security; a scanner finds it quickly. Do it only as a convenience on top of the real controls.

Reduce exposure

  • Firewall port 22 to known source ranges or to the VPN only.
  • Use fail2ban or similar to block addresses with repeated failures; keep in mind it only reacts after attempts and key-only authentication already makes guesses useless.
  • Prefer a bastion host (jump server): the only machine reachable from outside. It is hardened, heavily logged, and everything else accepts SSH only from it.
  • Port knocking or single packet authorization hides the port until a secret sequence arrives; helpful but adds complexity.
  • Zero trust alternatives replace long-lived keys with identity-aware, short-lived access (zero trust principles).
flowchart LR
    U["Admin laptop"] -- "SSH + key (ProxyJump)" --> B["Bastion host<br/>(public, hardened, logged)"]
    B --> S1["App server"]
    B --> S2["Database server"]
    I["Internet scanners"] -. "blocked by firewall" .-> S1

Using the bastion from the client is one line of configuration (read-only):

TEXT
Host db.internal
    HostName 10.0.5.20
    User alice
    ProxyJump bastion.example.org

Prefer ProxyJump over agent forwarding: forwarding exposes your agent socket on the intermediate host, where a root user there could ask it to sign things as you.

Auditing a configuration with code

Checking by eye does not scale. The program below audits configuration text against a small baseline and prints findings with a severity. It reads a string, so it is safe to run anywhere.

Python Audit an sshd_config against a baseline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In a real program you would read the file (or better, query the effective configuration with sshd -T, which resolves defaults and Match blocks) and report in your monitoring pipeline. Existing open-source scanners such as Lynis and CIS benchmark tools do this at scale.

Tunnelling features and their risks

SSH can forward ports: -L (local, bring a remote service to your machine), -R (remote, expose your machine's service through the server) and -D (a SOCKS proxy). They are handy for administration and for reaching private databases without opening firewall holes, but they can also smuggle traffic past controls. Restrict them with AllowTcpForwarding, PermitOpen and per-key options such as restrict,command="..." in authorized_keys, which pins an automation key to one command.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which sshd_config change most effectively stops automated password guessing?

    With password login disabled there is nothing to guess; the attacker would need a private key.
  2. 2. You connect and SSH warns that the host key has changed. What is the right reaction?

    A changed host key can mean a reinstall or a man-in-the-middle; verify out of band first.
  3. 3. What is the main purpose of a bastion host?

    A bastion centralizes and monitors administrative access so other servers need not be exposed.
  4. 4. Why prefer ProxyJump to agent forwarding?

    A root user on a host where your agent is forwarded can use the socket to authenticate as you while you are connected.

Summary

  • SSH authenticates the server (host keys and known_hosts), encrypts the session and authenticates the user.
  • Use Ed25519 or hardware keys with passphrases; consider SSH certificates for fleets; remove stale keys.
  • Harden sshd: no root login, no passwords, AllowUsers, low MaxAuthTries, limited forwarding, verbose logging.
  • Reduce exposure with firewalls, bastions and ProxyJump; changing the port is only noise reduction.
  • Audit configuration automatically and test changes with a second session open.

Next: Defense in Depth Architecture