
To secure SSH on Linux, you make three changes: allow only SSH key logins, block root from logging in, and use a firewall that lets in only the ports you need. You also follow one habit that keeps you safe while you make them: test every change from a second session before you close the first.
This guide follows the steps in order, from your laptop's terminal to the server's config file and firewall. Each step explains what to type, why it matters, and how to confirm it worked. The last section covers the three most common ways people lock themselves out and how to recover.
- Copy your public key with ssh-copy-id and confirm key login works before changing anything else.
- Set PasswordAuthentication no and PermitRootLogin no in sshd_config, and check drop-in files for overrides.
- In UFW, allow OpenSSH first, set the default to deny incoming, then enable the firewall.
- Run sshd -t before reloading, and keep a backup session open the whole time.
- Prove it from a new terminal: key accepted, password denied, root refused.
- 0:00Intro
- 0:29Your SSH port is always being probed
- 1:19What you'll need first
- 2:08Copy your public key to the server
- 2:57Edit the sshd_config file
- 3:53Allow SSH, then deny the rest
- 4:59Reload sshd to apply changes
- 5:49Prove keys work and passwords don't
- 6:38Fix the three common lockouts
- 7:23Five steps to a locked-down server
- 7:50Keys, no root, minimal open ports
Understand why your SSH port is a target
Once a Linux server is reachable from the internet, its SSH port starts receiving automated login attempts, day and night. Nobody has to find your server by hand. Login bots run on compromised machines, sweep the internet for servers, connect to port 22 (the default SSH port), and send their guesses to sshd, the SSH server process.
These bots aim for two weak spots. The first is passwords. Bots never get tired, so they try common and leaked passwords again and again, and one weak password on one account can be enough. The second is the root account. Every Linux system has one, and a single successful root login hands over the whole machine. The steps below close both openings, plus every port you don't need.
- Bots target port 22 by default
- Weak or reused passwords fall to automated guessing
- Root is the most valuable account to break into
Prepare sudo access, a key pair and a backup session
The goal is a server that accepts only SSH keys, refuses root logins, and runs a firewall that admits just the ports you actually use. Everything else gets dropped. The work itself takes only a few minutes, but the preparation is what stops you from locking yourself out halfway through.
You need three things. Sudo access on the server, because every change here requires admin rights. A local SSH key pair on your laptop. And a second SSH session, already logged in before you change anything. Existing SSH sessions normally stay connected when you reload the SSH config or enable the firewall, so if a mistake blocks new logins, that open window is your way back in.
- Confirm you can run commands with sudo on the server
- Make sure an SSH key pair exists on your laptop
- Open and log in to a second session before editing anything
Copy your public key to the server
Install your key before turning passwords off, because right now the password is your only way in. From your laptop's terminal, run ssh-copy-id followed by your user and server address. It finds your key, asks for your password one last time, and reports how many keys were added.
Behind the scenes, ssh-copy-id appends your public key to the ~/.ssh/authorized_keys file in that user's home folder on the server. Your private key never leaves your laptop. Next, run ssh to the same address. You should land at the server prompt without being asked for a password.
If the test login still asks for a password, the key is not being used. Stop and fix that here. Never move on to the next step until key login works, because the next step removes passwords entirely.
- ssh-copy-id reports at least one key added
- A fresh ssh login reaches the prompt with no password prompt
- If you are still asked for a password, fix it before continuing
ssh-copy-id deploy@203.0.113.10
ssh deploy@203.0.113.10
Disable password and root login in sshd_config
With key login working, tell the SSH server to stop accepting passwords and root. On the server, open the main config file with sudo in your editor, for example sudo nano /etc/ssh/sshd_config. Find the PasswordAuthentication line, remove any # in front of it, and set it to no. Then find PermitRootLogin and set it to no as well. Each line is the keyword first, then the value.
Edit carefully. Many defaults ship commented out, and a line that still starts with # is ignored. Search the file for each keyword so you don't leave a second, conflicting copy elsewhere.
Also check the drop-in files. Many systems include extra config files from the sshd_config.d folder, and a cloud image may set PasswordAuthentication yes there. Your settings only count if nothing else overrides them.
- PasswordAuthentication no, with no leading #
- PermitRootLogin no, with no leading #
- No duplicate or conflicting lines in the main file
- No overriding values in sshd_config.d
# /etc/ssh/sshd_config
# Keys only: no password logins
PasswordAuthentication no
# Nobody logs in directly as root
PermitRootLogin no
Allow SSH in UFW, then deny everything else
Next, shrink what the outside world can reach at all. UFW, the Uncomplicated Firewall, does this with three commands, and the order matters. First, sudo ufw allow OpenSSH, which updates the rules for both IPv4 and IPv6. Second, sudo ufw default deny incoming, which sets the default policy to deny. Third, sudo ufw enable. It warns that the command may disrupt existing SSH connections; answer yes and the firewall becomes active.
If you enable the firewall before allowing SSH, the deny policy blocks new SSH connections and you could be shut out of your own server. Allowing SSH first prevents that.
Give a rule only to what you need. If you run a website, allow ports 80 and 443 the same way. Anything you don't explicitly allow stays closed. If SSH runs on a custom port, note that the OpenSSH profile covers only port 22, so allow your real port number instead.
- Allow SSH before running ufw enable
- Default incoming policy is set to deny
- Extra ports such as 80 and 443 are added only on purpose
- Custom SSH port is allowed by number
sudo ufw allow OpenSSH
sudo ufw default deny incoming
sudo ufw enable
Test the config and reload sshd
The firewall is up, but the SSH server is still running with its old settings until you reload it. Check first, then apply, never the other way round. Run sudo sshd -t to test the config. No output means no errors. If it finds a typo, it prints the file name and line number so you can fix it before the server ever reads it.
Then run sudo systemctl reload ssh to apply the new settings, and systemctl status ssh to confirm the service shows as active and running. The service name varies: Debian and Ubuntu call it ssh, while Fedora and Arch call it sshd. A reload keeps existing sessions open, including your backup.
- sudo sshd -t prints nothing
- Reload uses the right service name for your distribution
- Status shows active (running)
sudo sshd -t
sudo systemctl reload ssh
systemctl status ssh --no-pager
Verify that keys work and passwords don't
Now prove the setup from the outside. Keep your backup session open and run these tests from a brand-new terminal on your laptop. First, connect normally. Your key is offered, the server accepts it, and you're in. Next, force a password login with ssh -o PubkeyAuthentication=no. The server should reply with permission denied.
Then check the firewall with sudo ufw status. You want status active, OpenSSH allowed from anywhere, and only the other ports you chose on purpose. Finally, try logging in as root. It should be refused, even with a valid key. Once all of these checks pass, it's safe to close your backup session.
- Key login succeeds from a new terminal
- Password-only login returns permission denied
- ufw status shows active with only intended rules
- Root login is refused, even with a key
Fix the three most common SSH lockouts
Most failures come from firewall order, file permissions, or a config typo, and each has a quick fix while your backup session is still open. If you're locked out right after ufw enable, allow OpenSSH from your backup session, or use your hosting provider's web console.
If your key is ignored and you're asked for a password, check permissions: set the .ssh folder to 700 and authorized_keys to 600. If sshd won't reload, run sshd -t and fix the line it reports.
When the cause isn't obvious, read the logs. journalctl -u ssh lists each refused login along with the reason, such as wrong ownership on authorized_keys. The log usually names the exact problem.
- Firewall lockout: allow OpenSSH or use the provider console
- Key ignored: chmod 700 ~/.ssh and 600 authorized_keys
- Reload fails: run sshd -t and fix the reported line
- Unclear cause: check journalctl -u ssh
Key takeaways
- Install and test your SSH key before you disable passwords.
- PasswordAuthentication no makes guessed passwords useless.
- PermitRootLogin no removes the attackers' main prize.
- In UFW, allow SSH before enabling a deny-by-default policy.
- Always validate with sshd -t and test from a second session.
Frequently asked questions
Will disabling password authentication lock me out of my server?
Not if your SSH key already works. Confirm that a fresh ssh login reaches the prompt without a password before you change sshd_config, and keep a second session open until every test passes.
Why is PasswordAuthentication still enabled after I set it to no?
The line may still start with #, there may be a duplicate further down the file, or a file in the sshd_config.d folder may override it. Cloud images sometimes set PasswordAuthentication yes in a drop-in file.
What order should I run the UFW commands in?
Run sudo ufw allow OpenSSH first, then sudo ufw default deny incoming, then sudo ufw enable. Enabling the firewall before allowing SSH can block new SSH connections to your own server.
Should I reload ssh or sshd?
It depends on your distribution. Debian and Ubuntu name the service ssh, while Fedora and Arch name it sshd. Run sudo sshd -t first so you only reload a valid config.
I use a custom SSH port. Does ufw allow OpenSSH still work?
No. The OpenSSH profile covers only port 22, so allow your actual SSH port number instead, before you enable the firewall.
How do I find out why an SSH login was refused?
Run journalctl -u ssh on the server. It lists refused logins and the reason, such as wrong ownership or permissions on authorized_keys.