我正在build立一个简单的服务器架构:1个应用程序服务器(LAMPserver)和1个数据库服务器(DBserver)。 在这个阶段一切正常(我可以从我的LAMP服务器连接到我的DB服务器)。 但是当我在我的DBserver上设置我的iptables规则时, 我无法连接了。 我的LAMP服务器的iptables规则是:
#!/bin/sh # Remove all rules iptables -t filter -F iptables -t filter -X # Forbid all traffic iptables -t filter -P INPUT DROP iptables -t filter -P FORWARD DROP iptables -t filter -P OUTPUT DROP # Allow established connection iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # Allow loopback iptables -t filter -A INPUT -i lo -j ACCEPT iptables -t filter -A OUTPUT -o lo -j ACCEPT # Allow HTTP iptables -t filter -A OUTPUT -p tcp --dport 80 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT # Allow HTTPS iptables -t filter -A OUTPUT -p tcp --dport 443 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 443 -j ACCEPT # Allow SSH iptables -t filter -A OUTPUT -p tcp --dport 22 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT # Mysql iptables -t filter -A OUTPUT -p tcp --dport 3306 -j ACCEPT iptables -t filter -A INPUT -p tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT # Allow FTP iptables -t filter -A OUTPUT -p tcp --dport 20:21 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 20:21 -j ACCEPT # Allow SMTP iptables -t filter -A OUTPUT -p tcp --dport 25 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 25 -j ACCEPT iptables -t filter -A OUTPUT -p tcp --dport 587 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 587 -j ACCEPT # Allow POP3 iptables -t filter -A OUTPUT -p tcp --dport 110 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 110 -j ACCEPT # Allow POPS iptables -t filter -A OUTPUT -p tcp --dport 995 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 995 -j ACCEPT # Allow POPS iptables -t filter -A OUTPUT -p tcp --dport 995 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 995 -j ACCEPT # Allow IMAP iptables -t filter -A OUTPUT -p tcp --dport 143 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 143 -j ACCEPT # Allow IMAPS iptables -t filter -A OUTPUT -p tcp --dport 993 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 993 -j ACCEPT # Allow DNS iptables -t filter -A OUTPUT -p tcp --dport 53 -j ACCEPT iptables -t filter -A INPUT -p tcp --sport 53 -j ACCEPT iptables -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT iptables -t filter -A INPUT -p udp --sport 53 -j ACCEPT # Allow ICMP (ping) iptables -t filter -A INPUT -p icmp -j ACCEPT iptables -t filter -A OUTPUT -p icmp -j ACCEPT # NTP (horloge du serveur) sudo iptables -t filter -A OUTPUT -p udp --dport 123 -j ACCEPT # Prevent Flood or Ddos iptables -A FORWARD -p tcp --syn -m limit --limit 1/second -j ACCEPT iptables -A FORWARD -p udp -m limit --limit 1/second -j ACCEPT iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/second -j ACCEPT # Limit port scan iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT
我在@MadHatter文章基于端口3306 iptables: IPTables:允许传出的MySQL连接,但不是传入的连接
问题出在我的数据库服务器上。 如果我不使用任何iptables它工作正常; 但是当我使用下面的iptables我不能得到它的工作。
#!/bin/sh # Remove all rules iptables -t filter -F iptables -t filter -X # Forbid all traffic iptables -t filter -P INPUT DROP iptables -t filter -P FORWARD DROP iptables -t filter -P OUTPUT DROP # Allow established connection iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # Allow loopback iptables -t filter -A INPUT -i lo -j ACCEPT iptables -t filter -A OUTPUT -o lo -j ACCEPT # Allow HTTP iptables -t filter -A OUTPUT -p tcp --dport 80 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT # Allow HTTPS iptables -t filter -A OUTPUT -p tcp --dport 443 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 443 -j ACCEPT # Allow SSH iptables -t filter -A OUTPUT -p tcp --dport 22 -j ACCEPT iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT # Mysql iptables -A INPUT -p tcp -s IP-DBserver --sport 1024:65535 -d IP-LAMPserver --dport 3306 -m stat$ iptables -A OUTPUT -p tcp -s IP-LAMPserver --sport 3306 -d IP-DBserver --dport 1024:65535 -m sta$ # Allow DNS iptables -t filter -A OUTPUT -p tcp --dport 53 -j ACCEPT iptables -t filter -A INPUT -p tcp --sport 53 -j ACCEPT iptables -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT iptables -t filter -A INPUT -p udp --sport 53 -j ACCEPT # Allow ICMP (ping) iptables -t filter -A INPUT -p icmp -j ACCEPT iptables -t filter -A OUTPUT -p icmp -j ACCEPT # NTP (horloge du serveur) sudo iptables -t filter -A OUTPUT -p udp --dport 123 -j ACCEPT # Prevent Flood or Ddos iptables -A FORWARD -p tcp --syn -m limit --limit 1/second -j ACCEPT iptables -A FORWARD -p udp -m limit --limit 1/second -j ACCEPT iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/second -j ACCEPT # Limit port scan iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT
有任何想法吗?
确定解决scheme已经由@MadHatter在serverfault上
IPTables:允许传出MySQL连接,但不允许传入连接
iptables -t filter -A OUTPUT -p tcp --dport 3306 -j ACCEPT iptables -t filter -A INPUT -p tcp --sport 3306 -m conntrack --ctstate ESTABLISHED -j ACCEPT