Ubuntu Tools – Change SSH Port
This guide explains how to change the default SSH port in Ubuntu safely.
Steps
- Keep your current SSH session open (do not close it yet).
- Edit the SSH configuration file:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sudo nano /etc/ssh/sshd_config
Find:
#Port 22
Change it to (example):
Port 2222
- Allow the new port in the firewall before restarting SSH:
sudo ufw allow 2222/tcp
- Test the new connection from another terminal:
ssh -p 2222 user@server_ip
- If successful, restart SSH and close the old port:
sudo systemctl restart ssh
sudo ufw delete allow 22/tcp
Best Practice: Always test the new port before closing your active session.