• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Change the SSH Port in Linux Server

Written by Admin, Updated On May 1, 2020
security, ssh
How to Change SSH Port in Linux

SSH is a network protocol which is used to remotely communicate securely with a Linux systems. By default, SSH service is using port 22. You can add additional security layer by changing SSH port to your server and reduce risk of attacks by hackers and bots. In this tutorial, you will learn how to change SSH port in Linux systems.

Change SSH Port#

Following are the steps to change SSH port in Linux systems:

Step 1 – Select a New Port#

Linux systems are reserving port numbers below 1024 for it’s services. You can also use a port within 1-1024 range for the SSH service but it’s recommend to choose a port higher than 1024 to avoid future issue. You can choose maximum port number up to 65535 for SSH service.

We are going to use port 4567 for SSH service in this tutorial, You can choose as per your choice.

Step 2 – Setting Up Firewall#

If your server have enabled firewall then you need to adjust it with new SSH port before changing it. So it will allow traffic on the new SSH port.

FirewallD is default firewall management tool in CentOS systems. You can open new port using below command on CentOS machines:

sudo firewall-cmd --permanent --zone=public --add-port=4567/tcp
sudo firewall-cmd --reload

In CentOS or RHEL Linux based distributions another requirement is to adjust the SELinux rules to allows the new SSH port. You can do it by typing:

sudo semanage port -a -t ssh_port_t -p tcp 4567

In Ubuntu systems, default firewall tool is UFW. Run below command to allow connection using new SSH port:

sudo ufw allow 4567

If you have installed iptables and using as firewall on your Linux server, you can open port by execute below command:

sudo iptables -A INPUT -p tcp --dport 4567 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

Step 3 – Configuring SSH#

In Linux, SSH service default port are stored in /etc/ssh/sshd_config file. At first, you need to open the main SSH configuration file for editing with your favorite text editor by issuing the below command:

sudo nano /etc/ssh/sshd_config

Now search line inside file which starts with Port 22. Mostly, this line is comment out with a hash #sign. Remove the hash # and enter your new SSH port number which will be used instead of the standard SSH port 22. So it should look like as below:

Port 4567

After you’ve made the above changes, restart the SSH service to reflect changes.

sudo systemctl restart ssh

In CentOS or RHEL Linux based distributions the ssh service is named sshd so you need to run following command to restart SSH service:

sudo systemctl restart sshd

You can verify that SSH daemon is listening on the new port 4567 by issuing below command:

ss -an | grep 4567

It will show output like this:

tcp        LISTEN   0    128       0.0.0.0:4567      0.0.0.0:*
tcp LISTEN 0 128 [::]:4567 [::]:*

Step 4 – SSH Connection Using New Port#

Now, you have changed successfully port for SSH service. To make connection using new port you have to mention port number -p with SSH command as below:

ssh -p 4567 username@remote_ip_address

Conclusion#

You learned how to change SSH port on your Linux system. If you have any questions or suggestion you can leave comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Set Up SSH Keys on Debian 9 System
Next Article   How to Change Hostname on CentOS 7

Related Posts

  • How to Install Set Up Firewall on Ubuntu 22.04

    How to Setup a Firewall with UFW on Ubuntu 22.04

    March 6, 2023
  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook