> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gcore.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Change the port for SSH connections

> Change the default SSH port from 22 to a custom number and update firewall rules to allow the new connection.

The SSH daemon listens on port 22 by default. [Log in to the server via SSH](/hosting/virtual-servers/manage/connect/linux-server/connect-to-linux-server-via-ssh) and follow these steps to change it.

**1. Open the SSH configuration file**

```bash theme={null}
sudo nano /etc/ssh/sshd_config
```

**2. Update the Port line**

Find the **Port** line and replace the value `22` with the new port number. If the line begins with `#`, remove the `#` to uncomment it first.

![sshd\_config with Port 22 commented out](https://mintcdn.com/gcore/494jjlOXZiI1ohtY/images/docs/hosting/virtual-servers/manage/connect/linux-server/change-the-port-for-ssh-connections/1-port-22.png?fit=max\&auto=format\&n=494jjlOXZiI1ohtY\&q=85\&s=3a8b74c64a02b85b043dcdc6f44235cf)

![sshd\_config with Port 2233](https://mintcdn.com/gcore/494jjlOXZiI1ohtY/images/docs/hosting/virtual-servers/manage/connect/linux-server/change-the-port-for-ssh-connections/2-port-2233.png?fit=max\&auto=format\&n=494jjlOXZiI1ohtY\&q=85\&s=336248cf8bef267269fb202ae46ac5c4)

**3. Save and close the file**

Press **Ctrl+O**, then **Enter** to confirm the filename, and **Ctrl+X** to exit nano.

**4. Restart the SSH service**

On Ubuntu and Debian:

```bash theme={null}
sudo systemctl restart ssh
```

On CentOS, AlmaLinux, and Rocky Linux:

```bash theme={null}
sudo systemctl restart sshd
```

On FreeBSD:

```bash theme={null}
sudo service sshd restart
```

**5. Allow traffic on the new port**

On Ubuntu and Debian (ufw):

```bash theme={null}
sudo ufw allow 2233/tcp
```

On CentOS, AlmaLinux, and Rocky Linux (firewalld):

```bash theme={null}
sudo firewall-cmd --permanent --add-port=2233/tcp && sudo firewall-cmd --reload
```

Replace `2233` with the chosen port number.

**6. Verify the port is reachable**

From a local machine (not from the server), run:

```bash theme={null}
telnet <server_ip> 2233
```

Replace `<server_ip>` with the server IP address and `2233` with the chosen port. A successful response includes the SSH banner line starting with `SSH-2.0`.

![Telnet output showing SSH banner on port 2233](https://mintcdn.com/gcore/494jjlOXZiI1ohtY/images/docs/hosting/virtual-servers/manage/connect/linux-server/change-the-port-for-ssh-connections/3-output.png?fit=max\&auto=format\&n=494jjlOXZiI1ohtY\&q=85\&s=aeeeec125b7c2c42e9b870d833219c38)

**7. Test the SSH connection**

> [!WARNING]
>
> Keep the current SSH session open. Open a separate terminal window and connect using the new port:
>
> ```bash theme={null}
> ssh -p 2233 username@<server_ip>
> ```
>
> Replace `username` with the server username (typically `root`) and `<server_ip>` with the server IP address. If the connection fails, revert the **Port** value in `/etc/ssh/sshd_config` back to `22` and restart the SSH service again.
