How can I change the SSH port on CentOS 7?
To change the SSH port on CentOS 7, follow these steps:
- Log in to the CentOS 7 server as the root user.
- Open the SSH configuration file with the following command:
- Open the sshd_config file in the /etc/ssh directory using a text editor.
- Using the vi editor, locate and edit the following line:
- Port number 22.
- Change it to the SSH port number you want to use, for example:
- Port number 2222
- Please make sure the selected port number is not being used by any other service.
- Save and close the file.
- To restart the SSH service, use the following command:
- Restart the sshd service.
- 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:
- Permanently add port 2222/tcp to the public zone using firewall-cmd, then reload the firewall.
- If you are using an iptables firewall, you can add rules using the following command:
- Add a rule to allow incoming TCP traffic on port 2222 in the iptables configuration, then save the changes.
- Please note that this is just an example command, you will need to make appropriate modifications based on your actual firewall configuration.
- Connect to the server using a new SSH port on the client side, for example:
- Connect to your server with the username using SSH on port 2222.
- 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.