Iptables is one of the commonly used firewall software in Linux. After LNMP is installed. After you restart the machine, you will find that the Web site cannot be opened, which is largely caused by iptable. Below are some common settings for everyone.
Install iptables firewall
If iptables is not installed, install it first. Run the following command in CentOS:
Yum install iptables
Clear existing firewall rules
Iptables-F
Iptables-X
Iptables-Z
Open specified port
# Allow the local loopback interface (that is, running the local machine to access the local machine)
Iptables-a input-s 127.0.0.1-d 127.0.0.1-j ACCEPT
# Allow established or related connections
Iptables-a input-m state -- state ESTABLISHED, RELATED-j ACCEPT
# Allow external access from all hosts
Iptables-a output-j ACCEPT
# Allow access to port 22
Iptables-a input-p tcp -- dport 22-j ACCEPT
# Allow access to port 80
Iptables-a input-p tcp -- dport 80-j ACCEPT
# Allow port 21 and Port 20 of the FTP service
Iptables-a input-p tcp -- dport 21-j ACCEPT
Iptables-a input-p tcp -- dport 20-j ACCEPT
# If there are other ports, the rule is similar. Just modify the preceding statement slightly.
# Prohibit access by other unpermitted rules
Iptables-a input-j REJECT (note: If port 22 is not added with the permit rule, the SSH link will be disconnected directly .)
Iptables-a forward-j REJECT
View added rules
Iptables-L-n
V: displays details, including the number of matching packages and the number of matching bytes for each rule.
X: disable automatic unit conversion (K, M) based on v)
N: only the ip address and port number are displayed, and the ip address is not resolved as a domain name.
Start iptables and save rules
After iptables is installed on CentOS, iptables does not start automatically after it is started. You can execute the following command:
Chkconfig -- level 345 iptables on
Add it to start
Other configurations
Effective after restart
Chkconfig iptables on # Enable
Chkconfig iptables off # Disable
Effective Immediately
Service iptables start # Enable
Service iptables stop # Disable