Change ssh port of CentOS 7

less than 1 minute read ~

To change SSH port of CentOS 7 please follow those steps-

  1. At first backup the default configuration file. It is always a best practice to backup files before modification, so that we can restore it if anything unwanted happends.
    sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
  2. Then open the sshd_config file -
    sudo vi /etc/ssh/sshd_config

    and change the port number(ex. 2122) -

    Port 2122
    #AddressFamily any
    #ListenAddress ::
  3. 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
    sudo semanage port -a -t ssh_port_t -p tcp 2122
  4. Add the new port to the firewall to allow access
    sudo firewall-cmd --permanent --zone=public --add-port=2122/tcp
  5. Reload the firewall configuration file -
    sudo firewall-cmd --reload
  6. Now restart SSH service
    sudo systemctl restart sshd.service
  7. Now exit and try to connect to new port
    ssh username@ -p 2022


Leave a Comment