---
title: "Change the SSH port without locking yourself out · CloudSat"
description: "Move SSH to a new port on Ubuntu, Debian or RHEL-family servers: open the firewall first, handle socket activation and SELinux, then retire port 22."
url: https://cloudsat.io/learn/change-ssh-port-safely
updated: 2026-11-18T12:00:00Z
source: CloudSat (https://cloudsat.io/). Prices in USD.
---

[Security](https://cloudsat.io/learn/topic/server-security)

# Change the SSH port without locking yourself out

Move SSH to a new port on Ubuntu, Debian or RHEL-family servers: open the firewall first, handle socket activation and SELinux, then retire port 22.

CloudSat team November 18, 2026 7 min read

Moving SSH off port 22 takes five minutes. Getting the order wrong can cost you the server. The method below never leaves you without a working door: the new port is opened in every firewall first, sshd listens on both ports while you test, and port 22 closes only after a fresh login on the new port succeeds.

It also covers the two traps that catch experienced admins in 2026: Ubuntu's socket-activated SSH, which ignores a plain service restart, and SELinux on RHEL-family systems, which refuses to let sshd bind an unlabeled port.

## What a new port does, and what it does not

A non-standard port cuts the noise. Most automated login attempts only knock on port 22, so your auth log gets quieter and tools like fail2ban have less to chew on. It does not stop anyone who scans all 65,535 ports, and it does nothing against a stolen key or a weak password.

| Measure | What it actually stops |
| --- | --- |
| New SSH port | Drive-by bots that only try port 22 |
| Key-only login (`PasswordAuthentication no`) | Password guessing, at any port |
| Firewall source rules | Everyone outside your address ranges |
| fail2ban or CrowdSec | Repeated failures from one address |

If you only have time for one change, disable password logins. Change the port as a second layer, not a first.

Make sure your key works first, then add a drop-in file such as `/etc/ssh/sshd_config.d/10-hardening.conf`. sshd keeps the first value it reads for most options, and Ubuntu cloud images ship `50-cloud-init.conf`, so a name that sorts earlier wins:

```text
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
```

Check the syntax with `sudo sshd -t`, reload with `sudo systemctl reload ssh` (the unit is `sshd` on RHEL-family systems), and confirm a fresh key-based login works before you close your session.

## Pick a port

Choose a number that nothing else on the server uses, and keep it out of the Linux ephemeral range (32768 to 60999 by default), which the kernel hands out for outgoing connections. Ports below 1024 can only be bound by root, which some admins like: an unprivileged process cannot impersonate sshd there if the daemon dies. This guide uses 2222.

```bash
sudo ss -tlnp | grep ':2222 '    # anything listening there already?
grep -w 2222 /etc/services       # registered to a well-known service?
```

## The order that keeps you connected

1. Keep your current SSH session open for the whole procedure. It is your way back.
2. Allow the new port in every firewall that sits in front of sshd.
3. Make sshd listen on 22 and 2222 at the same time.
4. Apply the change the way your distribution expects.
5. Log in on 2222 from a second terminal.
6. Only then remove port 22 from sshd and from the firewalls.

## Step 1: open the new port everywhere

A port has to be open in each layer. On a CloudSat server that means the [cloud firewall](https://cloudsat.io/docs/cloud-firewalls) attached in the console plus whatever runs inside the operating system. If your public address is fixed, limit the new rule to that address instead of the whole internet: it is a far stronger control than the port number itself.

On Ubuntu and Debian with UFW:

```bash
sudo ufw allow 2222/tcp comment 'SSH new port'
```

On RHEL, AlmaLinux, Rocky Linux and Fedora with firewalld:

```bash
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --reload
```

If you manage nftables directly, add `2222` to the set of accepted TCP ports in `/etc/nftables.conf` and load it with `sudo nft -f /etc/nftables.conf`.

## Step 2: make sshd listen on both ports

`Port` is one of the few sshd options that may appear several times, and sshd listens on every value. Edit `/etc/ssh/sshd_config`, replace the commented `#Port 22` line with two lines, and check that no drop-in file sets a port of its own:

```bash
sudo grep -rni '^port' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/
```

```text
Port 22
Port 2222
```

Validate the file before applying anything. No output means the syntax is fine.

```bash
sudo sshd -t
```

On RHEL-family systems with SELinux enforcing, label the port for SSH, or sshd will fail to bind it. The `semanage` tool ships in the `policycoreutils-python-utils` package.

```bash
sudo semanage port -a -t ssh_port_t -p tcp 2222
sudo semanage port -l | grep ssh_port_t
```

## Step 3: apply the change for your distribution

How you restart depends on what actually owns the listening socket.

| Distribution | What listens | Apply the change |
| --- | --- | --- |
| Ubuntu 24.04 and 26.04 | `ssh.socket` (systemd socket activation) | `sudo systemctl daemon-reload` then `sudo systemctl restart ssh.socket` |
| Ubuntu 22.04, Debian 12 and 13 | `ssh.service` | `sudo systemctl restart ssh` |
| RHEL 9 and 10, AlmaLinux, Rocky Linux, Fedora | `sshd.service` | `sudo systemctl restart sshd` |

The Ubuntu row is the one that surprises people. On recent releases systemd holds port 22 open and starts sshd on demand. A generator translates the `Port` lines in `sshd_config` into socket settings, but only when systemd reloads. Restart `ssh.service` alone and the server keeps listening on 22, exactly as before. Check which model you have with `systemctl is-active ssh.socket`.

Restarting does not drop your open session: each login runs in its own process. Confirm both ports are listening before you go further:

```bash
sudo ss -tlnp | grep -E ':(22|2222)\s'
```

On socket-activated Ubuntu the owner column shows `systemd` as well as `sshd`. That is expected.

## Step 4: test from a second terminal

Leave the first session alone and open a new terminal on your own computer:

```bash
ssh -p 2222 admin@203.0.113.10
```

A timeout usually means a firewall is still closed. "Connection refused" means the packet arrived but nothing listens on that port, so go back to step 3. Add `-v` to see where the handshake stops.

Once it works, save the port in your client configuration so you never type it again:

```text
Host web-01
    HostName 203.0.113.10
    Port 2222
    User admin
```

Other tools need the port too. `scp` takes a capital `-P 2222`, `rsync` takes `-e "ssh -p 2222"`, Git accepts `ssh://git@203.0.113.10:2222/srv/git/app.git`, and Ansible reads `ansible_port=2222` from the inventory.

## Step 5: retire port 22

Remove the `Port 22` line, run `sudo sshd -t`, and apply the change again with the command from step 3. Then close the old port in each firewall:

```bash
sudo ufw status numbered           # find the old rule: OpenSSH or 22/tcp
sudo ufw delete allow OpenSSH      # or: sudo ufw delete allow 22/tcp
```

With firewalld, remove the predefined service:

```bash
sudo firewall-cmd --permanent --remove-service=ssh
sudo firewall-cmd --reload
```

Delete the TCP 22 rule from the cloud firewall as well. If you run fail2ban, tell the jail about the new port in `/etc/fail2ban/jail.local`, or it will keep banning on 22:

```ini
[sshd]
enabled = true
port = 2222
```

Restart it with `sudo systemctl restart fail2ban` and check the jail with `sudo fail2ban-client status sshd`.

## If you do get locked out

Your original session is the first rescue: fix the config from there. If it is gone, use the provider's out-of-band console. On CloudSat you can request console access from the server page, as described in [Regain access with the recovery tools](https://cloudsat.io/docs/recovery-tools), and repair `sshd_config` or the firewall without SSH at all.

## FAQ

### Does changing the SSH port make a server secure?

No single setting does. A new port reduces automated noise, which makes real attacks easier to spot in the logs. Key-only authentication, firewall source rules and prompt updates carry the actual weight.

### Why is my Ubuntu server still listening on port 22?

It almost certainly uses socket activation. Run `sudo systemctl daemon-reload` and `sudo systemctl restart ssh.socket` after editing `sshd_config`. Restarting only `ssh.service` leaves the old socket in place.

### Can I just use a port above 60000?

You can, but numbers between 32768 and 60999 overlap the default range the kernel uses for outgoing connections. Pick something outside it to avoid an occasional bind failure after a reboot.

### Do I need to update monitoring or backup jobs?

Yes. Anything that connects over SSH, such as backup scripts, deployment pipelines or uptime checks, needs the new port. Search your automation for `:22` and `-p 22` before you close the old port.

**Run it on CloudSat.** Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. [See plans](https://cloudsat.io/plans) or [deploy a server](https://dash.cloudsat.io/console/deploy).

## Keep reading

[All guides in Security](https://cloudsat.io/learn/topic/server-security)

- [Check open and listening ports on Linux with ss and lsof](https://cloudsat.io/learn/list-listening-ports-linux): Networking Check open and listening ports on Linux with ss and lsof 6 min read
- [UFW, firewalld or nftables: choosing a Linux server firewall](https://cloudsat.io/learn/ufw-firewalld-nftables-compared): Security UFW, firewalld or nftables: choosing a Linux server firewall 7 min read
- [Hardware vs software firewalls, and where cloud firewalls fit](https://cloudsat.io/learn/hardware-software-cloud-firewalls): Security Hardware vs software firewalls, and where cloud firewalls fit 6 min read
- [UFW commands in practice: rule order, profiles and logs](https://cloudsat.io/learn/ufw-commands-rule-order-profiles-logs): Security UFW commands in practice: rule order, profiles and logs 8 min read
