Skip to content
Back to Series SSH from Scratch
Sysadmin

Setting Up Your First OpenSSH Server the Right Way

A practical guide to installing and configuring an OpenSSH server on Linux, covering sshd_config, ports, and a safe first connection test.

A
Alif Afriza Author
· · 4 min read
Setting Up Your First OpenSSH Server the Right Way

Now that you understand what SSH is, it is time to set up your first OpenSSH server on a Linux machine. This article follows standard practice: installation, a minimal secure configuration, and verification that the server actually works.

Installing the OpenSSH Server

On Debian and Ubuntu, the SSH server package is called openssh-server. Install it with:

sudo apt update
sudo apt install openssh-server

After installation, the sshd service starts automatically. Check its status:

systemctl status ssh

The firewall needs to be opened for the SSH port (standard port 22):

sudo ufw allow ssh
sudo ufw enable

The Main Configuration File

The SSH server configuration lives in /etc/ssh/sshd_config. Most distributions also have an /etc/ssh/sshd_config.d/ directory for additional per-file configuration, which is tidier to manage.

The recommended minimum configuration for a personal machine or lab:

Port 22
PermitRootLogin no
PasswordAuthentication yes
PubkeyAuthentication yes
  • PermitRootLogin no: forbids logging in directly as root over SSH. Log in as a regular user first, then use sudo to elevate privileges.
  • PasswordAuthentication yes: allows password login, which is convenient the first time. Once a public key is in place, it should be turned off (covered in the hardening article).
  • PubkeyAuthentication yes: allows public key authentication, the best security practice.

Any change to sshd_config takes effect after a reload:

sudo systemctl reload ssh

Important: the reload command does not drop running sessions; the configuration only applies to new connections.

Testing the Configuration Before Reloading

OpenSSH provides a configuration checking tool:

sudo sshd -t

If there is no output and the exit code is 0, the configuration is valid. Error output will point to the problematic line. Get into the habit of running sshd -t before reloading, so that a typo does not take the SSH server down entirely and leave the server without remote access.

Verifying the First Connection

From another machine, try to log in:

ssh user@server-address

The first time, SSH shows the server fingerprint and asks for confirmation. Answer yes, type your password, and you are in the server shell.

To make sure the configuration was picked up, run on the server:

systemctl status ssh
ss -tlnp | grep ssh

The ss output shows sshd listening on the configured port.

Handling Common Problems

A few cases that often confuse beginners:

  1. “Connection refused” means the port is not open yet or sshd is not running. Check systemctl status ssh and the firewall.
  2. Connection timeouts are usually caused by a firewall blocking traffic along the way.
  3. Permission denied when using a password: make sure PasswordAuthentication yes is set on the server side (if you use passwords), or the key is not registered yet (for public keys).
  4. Config not being read: check the order of the sshd_config.d/ includes and make sure there are no conflicting duplicate directives.

For clearer diagnosis, run the client in verbose mode:

ssh -v user@server-address

Its output shows at which stage the connection stops: DNS, TCP, key exchange, or authentication.

Securing It from the First Step

Before exposing the server to the internet, get into the habit of:

  • Using a non-standard SSH port on public networks (to dampen bots scanning port 22).
  • Installing a public key right away and turning off password login.
  • Never allowing PermitRootLogin with a password.

Keys and hardening will be covered in the next article, but the principles must be right from the start.

Next in the curriculum: next, set up public key authentication with ssh-keygen to replace passwords.

References

  • sshd_config(5) man page, OpenSSH (the official reference for all directives).
  • OpenSSH documentation, openssh.com/manual.html (server and client guides).
  • Debian Wiki, SSH, wiki.debian.org/SSH (Debian installation and configuration practices).
  • Ubuntu Server Guide, OpenSSH Server, ubuntu.com/server/docs/openssh-server.
  • NIST SP 800-52 Rev 2, Guidelines for the Selection, Configuration, and Use of Transport Layer Security (TLS) Implementations (a reference for related cryptographic practices).
Open for Contributors

Your ideas deserve to be read by thousands of people.

ByteNesia is built for everyone. If you have experience, tutorials, or opinions about technology, write and share them here. Your name will be credited as a contributor.

Write anything: tool reviews, homelab tutorials, security tips, or your tech stories.

Discussion & Comments