Skip to main content

Change SSH Port in Linux CentOS (Oracle Linux 8)

· 2 min read
Suraj Jha

On Linux systems, the default SSH port is 22. There are a few reasons why you might want to change this number. This article will walk you through the process of changing the default ssh port 22 to 8080.

Prerequisite

  • If you are using oracle cloud, open port 8080 in Oracle Cloud Security list for SSH Access.

Setup VM Firewall

Check firewall state

sudo firewall-cmd --state

Login as root user

sudo su root

Update Port 22 to 8080

vi /usr/lib/firewalld/services/ssh.xml
~/usr/lib/firewalld/services/ssh.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>SSH</short>
<description>Secure Shell (SSH) is a protocol ... to be useful.</description>
<port protocol="tcp" port="8080"/>
</service>

Reload firewall and network

firewall-cmd --reload
systemctl restart network
systemctl reload firewalld

SSH Configuration

Set port 8080 as ssh port

semanage port -m -t ssh_port_t -p tcp 8080
Solve the error semanage: command not found and retry above command
Check your linux distribution
cat /etc/os-release | grep -E -i 'Fedora|Debian|CentOS'
Install semanage via apt
apt-get install policycoreutils-python-utils

Update ssh port 22 to 8080

vi /etc/ssh/sshd_config
~/etc/ssh/sshd_config
...
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
Port 8080
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

HostKey /etc/ssh/ssh_host_rsa_key
...

Restart ssh daemon

systemctl restart sshd

Testing

Open another terminal and try to ssh through new port

ssh -i "PATH_TO_SSH_KEY" -p 8080 USERNAME@IP_ADDRESS -p 8080

References