Before changing the SSH port on your server, ensure that you’re logged in as a user with sudo privileges or root user.
Here are the steps:
1. Open the SSHD configuration file:
`sudo nano /etc/ssh/sshd_config`
(Note: We’re using `nano` as the text editor in this example, but you can use the editor you’re comfortable with such as `vim` etc.)
1. Look for a line that says `Port 22`. If you can’t find it, you can add it at the top of the file. This represents the current port number.
1. Change `22` to your desired port number. Make sure you choose a port number that is not in use by any other service. For example:
`Port 2222`
No other service should be using this port, and it should be opened in your firewall.
1. Save and close the file. In `nano`, you can do this by pressing `ctrl+x`, then `y` to confirm that you want to save the changes, and then `Enter` to confirm the file name.
1. Then, restart the SSH service for the changes to take effect. The command to restart the service varies depending on your distribution:
- Ubuntu/Debian:
`sudo systemctl restart ssh`- CentOS/Fedora:
`sudo systemctl restart sshd`1. You should now test your new configuration by opening a new terminal window and try connecting using the new port number:
`ssh -p 2222 user@hostname`
Here `-p 2222` should be replaced by `-p your_new_port_number`.
Caution: Do not close the current SSH session until you have confirmed that the new settings work. If there’s a configuration error, and you’ve closed all sessions, you could potentially lock yourself out of the server.