Please refer to the following steps to change the SSH port in Ubuntu.
1. Log in to your server via SSH.
2. Open the sshd_config file using this command –
# sudo nano /etc/ssh/sshd_config
3. Check the line which starts with #port 22.
We will need to uncomment this line and change the port number to your desired port number; from the default port 22 to port 232.
4. Save the changes that you have made and exit from the file. Now, you will have to reload the file by executing the following command –
# sudo systemctl reload sshd
5. To ensure everything is working fine, you will need to log out of the current session and try to log in with the new SSH port in the format given below –
ssh root@your_ip_address -p port
You have successfully changed your SSH port.