Domains and Hosting

How to change the SSH port on your server

In this article we’ll show you how to change your default SSH port from the default 22 to a custom 24.

Please note. This article is for Mac users using Terminal.

How to change your SSH port on a Mac

  1. Click on the magnifying glass in the top right corner of your screen.
  2. Type in the word Terminal and press enter.
  3. The server we’ll be using is MyServer.
  4. Type in ping 154.0.169.200 and press enter.
  5. The default user is Root. Now type in ssh root@154.0.169.200 -p 22 and press enter.
  6. Enter your password if prompted.
  7. 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.
  8. Now type in vi /etc/ssh/sshd_config.
  9. Go down to where it says ‘Port 22’.
  10. Press ‘i’ on your keyboard to enter the ‘Insert mode’ and remove the hash (#) that is hashing out the port.
  11. Now change 22 to 24.
  12. Now press ‘Escape’ on your keyboard to exit ‘Insert mode’.
  13. Type :wq! and press enter to save your changes.

Restart the SSH service

  1. Type systemctl restart sshd.service and press enter.
  2. 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

  1. Type vi etc/selinux/config and press enter.
  2. Go down to the ‘SELINUX=enforcing’ line and press ‘i’ on your keyboard to enter ‘Insert mode’.
  3. Change ‘enforcing’ to ‘disabled’.
  4. Hit ‘Escape’ on your keyboard to exit ‘Insert mode’.
  5. Type :wq! and press enter to save the changes.
  6. To enforce the change type setenforce 0 and press enter.

Restart the SSH service again

  1. Type systemctl restart sshd.service and press enter.
  2. Now type ss -ntlp |grep ssh.
  3. You’ll now see that the port has been change to 24.

Enable the public zone for port 24

  1. Type firewall-cmd—zone=public—add-port=24/tcp and press enter.
  2. You’ll see that TCP for port 24 is already enabled.
  3. Now type exit and press enter.

Still have questions? Contact us on any of the platforms below