Sort out the common configuration of iptables under centos
1. View all iptables configurations
iptables -L -n
2. Add a rule to allow INPUT access. The following are the port settings for common services. If you need to deny access, change ACCEPT to DROP.
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#HTTP
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
#HTTPS
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#POP3
iptables -A INPUT -p tcp --dport 110 -j ACCEPT
#SMTP
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
#FTP
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
#DNS
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
3. Add a rule to restrict INPUT access using IP, here take SSH as an example, 192.168.0.100 is the allowed IP
iptables -D INPUT -p tcp --dport 22 -j ACCEPT
#ADD
iptables -A INPUT -s 192.168.0.100 -p tcp --dport 22 -j ACCEPT
4. Save the iptables settings, remember to save after modifying the rules
/etc/rc.d/init.d/iptables save
5. Restart iptables
service iptables restart
6. Turn on/off boot up
chkconfig iptables on
chkconfig iptables off
7. Open the iptables configuration file:
vi /etc/sysconfig/iptables
Use the /etc/init.d/iptables status command to check whether port 80 is opened. If not, there are two ways to deal with it:
8. Modify the vi /etc/sysconfig/iptables command to add the firewall to open port 80
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
9. Turn off/on/restart the firewall
/etc/init.d/iptables stop
#start start
#restart restart
10. Permanently turn off the firewall
chkconfig --level 35 iptables off
/etc/init.d/iptables stop
iptables -P INPUT DROP
11. Open port 21 in active mode
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
12. Open ports between 49152 and 65534 in passive mode
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
Restart iptables after all modifications are made:
service iptables restart
You can verify that the rules are in effect:
How to manually change the config file:
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [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 -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
[root@CentOS ~]# /etc/init.d/iptables restart