How to Set Up a Secure SSH Server: A Beginner's Guide

Updated on
10 min read

Secure Shell (SSH) is the standard way to administer Linux servers over an untrusted network. A secure SSH server is more than a daemon with a non-default port: it combines verified server identity, carefully scoped user authentication, network restrictions, monitoring, and a tested recovery path. This guide walks through those controls for a Linux host, whether it is a VPS, a production machine, or a home lab.

What Is a Secure SSH Server?

An SSH server runs the OpenSSH daemon (sshd) and accepts encrypted connections from SSH clients. It lets authorized users open a remote shell and, when permitted, tunnel traffic or transfer files. A secure setup makes the server’s identity verifiable, allows only approved users and authentication methods, limits network exposure, and records activity for investigation.

The examples below target common systemd-based Linux distributions. Package names, service names, and vendor configuration files vary; check the documentation for your distribution before applying changes.

Why SSH Hardening Matters

SSH is often an exposed administrative control plane: a successful login can grant access to powerful commands, application secrets, and other systems. Attackers may probe internet-facing hosts for weak or reused passwords, stolen private keys, vulnerable software, or permissive accounts. Misconfiguration can also create an availability problem by locking legitimate administrators out.

Hardening reduces the number of reachable accounts and authentication paths, keeps the daemon patched, and makes recovery deliberate. It does not make a compromised administrator device safe, replace operating-system updates, or protect a server after an attacker has already gained privileged access.

How SSH Works: Connection and Authentication

An SSH session begins when the client connects to the server’s listening address and port. The peers negotiate protocol parameters and establish an encrypted, integrity-protected transport. The client checks the server’s host key against its known-hosts database; an unexpected change can indicate a rebuilt server, a changed host key, or a man-in-the-middle attempt and should be verified out of band.

After transport setup, the user-authentication protocol proves that the requested account is allowed to log in. With public-key authentication, the client uses its private key to sign a challenge and the server checks the corresponding public key against the account’s authorized credentials. The private key is not sent to the server. Once authenticated, SSH can carry a shell, a file-transfer session, or explicitly permitted forwarding channels.

The distinction between server host keys and user keys matters: host keys identify the server to clients, while user keys authorize people or automation to the server. Replacing one does not rotate the other. The protocol roles are specified in the SSH transport RFC and the SSH user-authentication RFC.

Components and Authentication Options

  • SSH client: Connects to the server and stores trusted host identities in a known-hosts file.
  • OpenSSH daemon (sshd): Listens for connections and applies system-wide and account-specific policy from sshd_config and any included files.
  • Host keys: Server-held keys used to prove the server’s identity. Protect them and investigate unexpected host-key changes rather than bypassing warnings.
  • User credentials: Public keys, passwords, keyboard-interactive methods, security keys, or SSH certificates used to authorize a login.
  • Access controls and logs: Firewall rules, account/group policy, PAM integration where configured, and system authentication logs limit and record access.
Method How it works Strengths and trade-offs
Password The user enters a password inside the encrypted SSH session. Familiar, but exposed to guessing, reuse, and phishing; disable for internet-facing administration when key-only access is practical.
Public key The client proves possession of a private key matching an authorized public key. Strong and convenient when private keys are passphrase-protected, access is reviewed, and lost keys are revoked.
Hardware-backed security key A compatible FIDO security key performs a user-presence or user-verification operation. Can keep signing material on the device and improve resistance to key theft; test client, server, and recovery support first.
SSH user certificate A trusted SSH certificate authority signs short-lived user identities. Simplifies large fleets and expiry, but requires protected CA keys, issuance policy, and a reliable renewal path.

An SSH key is not automatically a second factor: a key file stored on a device can be stolen. For stronger assurance, combine public-key login with a hardware authenticator or an appropriately configured multi-factor PAM policy. Review the OpenBSD sshd_config manual and the OpenSSH manuals for the options supported by the installed version.

Real-World Use Cases

  • VPS and cloud administration: Allow management access only from a VPN, bastion, or trusted network where possible, and restrict login to named administrative accounts.
  • Home labs and small fleets: Use individual keys and a documented emergency console path rather than sharing one administrator credential.
  • Deployment automation: Give automation a dedicated account and key with only the required commands or forwarding permissions; do not reuse a human administrator’s private key.
  • Larger Linux estates: Consider SSH certificates or centrally managed identity, together with local emergency access and a plan for directory-service outages.

For hands-on practice before changing a remote host, see the home lab hardware guide.

Practical Considerations: Install, Configure, and Validate

Prepare access and install OpenSSH

Before changing a remote server, confirm that you can use its provider console or another out-of-band recovery method. Back up the SSH configuration files you plan to edit and record how to restore them. Keep an existing administrative session open while testing a new one. Confirm the administrator account has working sudo access and note the current firewall rule and SSH service name.

Install the server package and enable its service:

# Debian or Ubuntu
sudo apt update
sudo apt install openssh-server
sudo systemctl enable --now ssh

# Fedora or RHEL-family systems
sudo dnf install openssh-server
sudo systemctl enable --now sshd

On Debian and Ubuntu the systemd service is commonly ssh; on Fedora and RHEL-family distributions it is commonly sshd. Confirm the actual unit and status on the host:

