SSH

Securing SSH Access to Your Servers

By ShellLayer Editorial Team · · 5 min read

Secure Shell, better known as SSH, is the standard way to administer Linux and Unix servers remotely. It encrypts the connection and verifies who you are talking to. Because it provides command-line control of a machine, it is also one of the first things attackers probe. Within minutes of a server going online, its logs usually fill with login attempts from automated scanners.

Fortunately, SSH is highly configurable, and a handful of changes make it very hard to attack. This article walks through them in a practical order.

Start with key-based authentication

Passwords can be guessed. A properly generated SSH key cannot, for all practical purposes. A key pair has a private half that stays on your computer and a public half that you place on the server. The server challenges your client to prove it holds the private key, without the private key ever being sent.

Generate a modern key on your own machine:

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

Protect the private key with a passphrase, so a stolen laptop does not hand over server access. Then copy the public key to the server:

ssh-copy-id user@server

Test that logging in with the key works before changing anything else.

Disable password logins

Once keys work, turn off password authentication. This removes the main target of brute-force attacks. Edit /etc/ssh/sshd_config:

PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes

Reload the service with sudo systemctl reload sshd (the unit may be named ssh on some distributions). Keep your existing session open and test a new connection in a second window. If something is wrong, you can still fix it from the first one.

Do not allow root to log in directly

The root account exists on every Linux system, so attackers always try it. Disable direct login and use a normal account with sudo instead.

PermitRootLogin no

This also gives you better records, since actions are tied to a named user rather than a shared administrator identity.

Limit who can connect

Decide which accounts should be able to use SSH at all, and say so in the configuration.

AllowUsers deploy alex

You can also use AllowGroups to manage this through group membership. Accounts used only by services, such as web server users, should never be able to log in over SSH.

Reduce the exposure window

  • Lower the number of attempts per connection with MaxAuthTries 3.
  • Shorten the login grace period with LoginGraceTime 30.
  • Drop idle sessions using ClientAliveInterval and ClientAliveCountMax.
  • Disable features you do not use, such as X11Forwarding no and, where not needed, agent or TCP forwarding.

Each setting trims away behavior that an attacker might abuse.

Use the network to narrow the door

The best protection for a service is not letting strangers reach it.

  • Firewall rules. Allow SSH only from known addresses, such as your office or a VPN, if your situation permits.
  • A bastion host. Place one hardened machine at the edge as the only server reachable by SSH, and make internal servers accept connections only from it. OpenSSH supports this pattern through the ProxyJump option.
  • A VPN. Require people to join a private network before SSH is even visible.

Changing the listening port reduces noise in logs, but treat it as cosmetic. A port scan finds the new number quickly.

Block repeated failures

A tool such as fail2ban reads authentication logs and temporarily blocks addresses that fail repeatedly. Even with password logins disabled, this keeps logs readable and trims wasted resources. Rate limiting in the firewall can achieve a similar effect.

Protect your keys

Security depends on the private keys staying private.

  1. Use a unique key per device, so you can revoke one without affecting others.
  2. Always set a passphrase, and use an SSH agent so you type it only once per session.
  3. Restrict file permissions: the private key file should be readable only by you (chmod 600).
  4. Remove public keys from authorized_keys when people leave or devices are retired.
  5. Be careful with agent forwarding, which lets a compromised intermediate server use your keys while you are connected.

For higher assurance, hardware-backed keys keep the private key inside a physical device. OpenSSH supports FIDO2 security keys directly.

SSH certificates for larger setups

When many servers and users are involved, copying public keys around becomes unmanageable. OpenSSH supports certificates, where a trusted certificate authority signs user keys with an expiry date. Servers trust the authority rather than individual keys. Access can then expire automatically, and revoking someone requires no editing of files on every machine.

Keep it updated and monitored

  • Install security updates for OpenSSH and the operating system promptly.
  • Review /var/log/auth.log or the equivalent system journal for failed or unusual logins.
  • Set alerts for logins from unexpected locations or at odd hours.
  • Audit authorized_keys files periodically for keys nobody recognizes.

You can check your effective configuration at any time with sudo sshd -T, which prints the settings the server is actually using.

A compact checklist

  • Keys with passphrases, passwords off.
  • No direct root login.
  • AllowUsers or AllowGroups set.
  • Firewall or VPN restricting access.
  • Failure blocking in place.
  • Logs reviewed, keys audited.

Frequently Asked Questions

Is it enough to change the SSH port?

No. A different port hides the service from casual bots and cuts down log clutter, but any scanner will find it. Real protection comes from key authentication, disabled passwords and restricted access.

What should I do if I lose my private key?

Remove the matching public key from authorized_keys on every server it was installed on, then generate a new pair. If you used a passphrase and the key file was not copied by anyone, the risk is lower, but removal is still the safest response. Keep a second key registered so you are not locked out.

Should I use RSA or Ed25519 keys?

Ed25519 is a good default today. It is fast, produces short keys and is supported by current OpenSSH versions. If you need compatibility with older systems, RSA with a key length of at least 3072 bits is still acceptable.

Related reading