
It was supposed to be a five-minute job.
Every hardening guide says the same thing: move SSH off port 22 and the bots will mostly leave you alone. So one evening I opened /etc/ssh/sshd_config on a fresh Ubuntu 24.04 server, changed one line, and restarted SSH.
Port 2222 sudo systemctl restart ssh No errors. I opened a second terminal to try the new port.
ssh: connect to host 203.0.113.10 port 2222: Connection refused The line that did nothing
"Refused" is a very specific word. It means my request reached the server and the server said nothing is listening there. So I checked what SSH was actually listening on:
sudo ss -tlnp | grep ssh Port 22. Still. The file said 2222, the restart had worked, and SSH did not care.
It took me an embarrassingly long time to find out why. Since Ubuntu 22.10, SSH is started by a systemd socket, ssh.socket, and the socket is what owns the port. The Port line in sshd_config is only picked up when systemd regenerates that socket, and restarting the ssh service doesn't do that. This does:
sudo systemctl daemon-reload sudo systemctl restart ssh.socket Now ss showed :2222. Progress.
The door I forgot to open
I tried again, and this time nothing happened at all. No "refused". The cursor just sat there until:
ssh: connect to host 203.0.113.10 port 2222: Connection timed out A timeout is a different animal. Refused means someone answered "no". Timed out means nobody answered, usually because something in between threw the packets away. In my case that something was UFW. Weeks earlier I had run ufw allow OpenSSH, which opens port 22. Nobody had ever told it about 2222.
And here's the part I'm least proud of. To "test cleanly", I had already closed my original session. SSH was no longer on 22, the firewall was blocking 2222, and I had no way in.
Getting back in through the side door
What saved me was the browser console in my VPS control panel. It's like plugging a screen and keyboard into the server: no SSH, no firewall in the way. I logged in as root with the password I had almost forgotten I had, and ran the one command I should have run first:
sudo ufw allow 2222/tcp Back in, from my own terminal, in about thirty seconds. The whole lockout lasted maybe twenty minutes. It felt much longer.
Two days later: Permission denied (publickey)
Feeling confident again, I did the next thing on the hardening list. I created a normal user, copied root's authorized_keys into the new user's home folder with sudo cp, and switched off root login. Then:
me@203.0.113.10: Permission denied (publickey). The key was right. I checked it three times. The problem was the word sudo. The copied file still belonged to root and kept its private permissions, and SSH reads authorized_keys as the user who is logging in. My new user wasn't allowed to read its own key file. SSH doesn't explain that, it just quietly ignores the key.
sudo chown -R me:me /home/me/.ssh sudo chmod 700 /home/me/.ssh sudo chmod 600 /home/me/.ssh/authorized_keys Logged straight in. This time, luckily, I had kept my root session open in another tab.
What I do now, every single time
I keep a second session open whenever I touch SSH or the firewall. If the change breaks something, that session is still my way in.
I open the new port in the firewall before I move SSH to it, never after.
I run sudo sshd -t after every edit. No output means the config is valid. One typo in that file can take SSH down entirely.
And I read the error message properly, because the three common ones mean very different things:
- Connection refused: the request arrived, but nothing is listening on that port.
- Connection timed out: nothing answered, usually a firewall dropping the packets, or the wrong IP.
- Permission denied (publickey): you got through the door, but the lock didn't accept your key.
When I'm stuck on a new variation, I go through a longer checklist for each of these SSH errors instead of guessing.
Moving SSH to 2222 did cut down the noise in my auth log. It also cost me an evening and a little pride. The port change was never the real problem, though. Changing things on a live server while having only one way in was.
![[ANN] FashionCraft 0.2.1 – 3D garment design app (egui + wgpu, GPU cloth simulation)](https://us1.discourse-cdn.com/flex019/uploads/rust_lang/optimized/3X/5/d/5d2ed8c963a8c2c0956b163516390e7fe13ca821_2_690x417.webp)
