Enable or disable the system firewall in a Linux instance

更新时间:
复制 MD 格式

The system firewall controls which network traffic reaches your Simple Application Server. Before enabling the firewall, make sure SSH access is explicitly allowed — otherwise you will be locked out of your server.

Prerequisites

Before you begin, ensure that you have:

  • A Simple Application Server running Linux

  • Root or sudo access to the server

CentOS 7, Red Hat 7, and Alibaba Cloud Linux 2

These distributions use firewalld, managed through systemctl and firewall-cmd.

Enable the firewall:

systemctl start firewalld.service

Disable the firewall:

systemctl stop firewalld.service

Check the firewall status:

firewall-cmd --state

Expected output when the firewall is running:

running

Expected output when the firewall is stopped:

not running

Ubuntu

Ubuntu uses ufw (Uncomplicated Firewall).

Warning

Before enabling ufw, allow SSH traffic to avoid being locked out of your server:

ufw allow ssh

Enable the firewall:

ufw enable

When prompted, type y to confirm. Disabling the firewall with ufw disable preserves all configured rules — they take effect again when you re-enable ufw.

Disable the firewall:

ufw disable

Check the firewall status:

ufw status

Expected output when active:

Status: active

Expected output when inactive:

Status: inactive

Debian

By default, no firewall is installed on Debian. If a firewall has been installed, you can remove all firewall rules to stop traffic blocking.

Important

Back up your firewall rules before deleting them.

  1. Back up the current firewall rules. Replace <backup-file-path> with the path where you want to save the backup file (for example, /tmp/iptables-backup.txt):

    touch <backup-file-path>
    iptables-save > <backup-file-path>
  2. Delete all firewall rules:

    iptables -F

    iptables -F flushes all rules from the active rule chains. The iptables module itself remains loaded, so this is equivalent to disabling traffic filtering — not uninstalling the firewall.