$ run as a normal user# run as root (or with sudo)
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.
| Item | RHEL / Rocky / Alma / Fedora | Ubuntu / Debian |
|---|---|---|
| Server package | openssh-server | openssh-server |
| Service | sshd.service | ssh.service (Ubuntu 22.10+: started by ssh.socket) |
| Config | /etc/ssh/sshd_config + /etc/ssh/sshd_config.d/*.conf | the same |
| Log | journalctl -u sshd, /var/log/secure | journalctl -u ssh, /var/log/auth.log |
| Port | 22/tcp | 22/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.
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))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-pagerNote
On Ubuntu 22.10 and newer, ssh.socket listens on port 22 and starts ssh.service at the first connection. That is normal.
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.10Make a short name for the server in your client config:
Host web1
HostName 192.0.2.10
User admin
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 60Now ssh web1 is enough.
First configuration: harden the server
Make a group for the people who may log in, add your user, then write the settings file:
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AuthenticationMethods publickey
AllowGroups sshusers
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
ClientAliveInterval 300
ClientAliveCountMax 2| Setting | Why |
|---|---|
PermitRootLogin no | Log in as a normal user, then use sudo. Logs show who did what. |
PasswordAuthentication no + KbdInteractiveAuthentication no | No password guessing: only keys work. |
AllowGroups sshusers | Only members of this group may log in at all. |
MaxAuthTries 3, LoginGraceTime 30 | Fewer tries per connection, less time for slow attacks. |
ClientAliveInterval 300 + CountMax 2 | Dead 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 sshusersWatch 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.
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 --reloadThen 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.
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.10Real 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-agentkeeps your unlocked key in memory, so you type the passphrase once.
Make it safe
Security checklist:
- Keys only, with a passphrase; one key per device; remove old keys from
authorized_keys. - No root login;
AllowGroups;sudofor admin work. - Block password guessing:
fail2ban(jailsshd) or the cloud firewall. - Allow port 22 only from your office, VPN or bastion IPs when you can.
- Keep
openssh-serverupdated. - Check the server settings with
sshd -Tafter every change.
Update and remove
dnf -y upgrade openssh-server
apt-get install --only-upgrade -y openssh-serverWatch 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.
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
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 refused | The service is stopped or listens on another port. | systemctl status sshd, ss -ltnp. |
Connection timed out | Firewall or security group blocks the port. | Open 22/tcp in firewalld/UFW and in the cloud security group. |
REMOTE HOST IDENTIFICATION HAS CHANGED | The server was rebuilt — or a man-in-the-middle. | Check with the admin, then ssh-keygen -R HOST. |
| Service does not start after a change | Syntax error or SELinux port label. | sshd -t; semanage port -l | grep ssh. |
| Login is very slow | Reverse DNS or GSSAPI lookups. | UseDNS no on the server; GSSAPIAuthentication no in the client config. |
| User can not log in after hardening | Not in the sshusers group. | usermod -aG sshusers USER. |
Next steps
- Quick how-tos: Install and enable the SSH server, Log in with SSH keys, Copy files between servers, Harden the SSH server, Use a jump host, SSH config for many servers, SSH tunnel to a database.
- Command pages: ssh, sshd, ssh-keygen, ssh-copy-id, scp, fail2ban-client.
- Learn more: RH124 — Configure and secure SSH, RHCSA — Connect to servers.
- SSH in daily DevOps work: ssh — log in and run commands.
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.