These are 4 easy steps that helps you to change SSH port on your Ubuntu box
1. Open the Terminal
2. Edit ssh_config
sudo nano /etc/ssh/ssh_config
3. Change the port
Find the line
Port 22
and change to
Port 2222
4. Ctrl + X to exit nano and answer Y to save the file
5. Restart SSH Server
sudo /etc/init.d/ssh restart
Another way to do it, which I personally by far prefer, because it avoids messing around with the default settings is using socat to redirect port 22 to whichever port you want.
A. Download socat: hhttp://www.dest-unreach.org/socat/download/socat-1.7.1.3.tar.gz
B. Move the tar.gz file to your /usr/local/ directory (sudo mv ./socat-1.7.1.3.tar.gz /usr/local/socat-1.7.1.3.tar.gz)
C. Go to your /usr/local directory (cd /usr/local)
D. Uncompress: sudo tar -xvzf socat-1.7.1.3.tar.gz
E. Move to the uncompressed file directory: cd ./socat-1.7.1.3
F. Run the usual configure, make and make install to install socat (sudo ./configure && make && make install)
G. Redirect port 22 (default ssh) to any port you want (in the following ex., 2222) using the correct option by sending a socat call (socat tcp4-listen:2222,reuseaddr,fork TCP:localhost:22)
You’re done and your default settings are left unchanged.
The last thing you need to do if you use a router/firewall is to include the correct redirect commands in your router/firewall.
With a little research, you may also easily prepare a script that runs at start up to rebuild the socat redirection each you restart your machine;
In addition, you can also improve security by (i) setting your firewall to block any connections to your port 22 from any other interface than the loopback (127.0.0.1) and (ii) make a similar change in your sshd.conf file to have ssh listen on the loopback only.
Enjoy.
Thank Cedric for such a great tip