How to Configure Linux VPS Firewall Rules Safely

Learn how to configure Linux VPS firewall rules without losing SSH access. Secure game servers, bots, apps, and admin ports with practical, fast setup steps.

A new VPS can be online in minutes, but every open port becomes part of its attack surface. When you configure Linux VPS firewall rules correctly, your bot, game server, website, or API stays reachable while unused services stay invisible to random scans.

The goal is not to block everything blindly. It is to allow only the traffic your project needs, preserve SSH access, and make future changes without locking yourself out. For most Linux VPS deployments, UFW is the fastest route to a clean baseline. Firewalld is also a strong option on distributions such as Rocky Linux, AlmaLinux, and Fedora.

ACLClouds
ACLClouds

How to Configure Linux VPS Firewall Rules Safely

Configure Linux VPS firewall rules with UFW or firewalld without losing SSH: open only the ports you need, limit SSH and test the rules from outside.

A new VPS can be online in minutes, but every open port becomes part of its attack surface. When you configure Linux VPS firewall rules correctly, your bot, game server, website, or API stays reachable while unused services stay invisible to random scans.

The goal is not to block everything blindly. It is to allow only the traffic your project needs, preserve SSH access, and make future changes without locking yourself out. For most Linux VPS deployments, UFW is the fastest route to a clean baseline. Firewalld is also a strong option on distributions such as Rocky Linux, AlmaLinux, and Fedora.

Start With the Ports Your VPS Actually Needs

Before changing a rule, identify what is listening on the server. Run this command as root or with `sudo`:

```bash ss -tulpn ```

Look for services bound to public interfaces. A basic server may only need SSH on port 22. A web app may require ports 80 and 443. A Discord bot normally makes outbound connections and often does not need any inbound port at all unless it provides a dashboard, webhook endpoint, or API.

Game servers need more attention because many use specific TCP and UDP ports. A Minecraft Java server commonly uses TCP port 25565. FiveM, Rust, ARK, Valheim, and other multiplayer servers have their own port ranges and may require both protocols. Check the software documentation and the port settings in your server configuration rather than opening a broad range "just in case."

You can also confirm a service is reachable locally before exposing it publicly:

```bash curl http://127.0.0.1:PORT ```

If it does not respond locally, opening the firewall will not fix the application. Diagnose the service first.

Configure Linux VPS Firewall Rules With UFW

UFW, short for Uncomplicated Firewall, is a practical choice for Ubuntu and Debian VPS instances. First, install it if it is not already available:

```bash sudo apt update sudo apt install ufw ```

The single most important rule comes before enabling the firewall: allow your current SSH port. If your server uses the default SSH port, run:

```bash sudo ufw allow 22/tcp ```

If SSH was moved to another port, replace `22` with that value. You can check the configured port in `/etc/ssh/sshd_config` or inspect your current connection details. Do not assume port 22 if you changed it during initial setup.

Next, set safe default policies. Deny unsolicited incoming traffic and allow outbound traffic, which lets your server download updates, connect to databases, send bot requests, and use external APIs.

```bash sudo ufw default deny incoming sudo ufw default allow outgoing ```

Now allow only the services you intend to publish. For a typical HTTPS website:

```bash sudo ufw allow 80/tcp sudo ufw allow 443/tcp ```

For a Minecraft Java server:

```bash sudo ufw allow 25565/tcp ```

For a service that requires UDP, specify it explicitly:

```bash sudo ufw allow 2456:2458/udp ```

Enable the firewall only after checking that the SSH rule is present:

```bash sudo ufw status numbered sudo ufw enable ```

Keep your existing SSH session open. Then open a second terminal and test a new SSH login. This small habit prevents a bad rule from becoming a support emergency.

Limit SSH Instead of Leaving It Open to Everyone

Allowing SSH from anywhere is workable for a personal VPS, especially when you use SSH keys, disable password login, and keep the system updated. It is still exposed to constant automated login attempts.

