Advanced
Searching inInstall guides
How to search
  • Paste an error message in quotes, e.g. "permission denied", to find the fix table that explains it.
  • Choose an area and a part of the guide (install, first settings, daily use, security, fixes) to go straight to the right step.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Guide 3OpenSSH server · Remote access

Install and harden the OpenSSH server

SSH is the door to every Linux server: admins log in with it, Ansible and Git use it, and attackers try it first. This guide installs the OpenSSH server on RHEL and Ubuntu, sets up key login, turns off passwords and root login in a safe order (without locking yourself out), and shows the daily uses: copy files, tunnels and jump hosts.

  • 11 Lessons
  • 21 Commands

$ run as a normal user# run as root (or with sudo)

3.1

Know it before you install

The server (sshd) waits on port 22 on the machine you log in to. The client (ssh, scp, sftp) runs on your computer. Both are in OpenSSH.

ItemRHEL / Rocky / Alma / FedoraUbuntu / Debian
Server packageopenssh-serveropenssh-server
Servicesshd.servicessh.service (Ubuntu 22.10+: started by ssh.socket)
Config/etc/ssh/sshd_config + /etc/ssh/sshd_config.d/*.confthe same
Logjournalctl -u sshd, /var/log/securejournalctl -u ssh, /var/log/auth.log
Port22/tcp22/tcp
Your keys~/.ssh/ (client), ~/.ssh/authorized_keys (server)the same

Remember

In sshd_config the first value of a setting wins. Files in sshd_config.d/ are read at the top (by the Include line), so put your settings in a file there, like 10-hardening.conf, and do not edit the big main file.

3.2

Install on RHEL, Rocky, Alma and Fedora

The server is usually already installed. Check, install if needed, start it and open the firewall:

dnf -y install openssh-server
systemctl enable --now sshd
firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload
ss -ltnp | grep :22
LISTEN 0  128  0.0.0.0:22  0.0.0.0:*  users:(("sshd",pid=812,fd=3))

3.3

Install on Ubuntu and Debian

Ubuntu Desktop and minimal images often do not have the server.

apt-get update
apt-get install -y openssh-server
systemctl enable --now ssh
ufw allow OpenSSH
ufw enable
systemctl status ssh --no-pager

Note

On Ubuntu 22.10 and newer, ssh.socket listens on port 22 and starts ssh.service at the first connection. That is normal.

3.4

Set up key login first

Do this before you turn off passwords. On your computer make a key and copy the public part to the server:

ssh-keygen -t ed25519 -C "laptop-2026"
ssh-copy-id admin@192.0.2.10
ssh admin@192.0.2.10

Make a short name for the server in your client config:

~/.ssh/config
Host web1
    HostName 192.0.2.10
    User admin
    IdentityFile ~/.ssh/id_ed25519
    ServerAliveInterval 60

Now ssh web1 is enough.

3.5

First configuration: harden the server

Make a group for the people who may log in, add your user, then write the settings file:

groupadd sshusers
usermod -aG sshusers admin
/etc/ssh/sshd_config.d/10-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AuthenticationMethods publickey
AllowGroups sshusers
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
ClientAliveInterval 300
ClientAliveCountMax 2
SettingWhy
PermitRootLogin noLog in as a normal user, then use sudo. Logs show who did what.
PasswordAuthentication no + KbdInteractiveAuthentication noNo password guessing: only keys work.
AllowGroups sshusersOnly members of this group may log in at all.
MaxAuthTries 3, LoginGraceTime 30Fewer tries per connection, less time for slow attacks.
ClientAliveInterval 300 + CountMax 2Dead sessions close after about 10 minutes.

Test the config, then reload — and keep your current session open:

sshd -t
systemctl reload sshd
sshd -T | grep -Ei 'permitrootlogin|passwordauthentication|allowgroups'
permitrootlogin no
passwordauthentication no
allowgroups sshusers

Watch out

Open a second terminal and log in again before you close the first one. If the new login fails, fix the config from the old session. On Ubuntu, reload ssh instead of sshd.

3.6

Change the port (optional)

Another port only reduces log noise; it is not real security. If you change it, tell SELinux and the firewall too:

semanage port -a -t ssh_port_t -p tcp 2222
firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload

Then add Port 2222 to your config file and restart sshd. On Ubuntu with ssh.socket: systemctl daemon-reload && systemctl restart ssh.socket, and ufw allow 2222/tcp.

3.7

Daily use: the commands and the real uses

ssh web1
ssh web1 'uptime; df -h /'
scp backup.tar.gz web1:/tmp/
rsync -avz --delete ./site/ web1:/var/www/site/
sftp web1
ssh -L 5432:127.0.0.1:5432 web1
ssh -J bastion admin@10.0.1.20
ssh-keygen -R 192.0.2.10

Real uses at work:

  • Admin login on every server; Ansible and Git work over SSH.
  • Tunnel (-L): reach a database that listens only on localhost of the server.
  • Jump host (-J): reach private servers through one hardened bastion.
  • ssh-agent keeps your unlocked key in memory, so you type the passphrase once.

3.8

Make it safe

Security checklist:

  • Keys only, with a passphrase; one key per device; remove old keys from authorized_keys.
  • No root login; AllowGroups; sudo for admin work.
  • Block password guessing: fail2ban (jail sshd) or the cloud firewall.
  • Allow port 22 only from your office, VPN or bastion IPs when you can.
  • Keep openssh-server updated.
  • Check the server settings with sshd -T after every change.

3.9

Update and remove

dnf -y upgrade openssh-server
apt-get install --only-upgrade -y openssh-server

Watch out

Do not remove or stop the SSH server on a remote machine — you lose your only way in. Use the cloud or VM console as a backup door.

Commands in this lesson

3.10

Fix common problems

ProblemCauseFix
Permission denied (publickey)Key not in authorized_keys, or bad file rights.chmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys; on RHEL also restorecon -Rv ~/.ssh.
Connection refusedThe service is stopped or listens on another port.systemctl status sshd, ss -ltnp.
Connection timed outFirewall or security group blocks the port.Open 22/tcp in firewalld/UFW and in the cloud security group.
REMOTE HOST IDENTIFICATION HAS CHANGEDThe server was rebuilt — or a man-in-the-middle.Check with the admin, then ssh-keygen -R HOST.
Service does not start after a changeSyntax error or SELinux port label.sshd -t; semanage port -l | grep ssh.
Login is very slowReverse DNS or GSSAPI lookups.UseDNS no on the server; GSSAPIAuthentication no in the client config.
User can not log in after hardeningNot in the sshusers group.usermod -aG sshusers USER.

3.11

Next steps

1Why test a new SSH login in a second terminal before you close the first one?

If the new settings lock you out, the old session is still open to fix them.

2Where do you put your own sshd settings, and why there?

In /etc/ssh/sshd_config.d/10-…conf. It is read first, and the first value of a setting wins.

All commands in this chapter