How To Change OpenSSH Port On CentOS 7

It is a good idea to change the default well-known port of SSH (22) to another port number that will make it more difficult for malicious scripts or unwanted personnel to attempt to login to your CentOS server. It can be a little tricky to get the server setup to listen to the new port and allow it through the firewall.

The first thing I will need is to get the utility semanage installed to make changes to the ports the CentOS server listens on. Before installing new software it is a good idea to update the repository information. To do that run the terminal command:
#yum  update
#yum -y install policycoreutils-python

To change the default SSH port, the first thing you want to do is backup the current SSH configuration on your system. To do that, run the commands below.

#cp /etc/ssh/sshd_config /etc/ssh/sshd_config.org

Next, run the commands below to open the default SSH configuration file

#vi /etc/ssh/sshd_config

When the file opens, make the below change and save the file. Un-comment or remove the (#) before the line the reads Port and change the port number you want to use.


# 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 2929
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

Save the file.

By default, SELinux only allows port 22 for SSH. What you need to do is enable the newly created port through SELinux. To do that, run the commands below

#semanage port -a -t ssh_port_t -p tcp 2929

Restart SSH by  running the commands below.

#systemctl restart sshd.service

Verify that SSH is now running on the new port by running the commands below.

#ss -tnlp | grep ssh

LISTEN     0      128          *:2929                     *:*                   users:(("sshd",pid=830,fd=3))

LISTEN     0      128         :::2929                    :::*                   users:(("sshd",pid=830,fd=4))

Exit and try signing in using the new port number.

#ssh root@192.168.0.1 -p 2929

Enjoy!
Share this article :

Post a Comment

silahkan komentar dibawah ini, komentar anda sangat berarti untuk penulis artikel

 
Support : Creating Website Copyright © 2015. BELAJAR LINUX MANDIRI - All Rights Reserved