Home > Linux > Changing SSH Ports CentOS 7

Changing SSH Ports CentOS 7

Here is how to change the SSH port in CentOS 7

All commands require root privileges

SSH into the server with the default SSH port 22

edit /etc/ssh/sshd_config 

un-comment #Port and put your new SSH port number there

for example, if you choose 9898 as your new port, the file should look like

# 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 9898

Make sure that the port you choose is not being used for any other purposes. For this you can use these commands

# list all services with their associated ports
semanage port -l

# check the current ssh port - you will see a couple of matches including the ssh_port_t
semanage port -l | grep '22'
ssh_port_t tcp 22

# check your new port - there should not be any matches with the exact port 
semanage port -l | grep '9898'

Now you need to tell SELinux about the new port – execute the command

semanage port -a -t ssh_port_t -p tcp #YOUR-NEW-PORT-NUMBER 

Example

# -a: add, -t: service type, -p: protocol 
semanage port -a -t ssh_port_t -p tcp 9898

Restart the sshd service

systemctl restart sshd.service

DO NOT GET OUT OF YOUR CURRENT SESSION UNTIL YOU CAN LOG IN WITH THE NEW PORT SETTINGS 

this is just a precaution because if something goes wrong, you still have an active session that you can use to make modifications

if you close this session and you SSH with the new port does not work, then your system might become inaccessible

Now open a SEPARATE SSH session and try to log in with the new port

If you can log in then everything worked fine

you can also check the port assignments with

semanage port -l | grep ssh_port_t
Advertisements
Categories: Linux
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: