Setting the SSH port in Linux
You can set your SSH port through the command line by editing the ssh configuration file ( /etc/ssh/sshd_config ) with this command. Note: if you are runnning a firewall on the server you will want to open the desired port before proceeding.
nano /etc/ssh/sshd_config
You can now edit the configuration file. It will start something like this.
# $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
Look for the line containing ” #Port 22 “. you will want to delete the # at the begining of the line and then change the “22” to your desired port. When finished it should be like follows, but with your desired port.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 22222
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
Make sure to note the new port number or use a number you will remember. You are limited to 5 digits and will want to select something between 5000 and 99999 as tehse are usually unreserved ports.
Now hit “control + v” this will open a dialog box at the bottom of the window. “Save modified buffer (ANSWERING “No” WILL DESTROY CHANGES) ?” Press “Y” and then press “Enter” to confirm saving your changes.
We will now need to restart ssh to begin using the new port. issue the following command.
service sshd restart
If everyhting worked correctly you will often get no confirmation.
To test the new ssh port I suggest opening a new ssh connection without logging out of your current session.