How to Change the SSH Port? | phoenixNAP KB (2024)

Introduction

SSH (Secure Shell) is a network protocol that provides a secure way to remotely access a computer or server. Since SSH offers strong password and public key authentication, as well as encrypted data communication between two machines, it is one of the most secure protocols.

However, leaving the default port 22 for SSH creates a security issue that makes the server vulnerable to cyber threats such as brute-force attacks. Therefore, changing the default SSH port is a great way to add extra protection to the server.

In this tutorial, you will learn to change the default SSH port.

How to Change the SSH Port? | phoenixNAP KB (1)

Prerequisites

  • A system running Linux.
  • An account with root privileges.
  • Access to the terminal.

Changing the Default SSH Port

Port numbers range from 0 to 65536, but port numbers 0-1023 are reserved for common TCP/IP applications and are called well-known ports. Well-known ports allow client applications to quickly locate the corresponding server application processes when connecting to hosts.

The following table contains the most common privileged services and their associated ports and functions:

Port NumberServiceDescription
7TCP/UDPEchoes data back to the sender.
13TCP/UDPReports time in a user-friendly format.
19UDPCharacter generator.
20TCPFTP's default data transfer port.
21TCPFTP server control channel.
22TCPSecure Shell (SSH) communication.
23TCPUsed by the Telnet protocol.
25TCPThe default port for relaying emails via SMTP.
53DNSPort for transferring Domain Name System (DNS) queries.
67UDPDHCP server port used to send configuration information.
68UDPDHCP client port used to receive configuration information.
69UDPTrivial file transfer (TFTP).
80TCPHypertext Transfer Protocol (HTTP).
110TCPPost Office Protocol 3 (POP3).
123UDPNetwork Time Protocol (NTP).
143TCPInternet Message Access Protocol (IMAP).
161/162TCP/UDPSNMP ports used to receive network management queries and network problem reports.
443TCPHTTP over SSL/TLS (HTTPS).
636TCP/UDPLDAP over SSL/TLS (LDAPS).
1011-1023ReservedReserved for future use.

Although it is possible to use well-known ports, they might cause a network conflict. Thus, it is safer to choose a port number from 1024-65535 as they are not reserved for any other service.

Follow the steps below to change the default SSH port:

Step 1: Connect to Remote Server

Connect to the remote server via ssh. Open a terminal (Ctrl+Alt+T) and use the following syntax:

ssh username@[ip_address]

For example:

How to Change the SSH Port? | phoenixNAP KB (2)

Step 2: Change SSH Configurations

On the remote server, edit the SSH configuration file located in /etc/ssh/sshd_config. Use a text editor of your choice. The default editor on Ubuntu is nano. Run:

sudo nano /etc/ssh/sshd_config

Warning: Only modify the settings outlined below. Providing an incorrect SSH configuration may render the server inaccessible via SSH. Read our tutorial to learn more about the SSH config file.

Find the line that reads #Port 22. Either uncomment the line and replace 22 with the desired port number or enter a new line below with the new default SSH port:

How to Change the SSH Port? | phoenixNAP KB (3)

Adding a new line allows you to revert to the default settings later by deleting the line.

Save the changes and exit the file.

Alternatively, open the port using iptables. The syntax is:

sudo /sbin/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport [port_number] -j ACCEPT

Replace [port_number] with the port you want to open.

Step 3: Update Firewall Rules

Ensure the firewall is not blocking the port you want to use for SSH. Depending on which firewall you are using, update the firewall settings to allow incoming connections to the specified port. The syntax for configuring the ufw firewall is:

sudo ufw allow [port_number]/tcp

For example, we will allow connections to port 1222:

How to Change the SSH Port? | phoenixNAP KB (4)

The output states that rules have been updated. Verify that the port is in a listening state by running the ss or netstat command:

ss -tulpn | grep [port_number]
netstat -tulpn | grep [port_number]

For example:

How to Change the SSH Port? | phoenixNAP KB (5)

The output shows that the specified port is open.

Note: If you are opening a port on an SELinux system, use the following syntax:

semanage port -a -t ssh_port_t -p tcp [port_number]

Step 4: Restart SSH

For the configuration changes to take effect, restart the SSH service. Run the following command:

sudo service ssh restart

Step 5: Test the New Port

