The Default Target Vector
When you connect to an Ubuntu server remotely, you use the Secure Shell (SSH) protocol. By universal standard, the SSH daemon constantly listens for incoming connections on Port 22. Because this is public knowledge, Port 22 is the primary target for automated botnets and brute-force hacking scripts scanning the internet. If your server is exposed to the web, leaving SSH on Port 22 guarantees that thousands of malicious bots will attempt to guess your password every single day, flooding your logs and wasting your bandwidth.
How to Change the SSH Port
You must plunge into the core SSH configuration file to manually redefine the listening port to a random, non-standard number.
1. Open your terminal application or connect to your server via SSH.
2. You must use a text editor with root privileges to modify the daemon’s configuration file. Execute the following command:
sudo nano /etc/ssh/sshd_config
3. The text editor will open. Scroll down the file using your arrow keys until you locate the line that says #Port 22.
4. The hash (#) means the line is commented out and using the default. Delete the hash.
5. Change the number 22 to a random, high-numbered port between 1024 and 65535 (for example, change it to Port 49221).
6. Press Ctrl + O to save, Enter to confirm, and Ctrl + X to exit the text editor.
7. Crucial Warning: Before you restart the SSH service, you must explicitly tell your server’s firewall (UFW) to allow traffic on your new port, otherwise you will lock yourself out forever:
sudo ufw allow 49221/tcp
8. Finally, restart the SSH daemon to apply the change:
sudo systemctl restart sshd
The next time you attempt to connect, you must explicitly append the new port to your command (e.g., ssh username@server_ip -p 49221).