If you always connect from a fixed home or office IP address, restrict SSH to that address:

```bash sudo ufw delete allow 22/tcp sudo ufw allow from 203.0.113.10 to any port 22 proto tcp ```

Replace the example IP with your own public IP. This is highly effective, but it has a trade-off: changing networks, using mobile data, or traveling can prevent you from logging in. For users with dynamic IP addresses, SSH keys and a tool such as Fail2ban are often more practical than IP allowlisting.

You can also rate-limit connection attempts on the SSH port:

```bash sudo ufw limit 22/tcp ```

Rate limiting helps reduce brute-force noise, but it is not a replacement for key-based authentication. Disable password authentication when your keys are tested and working.

Open Game and App Ports Precisely

A firewall rule should describe the workload, not the whole server. Avoid rules such as allowing every port from 1 to 65535. They make setup easy for five minutes and create an unnecessary exposure problem for the life of the VPS.

For example, a community running a Minecraft server with a web map may need TCP 25565 for the game and TCP 80/443 for the map or reverse proxy. It does not need to expose MySQL port 3306 publicly if the database is used only by local plugins. Keep the database bound to `127.0.0.1` where possible, then deny public access at the firewall as an additional layer.

The same rule applies to Redis, MongoDB, admin panels, Docker management interfaces, and development ports. Bind them privately or place them behind authentication and a reverse proxy. A firewall is not the only security control, but it provides a reliable first boundary.

When Docker is involved, test carefully. Docker can add its own iptables rules, and published container ports may bypass assumptions made in a simple host firewall setup. Verify access from outside the VPS after every deployment. If a container should be internal-only, do not publish its port to all interfaces.

Use Firewalld on RHEL-Based Linux VPS Plans

If your server runs Rocky Linux, AlmaLinux, CentOS Stream, or Fedora, Firewalld is usually the native firewall management tool. Start and enable the service:

```bash sudo systemctl enable --now firewalld ```

Allow SSH and web traffic permanently:

```bash sudo firewall-cmd --permanent --add-service=ssh sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload ```

For a custom game or application port, add the protocol and port directly:

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

Check the active configuration with:

```bash sudo firewall-cmd --list-all ```

UFW and Firewalld both manage underlying packet-filtering rules. Use one management approach consistently. Mixing UFW commands, raw iptables commands, and Firewalld settings without understanding their interaction can make troubleshooting much harder.

Verify Rules From Outside the VPS

A firewall configuration is only complete once it has been tested from a separate network. Your local SSH connection proves that one session is alive. It does not prove that the intended ports are open or that unwanted ports are blocked.

Test the public service from your browser, game client, or another VPS. For web services, confirm that HTTP redirects correctly to HTTPS if that is your setup. For game servers, test an actual player connection. For bots, confirm webhooks and dashboards work, while confirming no unused management port is exposed.

Check the firewall status after changes:

```bash sudo ufw status verbose ```

or:

```bash sudo firewall-cmd --list-all ```

If a service still cannot be reached, inspect three areas in order: the application is listening on the expected interface, the Linux firewall allows the correct protocol and port, and any provider-side network firewall or security group permits the traffic. DDoS protection helps absorb malicious traffic, but it does not replace correct port rules or application hardening.

Keep the Firewall Maintainable

A clean firewall is easier to operate during an outage. Add rules when you deploy a service, remove them when you retire it, and record why each public port exists. Numbered UFW rules make cleanup simple:

```bash sudo ufw status numbered sudo ufw delete RULE_NUMBER ```

Review firewall rules after installing a game panel, database, monitoring agent, or Docker stack. New software often creates listeners that were not part of the original deployment plan.

ACLClouds VPS plans provide full administrator access, so you control the operating system, services, and security model from day one. Use that control deliberately: allow the ports your players and users need, keep admin services private, and test every change from a second connection before calling the job done.