Operations 6 min read

How to Configure iptables Firewall on CentOS 7 (Avoid Common Pitfalls)

This article walks through disabling firewalld on a CentOS 7 Alibaba Cloud server, installing iptables‑services, adding a complete set of firewall rules, explaining each rule’s purpose, and finally enabling the iptables service to ensure a secure LAMP environment.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Configure iptables Firewall on CentOS 7 (Avoid Common Pitfalls)

Recently the author set up a new server on Alibaba Cloud using CentOS 7 and installed a LAMP stack. While configuring the firewall, they discovered that the default firewalld service does not behave as expected, so they switched to the traditional iptables.

First, stop and disable firewalld:

# systemctl stop firewalld.service
# systemctl disable firewalld.service

Then install iptables‑services, edit /etc/sysconfig/iptables and add the required rules:

yum install iptables-services
vi /etc/sysconfig/iptables
# (example rules below)
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

After saving, restart and enable iptables:

systemctl restart iptables.service
systemctl enable iptables.service

The default policy sets INPUT and FORWARD chains to DROP and OUTPUT to ACCEPT. The 15 rules explained include opening web ports (80, 8080), SSH (22122), allowing loopback, ICMP, established/related connections, and specific IP ranges for Alibaba Cloud DNS and the company's IPs, as well as rules required by Alibaba Cloud's security agent.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Alibaba CloudiptablesLAMPLinux firewallcentos7
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.