1. Open the new port in the firewall
sudo ufw allow 2222/tcp
Only needed when ufw is on (sudo ufw status).
2. Change the port
sudo sed -i 's/^#\?Port .*/Port 2222/' /etc/ssh/sshd_config
grep -n '^Port' /etc/ssh/sshd_config
sudo sshd -t
sudo systemctl restart ssh
The grep line must show "Port 2222". "sshd -t" checks the file and must print nothing. On Ubuntu 24.04 and newer also run: sudo systemctl daemon-reload && sudo systemctl restart ssh.socket
3. Try it — in a NEW window, keep the old one open
ssh -p 2222 root@YOUR.SERVER.IP
Close the old window only when the new one is in. If it does not work, the old window is your way back — or the browser console on the server page.
4. Then close the old port
sudo ufw delete allow 22/tcp
Where a command has YOUR.SERVER.IP, put the address of your server — it is on the server page in the console. Ask Joy Ops AI the same question in the console and it fills your own address in.
Stuck? Ask Joy Ops AI in your console, or open a ticket — a person answers.
Corrections and suggestions go straight to the team that wrote it — suggest an edit or see more in blog.