By default, the SSH protocol and port 22 are used to connect to a Linux server.
Sometimes, for security or other reasons, a user needs to change the port to a different one.
Changing the port number for SSH connections
First, let's see which ports are free so we don't accidentally take an occupied one.
# netstat -tupln | grep LISTEN
Once you have chosen the new port number and made sure it is not in use, open the file at /etc/ssh/sshd_config
This can be done with an FTP client (WinScp, Filezilla, etc.) or with one of the Linux text editors, vi or nano.
For the vi editor:
# vi /etc/ssh/sshd_config
For the nano editor:
# nano /etc/ssh/sshd_config
In the opened sshd_config file, find the line "Port 22" and change the port number to the desired one.
Now save the file.
For the vi editor:
# :wq
For the nano editor:
# CTRL+O
For the changes to take effect, you need to restart the SSH server.
This can be done with the command:
# /etc/init.d/ssh restart
or
# /etc/init.d/sshd restart
Changing the password for the root user
Log in via SSH as the root user and enter the command:
#passwd root
Press Enter, then enter the new password twice and press Enter again.
Done, the new root password has been set.