Test if the new port allows you to connect by establishing a new SSH connection to the server.

Important: Test the SSH connection in a new terminal window. Only close the previous root session after you make sure the new setup is working.

The syntax is:

ssh -p [port] username@[ip_address]

For example:

How to Change the SSH Port? | phoenixNAP KB (6)

In the example above, the connection was established using the specified port.

Conclusion

This tutorial showed how to change the default SSH port to additionally secure remote connections to your server. A secure connection is important, especially when dealing with sensitive data, so using a port other than the default one is recommended.

For more information, see how SSH works, read the difference between SSH and Telnet, or see the five SSH best practices for securing your system.

How to Change the SSH Port? | phoenixNAP KB (2024)

FAQs

How to Change the SSH Port? | phoenixNAP KB? ›

On the ssh page, click Change Settings and change the port value from ssh to your new custom SSH port (In this example, it is 2222). Once done, apply the changes. Note: If the default port (22) and custom port are used, add them as "port=2222".

How do I change my SSH port? ›

Changing the SSH Port
  1. Edit the SSH Configuration File. Open the SSHD configuration file with your preferred text editor. ...
  2. Change the Port Number. Edit the line to reflect your desired port number, preferably above 1024 to avoid conflicts with other standard services. ...
  3. Restart the SSH Service.
Sep 6, 2023

How do I allow SSH access to port 2222? ›

On the ssh page, click Change Settings and change the port value from ssh to your new custom SSH port (In this example, it is 2222). Once done, apply the changes. Note: If the default port (22) and custom port are used, add them as "port=2222".

How do I change my SSH connection? ›

To change the SSH port:
  1. Log on to the server as an administrator.
  2. Open the SSH configuration file sshd_config with the text editor vi: vi /etc/ssh/sshd_config.
  3. Search for the entry Port 22.
  4. Replace port 22 with a port between 1024 and 65536.

How to run SSH server on different port? ›

Configuring SSH to run on a nonstandard port
  1. Open /etc/ssh/sshd_config in a text editor. ...
  2. Go to the following line: ...
  3. Uncomment and edit this line to reflect the new port. ...
  4. Save and quit the file, and restart SSH. ...
  5. If you are connected to the server by using SSH on port 22, you need to reconnect by using the new port.

Is it a good idea to change SSH port? ›

Changing the default SSH port is an effective security measure that can reduce your server's vulnerability to attacks.

How do I change my port? ›

How to Change COM Port Number on Windows
  1. Open the Device Manager by pressing the Windows Key + R. Type “devmgmt. ...
  2. Expand the Ports (COM & LPT) section.
  3. Right-click the COM port and select Properties.
  4. Click the Port Settings tab and click Advanced.
  5. Change the COM port number to an available COM port number.

How do I change the port in my terminal? ›

With the explanation out of the way, let's get started with the actual process.
  1. Step 1: Launching the Terminal. First, we need to open the Terminal. ...
  2. Step 2: Edit the SSH Config File. ...
  3. Step 3: (Optional) Change the SSH Port for Security. ...
  4. Step 4: Restart the SSHD Service to Finalize Changes. ...
  5. Step 5: Configure your Firewall.
Jan 17, 2022

Why SSH is not connecting to port 22? ›

The error “port 22: Connection refused” typically indicates that your SSH client is unable to establish a connection with the SSH server. It can occur due to various reasons: The SSH server may not be running on the remote host. The host or IP address provided could be incorrect.

How to unblock SSH port? ›

It's important to make sure that your firewall settings are configured correctly. You can check this by opening the Windows Defender Firewall settings and making sure that the SSH port (port 22) is allowed through the firewall. You may need to create a new inbound rule to allow traffic on this port.

How to check SSH port? ›

There are two general ways to check which port the SSH service is running on. One is checking the SSH configuration file, and the other is examining the running process. On most systems, the SSH configuration file is /etc/ssh/sshd_config .

How to set SSH settings? ›

Procedure
  1. Open the SSH configuration file /etc/ssh/sshd_config and set the appropriate SSH key type. The HostKey directive can be set to specify various SSH key types. ...
  2. Restart the SSH server to apply new settings. ...
  3. Restart the noded service to activate your changes.

How do I reset my SSH connection? ›

