# Changing the SSH server port number

> Change the port your SSH server listens on for a VPS or dedicated server, and connect on the new port safely.

Source: https://helpcenter.ruachost.com/getting-started/your-account/changing-the-ssh-server-port-number/

By default, SSH servers listen on **port 22**. For security reasons, or when required by third-party services, you may want to change this port number. This guide explains how to safely update the SSH port on your Server.

> **Note:**
**Important Notes**\
These steps apply only to VPS and dedicated servers with root access.

Shared hosting accounts do not have root access and cannot change the SSH port.

Always test the new port in a separate session before closing your root session, to avoid locking yourself out.

If you use a firewall, you must update its rules to allow the new port.

## Steps to Change the SSH Port

### Step 1: Log in as Root

Connect to your server using SSH on the current port (default is 22):

```
ssh root@yourdomain.com -p 22
```

### Step 2: Edit the SSH Configuration File

1. Open the SSH configuration file in a text editor:

   ```
   nano /etc/ssh/sshd_config
   ```
2. Locate the line:

   ```
   Port 22
   ```
3. Change `22` to your desired port number (e.g., `7822`).

   ```
   Port 7822
   ```
4. Save and exit the file.

### Step 3: Update Firewall Rules

If your server has a firewall enabled, you must allow the new port:

- **iptables** example:

  ```bash
  iptables -A INPUT -p tcp --dport 7822 -j ACCEPT
  service iptables save
  ```
- **firewalld** example (CentOS/AlmaLinux):

  ```bash
  firewall-cmd --permanent --add-port=7822/tcp
  firewall-cmd --reload
  ```
- **ufw** example (Ubuntu/Debian):

  ```bash
  ufw allow 7822/tcp
  ufw reload
  ```

### Step 4: Restart the SSH Service

Restart SSH to apply the changes:

- On AlmaLinux/Fedora:

  ```bash
  service sshd restart
  ```
- On Debian/Ubuntu:

  ```bash
  service ssh restart
  ```

### Step 5: Test the New Port

Open a **new terminal window** and try connecting with the new port:

```bash
ssh root@yourdomain.com -p 7822
```

Do not close your original session until you confirm the new connection works.
