To change the port when connecting via SSH, you need to run the following commands:
Ubuntu
Open the SSH configuration file in a text editor:
sudo nano /etc/ssh/sshd_config
We are looking for the line #Port 22, under it we will add the line (the port you need) Port 4000
We save and exit the configuration file (Ctrl+X) And
restart the ssh service:
sudo service sshd restart
We allow incoming SSH connections on the server:
sudo ufw allow ssh
Opening the port:
sudo ufw allow 4000
If you need to close the default port, run the following command:
sudo ufw deny 22
Centos
Installing a text editor:
yum install nano
Open the SSH configuration file in a text editor:
nano /etc/ssh/sshd_config
We are looking for the line #Port 22, under it we will add the line (the port you need) Port 1234
Installing policycoreutils-python to resolve the port:
yum install -y policycoreutils-python
Now you need to allow this port to connect via SSH:
semanage port-a-t ssh_port_t-p tcp 1234
The firewall is enabled by default in the CentOS 7 system. Therefore, you need to add a rule to it:
firewall-cmd --add-port=1234/tcp --permanent
And restart:
firewall-cmd --reload
Performing a reboot of the ssh service:
systemctl restart sshd