How do we change the SSH port in Linux

  1. Login to the server with the console(KVM/Drac/iLO/XenCenter)
  2. Edit the sshd_config file
    • # vi /etc/ssh/sshd_config
    • Uncomment Port22 and change it to 8xxx (If there is another port listed then change it to 8xxx)
    • Save the file
  3. Restart the SSH service
    • # /etc/init.d/sshd restart
  4. Test SSH to make sure you can connect
    • If you cannot connect then open port 8xxx in the firewall:
      • # iptables -I INPUT -p tcp –dport 8xxx -j ACCEPT
      • For Xen servers, you will need to edit /etc/sysconfig/iptables and add the following line:
        • for xenserver 6.2 and earlier : -A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8xxx -j ACCEPT for xenserver 6.5 and later : A RH-Firewall-1-INPUT -m conntrack –ctstate NEW -m tcp -p tcp –dport  8xxx -j ACCEPT
        • then save the file and restart the firewall with # service iptables restart
  5. Always check the firewall and if there is a rule for port 22 change it to 8xxx. For CentOS this is located at /etc/sysconfig/iptables. After editing the file iptables restart the iptables service.