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
#1
Before we begin, please note that changing SSHD's port isn't much about security but about reducing the size of your log files and shaving off few CPU cycles due to the inevitable automated(/random) login attempts directed towards the default SSH port (ie port 22.)

Indeed, our assumption here is that you do know better and that you've already disabled SSH password login in favour of the public-key authentication [1]; thus our problem is to get SSH daemon off its default port to not bother with those failed login attempts that are piling up in the logs and get the 'poor' fail2ban service a break (in case you did install it), which save us few CPU cycles.

1-Make SSHD Listen on 2 Ports:
What we'll do is to configure 'sshd' to listen on two ports, the default and the random one (eg 4600.) This is a good practice to avoid being locked out of the VPS if something goes wrong.
vi /etc/ssh/sshd_config
# Uncomment the line, # Port 22
Port 22
Port 4600

Next we need to check if SSHD does indeed listen on both ports, as follows:
[root@centos ~]# netstat -tulpn|grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      836/sshd            
tcp6       0      0 :::22                   :::*                    LISTEN      836/sshd            

At our surprise, SSHD failed to listen on our selected port!!.. What might be the cause? If we check the logs, we see this:
[root@centos ~]# journalctl -u sshd -f
-- Logs begin at Wed 2020-04-08 14:54:06 +01. --
Apr 10 16:39:52 centos.xyz.xy sshd[836]: Server listening on :: port 22.
Apr 10 16:39:52 centos.xyz.xy sshd[836]: error: Bind to port 4600 on 0.0.0.0 failed: Permission denied.
Apr 10 16:39:52 centos.xyz.xy sshd[836]: error: Bind to port 4600 on :: failed: Permission denied.
Apr 10 16:39:52 centos.xyz.xy systemd[1]: Started OpenSSH server daemon.

2- Enable the Selected Port Usage by SSHD with SELinux:
So, SSHD failed with a permission denied. Of course, SSHD doesn't have the permission to listen on any port other than its own/22; says WHO?.. Well, SELinux says so, and it's in its enforcing mode.
[root@centos ~]# semanage port -l | grep ssh
ssh_port_t                     tcp      22

What we need to do is ask SELinux for permission to use port 4600 for SSHD, like so:
[root@centos ~]# semanage port -a -t ssh_port_t -p tcp 4600
# Now let's see:
[root@centos ~]# semanage port -l | grep ssh
ssh_port_t                     tcp      4600, 22

Now let's test again to see if it's working:
# First restart SSHD
[root@centos ~]# systemctl restart sshd
#check on which ports SSHD is listening on:
[root@static ~]# netstat -tulpn|grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2776/sshd          
tcp        0      0 0.0.0.0:4600            0.0.0.0:*               LISTEN      2776/sshd          
tcp6       0      0 :::22                   :::*                    LISTEN      2776/sshd          
tcp6       0      0 :::4600                 :::*                    LISTEN      2776/sshd          
It's all good!

3- Open the Firewall to the Selected SSHD Port:
We need to let the port 4600 through the firewall:

Before: only ssh and dhcp services are allowed to pass through
[root@centos ~]# firewall-cmd --zone=public --list-all
public (active)
 target: default
 icmp-block-inversion: no
 interfaces: ens3
 sources:
 services: dhcpv6-client ssh
 ports:
 protocols:
 masquerade: no
 forward-ports:
 source-ports:
 icmp-blocks:
 rich rules:

We now want to allow port 4600/tcp through
[root@centos ~]# firewall-cmd --zone=public --add-port=4600/tcp --permanent
success
[root@centos ~]# firewall-cmd --reload
success

Now TCP traffic through port 4600 is also allowed:
[root@centos ~]# firewall-cmd --zone=public --list-all
public (active)
 target: default
 icmp-block-inversion: no
 interfaces: ens3
 sources:
 services: dhcpv6-client ssh
 ports: 4600/tcp
 protocols:
 masquerade: no
 forward-ports:
 source-ports:
 icmp-blocks:
 rich rules:

4-Test SSHD Login with the Custom Port:
At this stage we have to completely logout and test if we can login with the new port:
If we exit our shell both as root and sudoer:
[root@centos ~]# exit
logout
[user@centos ~]$ exit
logout
Connection to 50.*.*.* closed.
[user@local ~]$ ssh -i ~/.ssh/centos-key-ecdsa 50.*.*.* -p 4600
Activate the web console with: systemctl enable --now cockpit.socket

Last login: ....
[user@centos ~]............................YOUR.IN
Our setup is working!

5-Disable SSHD Default Port:
> To Shut-off SSHD default port through the firewall
[root@centos ~]# firewall-cmd --zone=public --remove-service=ssh --permanent
success
[root@centos ~]# firewall-cmd --reload
success
[root@centos ~]# firewall-cmd --zone=public --list-all
public (active)
 target: default
 icmp-block-inversion: no
 interfaces: ens3
 sources:
 services: dhcpv6-client
 ports: 4600/tcp
 protocols:
 masquerade: no
 forward-ports:
 source-ports:
 icmp-blocks:
 rich rules:
From this moment onward, we're done with those random poking around port 22; the logs will be much cleaner and fail2ban will thank you for it.

6- Make SSHD Listen on the Selected Port Only:
> Last thing to do is to completely remove port 22 from the SSHD config and restart the SSHD daemon, and I'll leave that to you.

That's all there is to it.


Notes:
[1]- If you're unsure, please check Running Fedora inside an LXD/LXC System Container for a reminder.
VirMach's Buffalo_VPS-9 Holder (Dec. 20 - July 21)
microLXC's Container Holder (july 20 - ?)
VirMach's Phoenix_VPS-9 Holder (Apr. 20 - June 20)
NanoKVM's NAT-VPS Holder (jan. 20 - ?)


Messages In This Thread
How to Change SSHD's Port The Right-Way In CentOS 8 - by fChk - 04-10-2020, 06:26 PM

Possibly Related Threads…
Thread
Author
Replies
Views
Last Post
6,233
04-13-2020, 06:45 AM
Last Post: Mashiro
6,606
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