How To Change SSH Port on Linux VPS?

Change the default SSH port on Linux by editing the SSH configuration and updating firewall and SELinux rules on Debian and RHEL systems.
10 min read
Intermediate
how to change SSH port on linux

By default, Linux SSH servers listen for incoming connections on TCP port 22. You can change this port by modifying the SSH server configuration, updating firewall rules, and adjusting SELinux policies if enabled.

When you buy Linux VPS, SSH is typically configured to use the default port 22. However, some VPS providers may configure SSH to use a different port for security or network management purposes. Always check which SSH port your provider has assigned before connecting to your server.

This tutorial explains how to change the SSH port on Ubuntu, Debian, CentOS Stream, and AlmaLinux. It also covers Ubuntu’s systemd socket activation, testing the new SSH connection, and resolving common errors that may occur during the process.

Make sure you keep the current session open during the process until you’ve successfully connected using the new port; otherwise, you can be locked out of your server.

Note:
The steps in this guide have been tested and verified by the VPSMakers technical support team on Ubuntu 24.04 LTS, Ubuntu 26.04 LTS, and CentOS Stream 9. The instructions also apply to other Debian-based and RHEL-based Linux distributions, with minor differences depending on the distribution.

Before Changing the SSH Port on Linux

Before changing the default SSH port, make sure the desired port you want to change the SSH to is available and allowed through the VPS firewall. Changing the SSH port without updating the firewall or SSH configuration correctly can prevent you from connecting to your server.

Check Current SSH port

First, check your current SSH port if you already don’t know what you are using:

sudo grep -i '^Port ' /etc/ssh/sshd_config
OUTPUT
Port 22

Note:
This command only checks the main SSH configuration file (/etc/ssh/sshd_config). If your SSH configuration uses Include directives to load additional configuration files, the active port setting may be defined in one of those files instead. In that case, check the included configuration files as well to identify the port SSH is configured to use.

Check Listening ports

Next, you should check your server’s listening ports and verify that your desired port is not listening for any connections. To learn how you can check listening ports on Linux, you can follow this guide:

Learn how to check open ports in Linux using netstat, ss, lsof, Nmap, and Netcat with practical commands and examples.

Read the full article

You can use this command to check too; replace the PortNumber with the port you want to change your SSH port to.

sudo ss -tuln | grep ':PortNumber'

Next, check your existing firewall rules to make sure the new SSH port is allowed for incoming connections. Before making changes, we also recommend backing up your SSH keys and configuration, if necessary, your VPS data. You can also use an alternative access method, such as your provider’s VPS console, in case you lose SSH connectivity.

How to Change the SSH Port on Ubuntu and Debian

On Ubuntu and Debian, you can change the default SSH port by editing the SSH server configuration file, allowing the new port through your firewall, and restarting SSH services.

Note:
Since Ubuntu is based on Debian, the process of changing the SSH port is largely the same on both distributions. So if you are working on a Debian VPS, you can follow the same process.

Edit the SSH Configuration File

After logging in, open the SSH server configuration file using Nano:

sudo nano /etc/ssh/sshd_config

Find the line #Port 22, remove the #, and replace 22 with your preferred port, such as 2222:

port 2222

Press Ctrl + X to exit Nano. When prompted to save the changes, press Y and then Enter to confirm the filename.

Allow SSH Port Through UFW

Then you need to allow a connection to the new port you’ve configured for your SSH connection in UFW on your Ubuntu VPS. For a detailed guide on how to open a port on Ubuntu using UFW, you can follow this guide:

How to Open a Port or IP With UFW in Ubuntu?
READ How to Open a Port or IP With UFW in Ubuntu?

Allow incoming connections on your new SSH port by replacing 2222 in the command below and running it.

sudo ufw allow 2222/tcp
sudo ufw status

If UFW is inactive, check whether another firewall or your VPS provider’s external firewall is filtering incoming connections.

After setting up firewall rules, restart the SSH services:

sudo systemctl restart ssh

Configure SSH Socket Activation

Since Ubuntu 22.10, fresh installations use systemd socket activation to manage SSH connections by default. In this setup, changing the port in sshd_config alone may not update the listening port. You may also need to modify the SSH socket configuration in systemd.

First, check if the SSH socket is active or not:

BASH
systemctl status ssh.socket

If the socket is active, configure its listening port:

sudo systemctl edit ssh.socket

Add the following configuration at the end of the configuration files. You should add a new override, not modify the commented-out lines:

[Socket]
ListenStream=
ListenStream=2222

After adding the command above, add the end of the file, press Ctrl + X, press y when asked to save changes, and press Enter to confirm the filename. Then run :

sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

Connect using a custom SSH port

Now you can connect using the custom SSH port from your local terminal or using apps like PuTTY on Windows.

ssh username@server_ip -p 2222

Replace:

  • username with your Linux VPS username (for example, root).
  • server_ip with your VPS IP address.
  • 2222 with your newly configured SSH port.

How to Change the SSH Port on CentOS Stream and AlmaLinux

CentOS Stream and AlmaLinux use SELinux and firewalld to manage additional security restrictions. To change the SSH port on your CentOS VPS or any other RHEL-based Linux distro, you need to update the SSH configuration, allow the port in SELinux, configure your firewall, and restart the SSH services.

Change SSH Configuration File In CentOS

The process of changing the SSH port in the configuration file is the same on Ubuntu, CentOS Stream, and AlmaLinux. After logging in to your server, run the following command to open the SSH configuration file in Nano:

sudo nano /etc/ssh/sshd_config

Find the line #Port 22, remove the #, and replace 22 with your preferred port, such as 2222. Press Ctrl + X to exit Nano. When prompted to save the changes, press Y and then Enter to confirm the filename.

port 2222

