To change the port, log in to the server.
Open the SSH configuration file:
nano /etc/ssh/sshd_config
In the file, find the line starting with #Port 22
. Remove the #
symbol and replace 22
with the desired port, for example: Port 2222
.
Save the file (CTRL + X, then Y and Enter).
Then add a firewall rule:
ufw allow 2222/tcp
Restart SSH:systemctl restart ssh