Change ssh port of CentOS 7
To change SSH port of CentOS 7 please follow those steps-
- 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
- Then open the
sshd_config
file -sudo vi /etc/ssh/sshd_config
and change the port number(ex. 2122) -
Port 2122 #AddressFamily any #ListenAddress 0.0.0.0 #ListenAddress ::
- 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
- Add the new port to the firewall to allow access
sudo firewall-cmd --permanent --zone=public --add-port=2122/tcp
- Reload the firewall configuration file -
sudo firewall-cmd --reload
- Now restart SSH service
sudo systemctl restart sshd.service
- Now exit and try to connect to new port
ssh username@192.168.10.240 -p 2022
Enjoy
Leave a Comment