Change SSH Port on CentOS 8 — Secure your server now Print

  • SSHD, Server Security, Shell
  • 0

Okay, In this article I will show you how to change default ssh port. And this tutorial I get from many sources.

Why you need to change ssh port? … Security!

Make sure no one have your main key to get into your house without permission and steal whatever there.

Change ssh port just one step to secure your server from bad guy. In my opinion you need do this.

It’s not difficult, I will make very easy for you to understand.

I assume you aleardy open ssh and ready to change it.

First backup your sshd_config, dont re-login or restart your server before tutorial done.

# cp /etc/ssh/sshd_config /etc/ssh/sshd_config_BACKUP

Then open ssh configuration

# sudo nano /etc/ssh/sshd_config

That command will open you SSH Server configuration ( SSHD ).

Add this code on top or bottom your configuration

# SSH port
Port 2525 # port want you change

Next, we will update firewall server, tell the firewall ssh port was changed. If you turn off your firewall you can skip this step. If you still using CentOS 6 you can run this command

For centOS 6

# iptables -I INPUT -p tcp --dport 2525 --syn -j ACCEPT
# service iptables save
# semanage port -a -t ssh_port_t -p tcp 2525

Fort centOS 7/8

# firewall-cmd --add-port 2525/tcp --permanent
# firewall-cmd --add-port 2525/tcp

Configuration is done, now time to restart SSH Server ( SSHD )

# service sshd resrtart

or

# systemctl restart sshd

Error? if you can’t restart SSHD, then you need adjust SELinux policy, because we change port 22 to another port.

Command same with on centOS 6

Run this command.

# semanage port -a -t ssh_port_t -p tcp 2525

Then restart.

Horay! you now aleardy change you port, to check you can relogin using port 22 and what you get? Erorr? Yes of course. You need login with new port.

Congratulations!


Was this answer helpful?

« Back