CentOS 7 Docker 防火牆簡單配置

LSTONE發表於2017-12-21

禁用 firewalld 服務

systemctl disable firewalld
systemctl stop firewalld

安裝 iptables 防火牆服務

yum install iptables-services

建立 iptables 配置指令碼

cat >> /usr/local/bin/fired.sh <<`EOF`
#!/bin/bash

iptables -F
iptables -X
iptables -Z
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p icmp --icmp-type 8 -j ACCEPT
#iptables -A INPUT -p tcp --dport 80 -i eth0 -m state -state NEW -m recent -update -seconds 60  -hitcount 50 -j DROP
#iptables -A OUTPUT -o eth0 -m owner -uid-owner vivek -p tcp --dport 80 -m state -state NEW,ESTABLISHED  -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport 1:1023 --dport 1:1023 --syn -j DROP
iptables -A INPUT -p tcp -i eth0 --dport 22     --sport 1024:65534 -j ACCEPT
iptables -A INPUT -p tcp --dport 80     --sport 1024:65534 -j ACCEPT
iptables -A INPUT -p tcp --dport 2376   --sport 1024:65534 -j ACCEPT
iptables -A INPUT -p tcp --dport 3306   --sport 1024:65534 -j ACCEPT

# OpenVPN Configuration
# iptables -A POSTROUTING -t nat -s 10.8.0.0/24 -o eth0 -j MASQUERADE
# iptables -A FORWARD -i tun+ -j ACCEPT
# iptables -A INPUT -s 10.8.0.0/24 -j ACCEPT
# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# iptables -A INPUT -p TCP -i eth0 --dport 10173 --sport 1024:65534 -j ACCEPT
# iptables -A INPUT -p UDP -i eth0 --dport 10173 --sport 1024:65534 -j ACCEPT
EOF

chmod +x /usr/local/bin/fired.sh

加入開機啟動項

cat >> /etc/rc.d/rc.local <<EOF

# Firewall & Docker
/usr/bin/systemctl start iptables.service
/usr/local/bin/fired.sh
/usr/bin/systemctl start docker
EOF

chmod +x /etc/rc.d/rc.local

禁用相關服務自啟動

# 注: Docker 啟動時會自動加入一些

systemctl disable iptables.service
systemctl disable docker

相關文章