Skip to content

防火墙

iptables

开启: chkconfig iptables on
关闭: chkconfig iptables off

即时生效,重启后失效

开启: service iptables start
关闭: service iptables stop
状态 service iptables status

查看、添加和删除

iptables -nL --line-number #查看当前规则
iptables -A INPUT -p tcp --dport 80 -j ACCEPT #允许访问80端口,-A 排在最后面,从上往下匹配
iptables -D INPUT 2 #删除 INPUT 指定行规则,第二行
iptables -I INPUT -p tcp --dport 80 -j ACCEPT #允许访问80端口,-I 排在前面,从上往下匹配
iptables -I INPUT -p tcp --dport 5700 -j DROP #禁止端口访问
service iptables save #保存修改规则
cat /etc/sysconfig/iptables #查看系统规则

iptables默认规则

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [34:4104]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# 允许我发出去的数据包入站
-A INPUT -p icmp -j ACCEPT
# 允许ICMP协议
-A INPUT -i lo -j ACCEPT
# 允许回环网卡
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
# 允许ssh连接
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
# 拒绝所有其他不符合上述任何一条规则的数据包。并且发送一条host prohibited的消息给被拒绝的主机
COMMIT

限制docker暴露的对外访问端口

docker 会在iptables上加上自己的转发规则,如果直接在input链上限制端口是没有效果的。这就需要限制docker的转发链上的DOCKER表。

# 查询DOCKER表并显示规则编号
iptables -L DOCKER -n --line-number

# 修改对应编号的iptables 规则,这里添加了允许访问ip的限制
iptables -R DOCKER 5 -p tcp -m tcp -s 192.168.1.0/24 --dport 3000 -j ACCEPT

# 限制端口访问
iptables -I DOCKER -p tcp --dport 5701 -j DROP