how to change ssh port

How to Change SSH Port: Step-by-Step Guide

By John Anderson
AD

Securing your server is a top priority, and one way to do this is by changing the default SSH port. The default SSH port is 22, which is well-known and often targeted by attackers. By changing it to a custom port, you can significantly improve your server's security.

Changing the SSH port is a straightforward process, but it's important to do it correctly to avoid locking yourself out of your server. In this guide, we'll walk you through the steps to change your SSH port on both the server and client sides, using Ubuntu as an example.

Changing the SSH Port on the Server

The first step is to change the SSH port on the server. SSH uses the OpenSSH server, which is configured in the /etc/ssh/sshd_config file.

AD
Mastering the SSH Config File
Mastering the SSH Config File
the diagram shows how to use ssh configurations for different types of networked devices
the diagram shows how to use ssh configurations for different types of networked devices
5 Ways to Harden Your Linux Server | Linux Security Basics
5 Ways to Harden Your Linux Server | Linux Security Basics

However, before we proceed, make sure your server is fully backed up to prevent any data loss in case something goes wrong.

Editing the SSH Configuration File

Open the SSH configuration file with a text editor. For instance, you can use nano:

the diagram shows how ssh works and what it is doing to do with it
the diagram shows how ssh works and what it is doing to do with it
open ssh checklist
open ssh checklist
A Practical Guide to SSH Tunnels: Local and Remote Port Forwarding | iximiuz Labs
A Practical Guide to SSH Tunnels: Local and Remote Port Forwarding | iximiuz Labs
sudo nano /etc/ssh/sshd_config

Upgrading the SSH Port

Look for the line that says "Port 22". Change this to a new port. For this guide, let's use 2222:

Port 2222

You can choose any port number you like, but avoid using a port that's already in use or reserved by the system. After changing the port, save the file and exit the editor.

AD

Restarting the SSH Service

Changes to the SSH configuration take effect when the SSH server is restarted. You can do this with the following command:

the diagram shows how ssh works and what it is doing to do with it
the diagram shows how ssh works and what it is doing to do with it
How SSH Works
How SSH Works
Linux Tip #16: Speed Up Repeated SSH Connections with Multiplexing
Linux Tip #16: Speed Up Repeated SSH Connections with Multiplexing
How to Use SSH to Safely Connect to GitHub: A Simple Guide for Windows OS
How to Use SSH to Safely Connect to GitHub: A Simple Guide for Windows OS
How to Create and Restore cPanel backup via SSH?
How to Create and Restore cPanel backup via SSH?
an image of a computer screen with the text,'ittpp / 1 pipeline 1979
an image of a computer screen with the text,'ittpp / 1 pipeline 1979
sudo service ssh restart

Congratulations! You've just changed the SSH port on your server. However, remember that this won't take effect on the client side yet. Let's address that next.

Connecting to the New SSH Port on the Client

Now that your server is using a new SSH port, you need to configure your client to connect to this new port. We'll use the OpenSSH client as an example.

AD

Specifying the New Port in the SSH Command

The simplest way to connect to your server's new SSH port is by specifying it in the SSH command. For instance:

How SSH Authentication with GitHub Works Under the Hood
How SSH Authentication with GitHub Works Under the Hood
a computer screen with the words i'm choosing homewf starlink
a computer screen with the words i'm choosing homewf starlink
SSH Explained: How Secure Shell Works 🔐🐧💻 **SSH = Secure Shell** It is a network protocol used to securely connect to and manage another computer over a network. 🌐 **How SSH Works** Your Computer 💻 ↓ 🔐 Encrypted SSH Connection ↓ 🖥️ Remote Server Once connected, you can interact with the remote machine through a terminal. ⚡ **Common SSH Uses** 1️⃣ Remote server management 2️⃣ Running commands on another computer 3️⃣ Secure file transfers 4️⃣ Managing cloud servers 5️⃣ Deploying applicat...
SSH Explained: How Secure Shell Works 🔐🐧💻 **SSH = Secure Shell** It is a network protocol used to securely connect to and manage another computer over a network. 🌐 **How SSH Works** Your Computer 💻 ↓ 🔐 Encrypted SSH Connection ↓ 🖥️ Remote Server Once connected, you can interact with the remote machine through a terminal. ⚡ **Common SSH Uses** 1️⃣ Remote server management 2️⃣ Running commands on another computer 3️⃣ Secure file transfers 4️⃣ Managing cloud servers 5️⃣ Deploying applicat...
ssh -p 2222 user@remote_server

Here, -p specifies the port, user is your server's username, and remote_server is your server's IP address or hostname.

Adding a New SSH Configuration Alias

If you connect to your server often, it might be more convenient to create an SSH configuration alias. Open your SSH configuration file with a text editor:

AD
nano ~/.ssh/config

Then, add the following lines:

Host new_server
  HostName remote_server
  Port 2222
  User user

Replace 'new_server' with a name of your choice, 'remote_server' with your server's IP address or hostname, and 'user' with your server's username. Now, you can connect to your server using the new port like this:

ssh new_server

So there you have it! You've successfully changed your SSH port and connected to it from a client. This is a great step towards enhancing your server's security. Make sure to test your connection and, if everything works well, update your firewall rules to allow traffic on your new SSH port.

AD