Check, enable, configure, and disable the Linux OS firewall without locking yourself out.
How it works
A security group filters all traffic to and from an ECS instance first, then the OS firewall processes it. Traffic must pass both to reach the application.
-
Security group (cloud network layer)
-
Function: A security group acts as a virtual firewall that controls inbound and outbound traffic outside the OS — the first line of defense.
-
Logic: If a security group denies traffic on a port (such as port 22 for SSH), all external requests to that port are blocked, regardless of the OS firewall settings.
-
-
System firewall (instance layer)
-
Function: Firewall software such as
firewalldorufwruns inside the Linux OS and is disabled by default. It provides granular control over traffic already allowed by the security group. -
Logic: Traffic must pass the security group before reaching the OS firewall. The two form an AND relationship — your application receives traffic only if both allow it.
-
Procedure
Check firewall status
Check the current firewall status before making changes.
Alibaba Cloud Linux, CentOS, or Red Hat
Run the following command to check firewalld status.
sudo firewall-cmd --state
-
not running: The firewall is disabled. -
running: The firewall is enabled. -
If the output is
command not found,firewalldis not installed. Install it by runningsudo yum install firewalld -yorsudo dnf install firewalld -y.
Ubuntu or Debian
Run the following command to check ufw status.
sudo ufw status
-
Status: active: The firewall is enabled. -
Status: inactive: The firewall is disabled. -
On Debian, if the output is
command not found,ufwis not installed. Runsudo apt update && sudo apt install ufw -yto install it.
Enable the firewall
Never enable the firewall before you add rules to allow necessary traffic. Doing so may lock you out of the instance. Always allow first, then enable.
Alibaba Cloud Linux, CentOS, or Red Hat
-
Permanently allow SSH to keep your remote session open.
sudo firewall-cmd --permanent --add-service=ssh -
Reload the firewall to apply the new rules.
sudo firewall-cmd --reload -
Start the firewall service.
sudo systemctl start firewalld -
(Optional): Enable the firewall to start on boot.
sudo systemctl enable firewalld
Ubuntu or Debian
-
Allow SSH connections.
sudo ufw allow ssh -
Enable the firewall.
ufwautomatically loads existingallowrules and starts on boot by default.sudo ufw enableThe system warns that the operation may disrupt existing connections. Enter
yto confirm. SSH is already allowed, so your session stays connected.
Open a specific port or service
Alibaba Cloud Linux, CentOS, or Red Hat
-
Open a specific port or service:
-
By service name (recommended):
# Permanently allow the HTTP and HTTPS services sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https -
By port number:
# Permanently allow port 8080/TCP sudo firewall-cmd --permanent --add-port=8080/tcp
-
-
Apply the new rules: Reload the firewall after you add or remove rules.
sudo firewall-cmd --reload -
Verify that the rules are in effect:
Lists all active rules in the current zone, including services, ports, and protocols.
sudo firewall-cmd --list-all
Ubuntu or Debian
With ufw, rules take effect immediately and persist across reboots.
-
Open a specific port or service:
-
By service name (recommended):
# Allow the HTTP and HTTPS services sudo ufw allow http sudo ufw allow https -
By port number:
# Allow port 3306/TCP sudo ufw allow 3306/tcp
-
-
Verify that the rules are in effect:
Run the following command to view all active rules.
sudo ufw status
Disable the firewall
Temporarily disable the firewall to determine whether a blocking policy causes a connectivity issue.
Do not disable the firewall in production. Re-enable it after testing or resolving the issue.
CentOS, Red Hat, or Alibaba Cloud Linux
sudo systemctl stop firewalld
Ubuntu or Debian
sudo ufw disable
Apply in production
-
Best practices
-
Principle of least privilege: Only open ports essential for your application. For core services such as databases, allow access only from trusted internal IP addresses and avoid exposure to the Internet.
-
Maintain a backup connection: Before changing firewall rules, establish a remote connection to the instance. An existing session persists after rule changes and serves as a backup channel.
-
-
Risk prevention
-
Logging and monitoring: Audit firewall logs regularly to detect unusual access attempts.
-
firewalld: Logs are managed byjournald. View them withsudo journalctl -u firewalld. -
ufw: Logs are in/var/log/ufw.log.
-
-
FAQ
-
Service still inaccessible after disabling the OS firewall
Check the following in order:
-
Check the security group: Log on to the ECS console - Security Groups page. Confirm that the inbound rules allow traffic on the required port from the appropriate source IPs.
-
Check the service listening status: Confirm that the application listens on the expected address (such as
0.0.0.0) and port. Runss -tunlp | grep <port_number>ornetstat -tunlp | grep <port_number>on the instance. -
Check the network ACL: If your instance is associated with a network ACL, verify that its rules permit the relevant traffic.
-
-
Resolve a
command not founderrorThe firewall management tool is not installed. Install it:
-
On CentOS/Red Hat/Alibaba Cloud Linux, run
sudo yum install firewalld -yorsudo dnf install firewalld -y. -
On Ubuntu/Debian, run
sudo apt update && sudo apt install ufw -y.
-