Then restart the SSH service on your CentOS or AlmaLinux VPS to make sure that all things work correctly. Run the command below to restart it:

sudo systemctl restart sshd

Allow New SSH Port in SELinux

SELinux restricts which ports SSH is allowed to listen on. If SELinux is enabled, you must add your new port to its SSH security policy. First, check the SELinux status on your Linux:

getenforce

This command returns one of three possible results:

  • Enforcing: SELinux is enforcing its security policies. You must allow the new SSH port.
  • Permissive: SELinux monitors policy violations but does not block them. You don’t need to change the SSH port policy; it works correctly.
  • Disabled: SELinux is turned off, so you don’t need additional configuration.

If SELinux is running in enforcing mode, use the following command to allow your new SSH port. Replace 2222 with your chosen port:

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

Note:
If the port is already defined in SELinux under another port type, the -a command may return an error. In this case, you can use -m instead of -a to modify the existing port assignment. However, changing an existing SELinux port assignment may affect other services that rely on it. Before using -m, make sure you understand which service uses that port. Otherwise, choose a different port for SSH.

You can verify the allowed SSH port using:

sudo semanage port -l | grep ssh_port_t

Configure Firwalld to Allow SSH Custom Port

Next, you need to configure your VPS firewall to allow incoming connections through the new SSH port. By default, CentOS and AlmaLinux use firewalld as their firewall management tool.

First, check whether firewalld is running:

sudo systemctl status firewalld

If firewalld is running, use the following command to allow incoming connections through your new SSH port. Replace 2222 with the port you configured in SSH:

sudo firewall-cmd --permanent --add-port=2222/tcp

For more detailed instructions on opening ports and allowing incoming connections on a Linux VPS, including CentOS and AlmaLinux, check out our dedicated guide:

How to Open Port on Linux VPS?
READ How to Open Port on Linux VPS?

Then you need to reload the firewall rules to apply the --permanent firewall rule; otherwise, the new rule might not become active immediately. Use the command below to reload:

sudo firewall-cmd --reload

Connect to CentOS, AlmaLinux using Custom Port

After changing the SSH port and setting all things up, you can connect to your CentOS or AlmaLinux VPS using the new port.

If you’re using Linux on your local machine, open the terminal and use the -p option followed by your new SSH port. If you’re using PuTTY or Bitvise on Windows, simply enter the new port in the Port field before connecting.

ssh username@your-server-ip -p 2222

Replace username with your VPS username, your-server-ip with your server’s IP address, and 2222 with your new SSH port.

How to Improve SSH Server Security

Changing the default SSH port is one way to reduce unwanted connection attempts, but it shouldn’t be your only security measure. There are several additional steps you can take to protect your SSH server from unauthorized access.

Disable Root Login

By default, VPS providers give you access to your server through the root account, which has full administrative privileges. Since the root username is predictable, attackers can easily target it in brute-force attacks. Disable direct root login via SSH and create another user with sudo privileges to manage your server instead.

Use SSH Key Authentication

SSH keys provide a more secure alternative to password-based authentication. Instead of entering your account password every time you connect, you authenticate using a private key stored on your local machine and a matching public key installed on your VPS.

To set this up, first install OpenSSH Server. Then, use OpenSSH to generate an SSH key pair on your client side (Windows or Linux) and copy the public key to your VPS. Once you have confirmed that you can connect using SSH keys, you can disable password-based authentication to prevent attackers from trying to access your server with passwords.

Restrict SSH Access Using Firewall

You can also restrict SSH access to specific IP addresses or IP ranges, ensuring that connections from any other IP address are blocked from accessing your VPS.

Conclusion

To improve the security of your Linux VPS, you can change the default SSH port and use a custom port to connect to your server. This makes it harder for attackers to find your SSH service, but changing the SSH port alone does not make your server fully secure. You should also consider implementing other security measures to protect your VPS.

In this guide, we’ve walked you through the process of changing the SSH port and configuring your Linux servers to accept SSH connections through the new port on both Debian-based distributions (including Debian and Ubuntu) and RHEL-based distributions (including CentOS and AlmaLinux).

Frequently Asked Questions About Changing SSH Ports

Can I use any port for SSH?

You can configure SSH to use most available TCP ports between 1 and 65535. However, avoid ports already used by other services and consider using an unprivileged port above 1024, such as 2222 or 2200.

How do I restore SSH to the default port 22?

Open the SSH server configuration file, change the custom port to 22, and save changes and make sure the port 22 is allowed through your firewall and updaet ysstemd socket or SELinux configurations if applicable. Then restart the SSH services.

Does changing the SSH port affect existing SSH connections?

No. Changing the SSH port does not normally terminate existing SSH sessions. However, new connections must use the newly configured port, meaning that if you disconnect and try to reconnect again, you should use the new port.

Do I need to configure SELinux when changing the SSH port?

Only if SELinux is enabled and enforcing on your server. On CentOS Stream and AlmaLinux, you must allow the new SSH port in the SELinux policy before SSH can listen on it.

Share:

Was this article helpful?

Your feedback helps us improve our technical guides.

Related Posts

You May Also Like

Explore more tutorials, guides, or documentation, from VPSMakers.

Learn how to easily update MikroTik router firmware manually using winbox and CLI. Stay up to date with the latest updates!
Jacob Rafet
November 24, 2026
default RDP port is (3389) and to increase the security of all Windows Server 2012, 2016, 2019 and 2022, you can change it with registry editor and PowerShell.
Robert Smith
November 23, 2026
Learn how to change the default MikroTik WinBox port using Terminal or WinBox and allow connections through the new port in the firewall.
Jacob Rafet
November 22, 2026
Learn how to extract regular, multi-part, and password-protected RAR files on Ubuntu VPS using command-line and graphical tools.
Richard Mourar
November 21, 2026