In this article we’ll show you how to change your default SSH port from the default 22 to a custom 24.
How to change the SSH port on your server
Please note. This article is for Mac users using Terminal.
How to change your SSH port on a Mac
- Click on the magnifying glass in the top right corner of your screen.
- Type in the word Terminal and press enter.
- The server we’ll be using is MyServer.
- Type in ping 154.0.169.200 and press enter.
- The default user is Root. Now type in ssh root@154.0.169.200 -p 22 and press enter.
- Enter your password if prompted.
- If you run ss -ntlp |grep ssh it will show you that the SSH is currently running on port 22 on your server. We’re going to change this to port 24.
- Now type in vi /etc/ssh/sshd_config.
- Go down to where it says ‘Port 22’.
- Press ‘i’ on your keyboard to enter the ‘Insert mode’ and remove the hash (#) that is hashing out the port.
- Now change 22 to 24.
- Now press ‘Escape’ on your keyboard to exit ‘Insert mode’.
- Type :wq! and press enter to save your changes.
Restart the SSH service
- Type systemctl restart sshd.service and press enter.
- Now if we do ss -ntlp |grep ssh, you’ll see that SSH is not running. This is because SELinux is enabled and we need to disable that in order to enforce the change.
Disable SELinux
- Type vi etc/selinux/config and press enter.
- Go down to the ‘SELINUX=enforcing’ line and press ‘i’ on your keyboard to enter ‘Insert mode’.
- Change ‘enforcing’ to ‘disabled’.
- Hit ‘Escape’ on your keyboard to exit ‘Insert mode’.
- Type :wq! and press enter to save the changes.
- To enforce the change type setenforce 0 and press enter.
Restart the SSH service again
- Type systemctl restart sshd.service and press enter.
- Now type ss -ntlp |grep ssh.
- You’ll now see that the port has been change to 24.
Enable the public zone for port 24
- Type firewall-cmd—zone=public—add-port=24/tcp and press enter.
- You’ll see that TCP for port 24 is already enabled.
- Now type exit and press enter.