sudo systemctl status ssh
# Or, where the unit is named sshd:
sudo systemctl status sshd

Create and enroll an administrator key

Generate a key on the client, not on the server. ED25519 is a sensible default on current OpenSSH clients; use another supported key type only when compatibility requires it. Choose a strong passphrase and keep the private key out of repositories, shared folders, and server home directories.

ssh-keygen -t ed25519 -a 64 -C "admin-laptop"
ssh-copy-id [email protected]

If ssh-copy-id is unavailable, add the public key (.pub) to the target account’s authorized_keys file using a trusted channel. The private key stays on the client. On the server, check that the account owns the files and that permissions are restrictive:

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

Open a second terminal and verify that the new key can log in and run sudo before changing authentication policy. Record which user and key were tested.

Rotate or revoke user keys

For planned rotation, add a replacement key before removing the old one. Enroll and test the replacement from a new client session, then remove only the old public-key entry from the account’s authorized keys. This overlap prevents an avoidable outage:

ssh-keygen -t ed25519 -a 64 -f ~/.ssh/id_ed25519_server -C "admin-laptop-server"
ssh-copy-id -i ~/.ssh/id_ed25519_server.pub [email protected]
ssh -i ~/.ssh/id_ed25519_server [email protected]

If a private key is lost or suspected stolen, remove its public key promptly, review authentication logs for unexpected use, and rotate any other credentials the key could access. Keep a record of key owner, purpose, enrollment, and revocation so stale access can be found during regular reviews.

Apply a cautious key-only policy

OpenSSH may read additional files through Include directives, and distribution defaults can differ. Inspect the active configuration before editing; many Linux distributions support drop-in files in the SSH daemon’s configuration directory. Put each setting in the location and order recommended by the distribution, because the first obtained value can take precedence. A basic key-only policy for a host that does not rely on keyboard-interactive MFA can look like this:

PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitEmptyPasswords no
MaxAuthTries 4
LoginGraceTime 30

Do not disable KbdInteractiveAuthentication blindly if the host uses it for MFA or another PAM-backed login flow. In that case, configure and test the intended authentication sequence with the identity/security team. You can also restrict access with AllowGroups or AllowUsers, but first verify every required administrator is included. Avoid copying custom cipher or key-exchange lists from old hardening recipes; current OpenSSH defaults are maintained for supported algorithms.

Before applying any change, validate syntax and inspect effective values:

sudo sshd -t
sudo sshd -T | grep -E 'permitrootlogin|pubkeyauthentication|passwordauthentication|kbdinteractiveauthentication|maxauthtries'

For configuration that uses Match blocks, evaluate the effective settings for a real connection context with sshd -T -C user=adminuser,host=server.example.com,addr=198.51.100.10. If validation succeeds, reload the correct service (sudo systemctl reload ssh or sudo systemctl reload sshd) and immediately test a fresh login in the second terminal. Keep the original session open until the new login and required privileges work.

Restrict network exposure and monitor access

Allow SSH through the host firewall before enabling a default-deny policy, and make sure the rule matches the port and source addresses actually in use. For example:

# UFW: allow the OpenSSH application profile before enabling UFW
sudo ufw allow OpenSSH
sudo ufw status verbose

# firewalld: enable the SSH service in the active zone
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload

Where practical, limit management traffic to a VPN, bastion, or trusted source range. Changing port 22 can reduce background scan noise but does not strengthen authentication or replace firewall rules. Tools such as Fail2ban may help with repeated attempts, but they are an additional control, not a substitute for sound authentication, patching, or access restrictions.

Review the system’s authentication logs after rollout and route them to centralized storage when the host’s risk warrants it. Common sources are the system journal (sudo journalctl -u ssh or sudo journalctl -u sshd) and distribution-specific authentication logs, such as auth.log on Debian/Ubuntu or secure on some RHEL-family systems. Investigate unexpected successful logins, repeated failures, new authorized keys, and changes to SSH configuration.

For fleet-wide policies, manage configuration and authorized keys through reviewed automation rather than ad hoc shell edits. The Ansible configuration management guide and LDAP integration guide cover related fleet and identity controls.

Common Misconceptions

  • Changing the port secures SSH. A different port can reduce routine log noise, but scanners can find it. Access restrictions and strong authentication matter more.
  • A public key means there is MFA. A copied or stolen private key may be used by whoever obtains it. Protect it with a passphrase or hardware-backed storage and revoke it when compromised.
  • Disabling passwords is always safe. It can lock out administrators or break an intentional MFA flow if keys and recovery paths have not been tested first.
  • A firewall or banning tool makes the server secure. These controls limit exposure or repeated attempts, but do not fix vulnerable software, stolen credentials, permissive accounts, or poor monitoring.
  • The server’s host key and a user’s login key are interchangeable. Host keys identify the server to clients; user credentials authorize a person or service to the server.
  • An unfamiliar host-key warning should be ignored. Verify the new fingerprint through a trusted console or administrator before replacing the stored identity.
TBO Editorial

About the Author

TBO Editorial writes about the latest updates about products and services related to Technology, Business, Finance & Lifestyle. Do get in touch if you want to share any useful article with our community.