> ## 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

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.

<Steps>
  <Step title="Open the SSH configuration file">
    ```bash theme={null}
    sudo nano /etc/ssh/sshd_config
    ```
  </Step>

  <Step title="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.

    <Frame>
      <img src="https://mintcdn.com/gcore/ftp_39AiEBxr8OPE/images/docs/hosting/virtual-servers/manage/connect/linux-server/change-the-port-for-ssh-connections/1-port-22.png?fit=max&auto=format&n=ftp_39AiEBxr8OPE&q=85&s=bf2620200c774c28ee8ff4f83135acce" alt="sshd_config with Port 22 commented out" width="823" height="519" data-path="images/docs/hosting/virtual-servers/manage/connect/linux-server/change-the-port-for-ssh-connections/1-port-22.png" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/gcore/ftp_39AiEBxr8OPE/images/docs/hosting/virtual-servers/manage/connect/linux-server/change-the-port-for-ssh-connections/2-port-2233.png?fit=max&auto=format&n=ftp_39AiEBxr8OPE&q=85&s=70c0eb6f0bc4d608c0047841f695ed56" alt="sshd_config with Port 2233" width="765" height="426" data-path="images/docs/hosting/virtual-servers/manage/connect/linux-server/change-the-port-for-ssh-connections/2-port-2233.png" />
    </Frame>
  </Step>

  <Step title="Save and close the file">
    Press **Ctrl+O**, then **Enter** to confirm the filename, and **Ctrl+X** to exit nano.
  </Step>

  <Step title="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
    ```
  </Step>

  <Step title="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.
  </Step>

  <Step title="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`.

    <Frame>
      <img src="https://mintcdn.com/gcore/ftp_39AiEBxr8OPE/images/docs/hosting/virtual-servers/manage/connect/linux-server/change-the-port-for-ssh-connections/3-output.png?fit=max&auto=format&n=ftp_39AiEBxr8OPE&q=85&s=bf66ead8760c9c5a0eda2f9cb7ac9b3a" alt="Telnet output showing SSH banner on port 2233" width="590" height="99" data-path="images/docs/hosting/virtual-servers/manage/connect/linux-server/change-the-port-for-ssh-connections/3-output.png" />
    </Frame>
  </Step>

  <Step title="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.
    </Warning>
  </Step>
</Steps>
