How can I change the SSH port on CentOS 7?

To change the SSH port on CentOS 7, follow these steps:

  1. Log in to the CentOS 7 server as the root user.
  2. Open the SSH configuration file with the following command:
  3. Open the sshd_config file in the /etc/ssh directory using a text editor.
  4. Using the vi editor, locate and edit the following line:
  5. Port number 22.
  6. Change it to the SSH port number you want to use, for example:
  7. Port number 2222
  8. Please make sure the selected port number is not being used by any other service.
  9. Save and close the file.
  10. To restart the SSH service, use the following command:
  11. Restart the sshd service.
  12. Make sure that the firewall allows the new SSH port to pass through. If you are using the firewalld firewall, you can use the following command to add a rule:
  13. Permanently add port 2222/tcp to the public zone using firewall-cmd, then reload the firewall.
  14. If you are using an iptables firewall, you can add rules using the following command:
  15. Add a rule to allow incoming TCP traffic on port 2222 in the iptables configuration, then save the changes.
  16. Please note that this is just an example command, you will need to make appropriate modifications based on your actual firewall configuration.
  17. Connect to the server using a new SSH port on the client side, for example:
  18. Connect to your server with the username using SSH on port 2222.
  19. Please replace “username” with your username, “your_server_ip” with the server’s IP address, and “2222” with the new SSH port number you have set.

In this way, you have successfully changed the SSH port on CentOS 7.

bannerAds