Skip to content

Firewall Configuration: ufw, firewalld, nftables, Windows

In this article

Before you begin

The Invapi control panel has no firewall settings or network security rules. The firewall is configured inside the server's operating system.

Attention

The golden rule: allow the remote access port first (SSH — 22/tcp or your custom port, RDP — 3389/tcp), and only then enable the firewall or change the default policy to deny. Before you start, make sure you have access to the server control console — you can use it to fix a mistake if you lose network access.

Use only one rule management tool on the server. Running ufw, firewalld, and your own iptables rules at the same time leads to conflicts.

Some ports are blocked at the HOSTKEY network level regardless of the firewall settings on the server. The list is available in Which network ports are blocked, besides SMTP?

ufw (Ubuntu and Debian)

Installation (not installed by default on Debian):

sudo apt install ufw

Basic setup:

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp          # SSH — required before enabling
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

Useful commands:

sudo ufw status verbose                 # current rules
sudo ufw status numbered                # rules with numbers
sudo ufw delete 3                       # delete rule #3
sudo ufw allow from 203.0.113.10 to any port 3306 proto tcp   # MySQL access from a single IP
sudo ufw deny from 198.51.100.0/24      # deny a subnet
sudo ufw disable                        # disable the firewall

Note

Docker adds its own iptables rules and publishes container ports bypassing ufw. Keep this in mind when configuring servers that run Docker.

firewalld (AlmaLinux, Rocky Linux, CentOS)

firewalld is used by default on AlmaLinux, Rocky Linux, CentOS 7, and CentOS 8. The commands are the same in all versions.

Installation and startup, if the service is not active:

# AlmaLinux/Rocky/CentOS 8
sudo dnf install firewalld
# CentOS 7
sudo yum install firewalld

sudo systemctl enable --now firewalld

Main commands:

sudo firewall-cmd --state                          # status
sudo firewall-cmd --get-active-zones               # active zones
sudo firewall-cmd --list-all                       # rules of the default zone

sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --permanent --remove-port=8080/tcp
sudo firewall-cmd --reload                         # apply permanent rules

Allow a port from a single IP address only (rich rule):

sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="3306" protocol="tcp" accept'
sudo firewall-cmd --reload

Tip

Rules added without --permanent stay in effect until a reboot or --reload. This is handy for testing: if a rule blocks your access, simply reboot the server.

nftables

nftables is the modern replacement for iptables on Debian 10+, Ubuntu 20.04+, and RHEL-compatible systems 8+. The classic tool is described in Setting iptables basic Linux firewall.

An example of a basic ruleset in /etc/nftables.conf (on AlmaLinux/Rocky Linux — /etc/sysconfig/nftables.conf):

#!/usr/sbin/nft -f
flush ruleset

table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;

        iif "lo" accept
        ct state established,related accept
        ct state invalid drop
        ip protocol icmp accept
        ip6 nexthdr icmpv6 accept

        tcp dport 22 accept
        tcp dport { 80, 443 } accept
    }
    chain forward {
        type filter hook forward priority 0; policy drop;
    }
    chain output {
        type filter hook output priority 0; policy accept;
    }
}

Syntax check, applying, and enabling at startup:

sudo nft -c -f /etc/nftables.conf     # check only
sudo nft -f /etc/nftables.conf        # apply
sudo systemctl enable nftables
sudo nft list ruleset                 # current rules

Windows Firewall

Open PowerShell as administrator.

View the firewall state and RDP rules:

Get-NetFirewallProfile | Format-Table Name, Enabled
Get-NetFirewallRule -DisplayGroup "Remote Desktop" | Format-Table DisplayName, Enabled, Direction, Action

Allow RDP and a custom port:

Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
New-NetFirewallRule -DisplayName "Allow HTTPS" -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow

Note

The rule group name depends on the system language: on localized versions of Windows, use the translated group name in -DisplayGroup.

Restrict RDP access to your IP addresses only:

New-NetFirewallRule -DisplayName "RDP from trusted IP" -Direction Inbound -Protocol TCP -LocalPort 3389 -RemoteAddress 203.0.113.10,198.51.100.0/24 -Action Allow
Disable-NetFirewallRule -DisplayGroup "Remote Desktop"

First create the allow rule for your addresses and test the connection in a new RDP window, and only then disable the default rules. If RDP runs on a non-standard port, specify it in -LocalPort.

Allow replies to ping (ICMP Echo):

New-NetFirewallRule -DisplayName "Allow ICMPv4 Echo" -Protocol ICMPv4 -IcmpType 8 -Direction Inbound -Action Allow

If you have lost access to the server

  1. Open the server control console in Invapi and log in.
  2. Disable the firewall or add an allow rule:

    sudo ufw disable                                  # ufw
    sudo systemctl stop firewalld                     # firewalld
    sudo nft flush ruleset                            # nftables
    sudo iptables -P INPUT ACCEPT && sudo iptables -F # iptables
    

    On Windows:

    Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False
    
  3. Connect over SSH or RDP, fix the rules, and enable the firewall again.

Attention

Do not leave the server without a firewall for long — set up the rules again as soon as access is restored.

If the console is unavailable or you cannot log in, use Rescue mode or contact support.

You can contact technical support in two ways: by email from the address specified in your account, or through the Help Center in the Invapi control panel by creating a ticket (how to create a ticket).

question_mark
Is there anything I can help you with?
question_mark
AI Assistant ×