1.1 企业配置iptables的问题
- VPN服务(拨号到VPN服务器上,然后以VPN的内网地址去访问内部机器)
- 前端对外提供服务的机器SSH端口都做禁止外部IP访问限制,可以开启后端或者不对外提供服务的机器,保留SSH服务(更改root和SSH端口),然后平时通过VPN连接跳板机,通过跳板机连接其他机器
- 流量特别大的外网机器不要开防火墙,会影响性能,可以购买硬件防火墙代替
1.2 iptables企业环境配置步骤
1.2.1 环境配置主机防火墙的两种模式
- 逛公园模式(添加的规则相当于黑名单):
- 默认随便可以进出,对非法分子进行拒绝,默认规则默认是允许的状态。
- 企业应用:企业配置上网网关路由
- 看电影模式(添加的规则相当于白名单):
- 默认没认证进不去,认证过才可以通行,默认规则默认是不允许的状态。
- 企业应用:服务器主机防火墙
第二种模式更严格,更安全;这两种模式的本质就是防火墙的默认规则是允许还是拒绝。
1.2.2 清除防火墙规则
[[email protected] ~]# iptables -F [[email protected] ~]# iptables -X [[email protected] ~]# iptables -Z
1.2.3 增加允许SSH端口访问规则
[[email protected] ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
1.2.4 设置链的默认规则
[[email protected] ~]# iptables -P INPUT DROP [[email protected] ~]# iptables -P FORWARD DROP [[email protected] ~]# iptables -P OUTPUT ACCEPT
1.2.5 配置回环接口策略
[[email protected] ~]# iptables -A INPUT -i lo -j ACCEPT # 让自己可以访问自己
1.2.6 允许内网地址通信
[[email protected] ~]# iptables -A INPUT -s 172.16.1.0/24 -j ACCEPT
1.2.7 配置应用程序规则
[[email protected] ~]# iptables -A INPUT -p tcp -m multiport --dport 80,443 -j ACCEPT
1.2.8 允许Web服务与FTP服务通信(可选)
[[email protected] ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
1.2.9 允许合作企业或公司的外网服务器进行访问
[[email protected] ~]# iptables -A INPUT -s 10.0.0.103 -j ACCEPT
1.2.10 配置完成后写成脚本
#!/bin/bash # This is a server firewall created by leon 17:03 2017-12-05 # http://blog.leonshadow.com # qq:632113590 # define variable PATH IPT=/sbin/iptables #Remove any existing rules $IPT -F $IPT -X $IPT -Z # setting for SSH service iptables -A INPUT -p tcp --dport 22 -j ACCEPT # setting default firewall policy $IPT -P OUTPUT ACCEPT $IPT -P FORWARD DROP $IPT -P INPUT DROP # setting for loopback interface $IPT -A INPUT -i lo -j ACCEPT # setting access rules # one,ip access rules,allow all the ips of $IPT -A INPUT -s 172.16.1.0/24 -j ACCEPT $IPT -A INPUT -p tcp -m multiport --dport 80,443 -j ACCEPT # icmp $IPT -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT # others RELATED $IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
1.2.11 查看配置情况
[[email protected] ~]# iptables -nvL --line-number Chain INPUT (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 1052 79597 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 2 30 2224 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 3 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 80,443 4 0 0 ACCEPT all -- * * 172.16.1.0/24 0.0.0.0/0 5 1835 74002 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 6 0 0 ACCEPT all -- * * 10.0.0.103 0.0.0.0/0 Chain FORWARD (policy DROP 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 61 packets, 6532 bytes) num pkts bytes target prot opt in out source destination
1.2.12 保存设置
- 命令行配置(即时生效):
[[email protected] ~]# /etc/init.d/iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
- 直接配置文件(重启iptables服务生效):
[[email protected] ~]# cat /etc/sysconfig/iptables # Generated by iptables-save v1.4.7 on Tue Dec 5 15:24:24 2017 *filter :INPUT DROP [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [90:10348] -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT -A INPUT -s 172.16.1.0/24 -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -s 10.0.0.103/32 -j ACCEPT COMMIT # Completed on Tue Dec 5 15:24:24 2017

我的微信
如果有技术上的问题可以扫一扫我的微信