arrow_upward

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Change SSHD's Port The Right-Way In CentOS 8
#5
@fChk  Yesterday when I reinstalled my VPS 9 Dallas and noticed the hammering of my Port 22, I tried to change the port number the old fashioned way.  However it didn't want to work.  @Mashirohen directed me to this thread, and that put me totally into the right direction. SELinux and Firewall.  So many steps to follow however and with each version of CentOS they're slightly different.  But at least one can still change the port number.

I am on CentOS 7 x86_64 Minimal v2 and had to do it slightly (very slightly) different, but thought I would put my steps here for the record for CentOS 7:

1. Fresh install of CentOS 7 x86_64 Minimal v2

2. Install Nano and add new 4-digit port number (of own choosing) to sshd configuration (I will use 5000 in the example):

yum install nano

nano /etc/ssh/sshd_config

Scroll with down key to #Port 22 and change it to Port 5000 (4-digit number of own choosing)
Remember to remove the comment # before Port number so it looks like: Port 5000 (replace with your number)

Ctrl X Select Y for Yes and then use Enter Key to exit out of configuration

3. Install semanage and let SELinux know of the new port number

yum install policycoreutils-python

semanage port -a -t ssh_port_t -p tcp 5000
Replace 5000 with new 4-digit port number

Test whether successful:
semanage port -l | grep ssh

Result should show Port 22 + Port 5000

4. Restart sshd and check which ports it is listening to
systemctl restart sshd

netstat -tulpn|grep sshd
The new port number should be in there

4. Add new port number to Firewall

Check whether Firewall is running:

firewall-cmd --state

firewall-cmd --zone=public --add-port=5000/tcp --permanent
(replace 5000 with new port number)

firewall-cmd --reload

Check Port
firewall-cmd --zone=public --list-all
It should show new port number

5. Test new port number by exiting SSH (both user and root) and then logging back in.

6. Disable SSHD Default Port

firewall-cmd --zone=public --remove-service=ssh --permanent

firewall-cmd --reload

Test port:
firewall-cmd --zone=public --list-all
Has to show new port number/tcp in result

And @fChk is correct.  No more hammering of Port 22. Thank you for this thread and Mashiro pointing me to it.  Otherwise I'd never have been able to change the port number.  Big Grin
Terminal
Thank you to Post4VPS and VirMach for my awesome VPS 9!  


Messages In This Thread
RE: How to Change SSHD's Port The Right-Way In CentOS 8 - by deanhills - 05-10-2021, 01:51 AM

Possibly Related Threads…
Thread
Author
Replies
Views
Last Post
7,214
04-13-2020, 06:45 AM
Last Post: Mashiro
7,526
12-16-2019, 06:52 AM
Last Post: youssefbasha

person_pin_circle Users browsing this thread: 1 Guest(s)
Sponsors: VirMach - Host4Fun - CubeData - Evolution-Host - HostDare - Hyper Expert - Shadow Hosting - Bladenode - Hostlease - RackNerd - ReadyDedis - Limitless Hosting