How to change SSH port on Centos 7.x?

SSH or secure shell is a network protocol. We use it to connect and access remote servers. We can change the default SSH port to enforce the security against the brute force attacks on your servers. 

Please refer to the following steps to change the default SSH port on CentOS.

  1. Login to your CentOS server using root or sudo user.

  2. Open the file sshd_config at the path /etc/ssh using the below command in your favorite editor. 

  3. Uncomment the line mentioned port 22 and change it to your required port. Here we have changed it to port 8756.

  4. Save and exit from the sshd_config file. 

  5. If you have installed the firewall on your server, please ensure that you add the new RDP port. Else, it will not allow connecting your system using ssh.

    Allow SSH port on Firewalld

    firewall-cmd --zone=public --add-port=8756/tcp --permanent
    firewall-cmd --reload

    Allow SSH port on Iptables

    iptables -A INPUT -p tcp -m tcp --dport 8756 -j ACCEPT
    systemctl restart iptables
  6. Run the below command to restart the ssh service. 

    systemctl restart sshd

Was this answer helpful?

« Back