Restarting ssh is simple job, exeute:
  1. # /etc/init.d/ssh restart. OR.
  2. # service ssh restart. OR.
  3. $ sudo service ssh restart. If you are using Debian/Ubuntu/Mint Linux with systemd, use the systemctl command:
Mar 5, 2024

How to change SSH port in Windows? ›

  1. Step 1: Connect to Remote Server.
  2. Step 2: Change SSH Configurations.
  3. Step 3: Update Firewall Rules.
  4. Step 4: Restart SSH.
  5. Step 5: Test the New Port.
Feb 13, 2023

Does SSH work on any port? ›

An SSH server, by default, listens on the standard TCP port 22.

What is the alternative port for SSH? ›

We recommend choosing a port above 1024 (any number you choose between 1024 and 65535). You can choose any available port number, but it should not conflict with other services running on your server. Standard alternative ports include 2222 or 8022.

How do I change the default SSH port in Windows 10? ›

How to Change the SSH Server Port Number on Windows in 2022
  1. Click the Start button at the bottom left corner of your screen.
  2. Click RUN.
  3. Type REGEDIT.
  4. Click OK.
  5. Select Registry Key: ...
  6. Select the menu item Edit and then click on Modify.
  7. Enter the new value for the usGSWSSHDPort and click OK.

What is the recommended port for SSH? ›

The OpenSSH suite included in the installer uses TCP/22 as the default port for SSH connections. Aspera recommends opening TCP/33001 and disabling TCP/22 to prevent security breaches of your SSH server.

How to SSH to an IP address and port? ›

Enter the hostname or IP address of your server into the Host Name field and leave the port number set to 22. Select Open to open a connection to your server, you'll see a login prompt to enter a username, then just enter your SSH username and password to connect to your server.

Top Articles
Tether (USDT) TRC-20 vs ERC-20: Differences Between USDT Networks
DJT: Why Trump Media shares closed more than 12% higher
Friskies Tender And Crunchy Recall
Live Basketball Scores Flashscore
9192464227
Produzione mondiale di vino
Santa Clara Valley Medical Center Medical Records
Conduent Connect Feps Login
Pro Groom Prices – The Pet Centre
OSRS Dryness Calculator - GEGCalculators
Pittsburgh Ultra Advanced Stain And Sealant Color Chart
Spartanburg County Detention Facility - Annex I
Playgirl Magazine Cover Template Free
Curtains - Cheap Ready Made Curtains - Deconovo UK
Xxn Abbreviation List 2023
Sport-News heute – Schweiz & International | aktuell im Ticker
25Cc To Tbsp
Voy Boards Miss America
Pekin Soccer Tournament
Officialmilarosee
CDL Rostermania 2023-2024 | News, Rumors & Every Confirmed Roster
Dover Nh Power Outage
Puss In Boots: The Last Wish Showtimes Near Cinépolis Vista
Heart Ring Worth Aj
Raz-Plus Literacy Essentials for PreK-6
Nesb Routing Number
Relaxed Sneak Animations
Stockton (California) – Travel guide at Wikivoyage
Mumu Player Pokemon Go
Kokomo Mugshots Busted
Everstart Jump Starter Manual Pdf
Puerto Rico Pictures and Facts
Craigslist In Myrtle Beach
Goodwill Thrift Store & Donation Center Marietta Photos
Pensacola 311 Citizen Support | City of Pensacola, Florida Official Website
Mta Bus Forums
Labyrinth enchantment | PoE Wiki
Trivago Myrtle Beach Hotels
What Does Code 898 Mean On Irs Transcript
Thelemagick Library - The New Comment to Liber AL vel Legis
Wayne State Academica Login
Directions To The Closest Auto Parts Store
VDJdb in 2019: database extension, new analysis infrastructure and a T-cell receptor motif compendium
Powerspec G512
2Nd Corinthians 5 Nlt
The Many Faces of the Craigslist Killer
A rough Sunday for some of the NFL's best teams in 2023 led to the three biggest upsets: Analysis
Aurora Southeast Recreation Center And Fieldhouse Reviews
300 Fort Monroe Industrial Parkway Monroeville Oh
Strange World Showtimes Near Century Federal Way
Southwind Village, Southend Village, Southwood Village, Supervision Of Alcohol Sales In Church And Village Halls
Craigs List Sarasota
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 5844

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.