介绍:防火墙是整个数据包进入主机前的第一道关卡。防火墙主要通过Netfilter与TCPwrappers两个机制(来源于linux 内核)来管理的。
## iptables命令使用
参数 | 作用 | 案例 |
-N | Iptables默认4表5链,-N是自定义链。 | iptables -N mgnt_ext |
-F | 清空规则链: | iptables -F |
-L | 查看规则链: | iptables -L ; iptables --line -nL |
-D | num 删除某一条规则 | iptables -D INPUT 1 (num) |
-A | 在规则链的末尾加入新规则: | iptables -A INPUT -s x.x.x.x/32 -p tcp --dport 1521 -j ACCEPT |
-d | 匹配目标地址 | iptables -A INPUT -d 172.21.51.3/32 -j ACCEPT |
-I | num 在规则链的头部加入新规则: | 如果iptables -P INPUT DROP # 配置默认的不让进,则需要访问的主机添加白名单IP、协议、端口即可。 iptables -I INPUT 3 -i lo -j ACCEPT iptables -I INPUT 4 -s x.x.x.x/32 -j ACCEPT |
-s | 匹配来源地址IP/MASK,加叹号"!"表示除这个IP外。 | |
-i | 网卡名称 匹配从这块网卡流入的数据 | |
-o | 网卡名称 匹配从这块网卡流出的数据 | |
-p | 匹配协议,如tcp,udp,icmp | |
-m | 扩展模块 | |
-j | 指定了当与规则(Rule)匹配时如何处理数据包,可能的值是ACCEPT, DROP, QUEUE, RETURN,还可以指定其他链(Chain)作为目标。 | iptables -A INPUT -j DROP |
-P | 设置默认策略 | iptables -P INPUT DROP # 配置默认的不让进 |
iptables-save | 保存 | service iptables save 或者 iptables-save >/etc/sysconfig/iptables |
iptables-restore | 恢复 | iptables-restore < /etc/sysconfig/iptables |
SNAT | 源地址转换:代理内部客户端访问外部网络 | iptables -t nat -A POSTROUTING -s 172.16.0.0/16 -j SNAT --to-source 192.168.254.61 #在POSTROUTING链上面,将来自源地址为172.16.0.0/16网段的数据包的源地址都转换为192.168.254.61 #基于动态IP地址来做NAT代理内部客户端来上网 #如果IP地址是动态获取的ADSL宽带来做NAT代理上网实现方式 iptables -t nat -A POSTROUTING -s 172.16.0.0/16 -o eth1 -j MASQUEREADE 注释:这里最好使用"-o"选项来指定出口,MASQUEREADE会自动调用该接口的地址作为源地址出去。 |
DNAT | 目标地址转换:将内部服务器发布至外部网络 | iptables -t nat -A PREROUTING -d 192.168.254.61 -j DNAT --to-destination 172.16.251.185 #在PREROUTING链上面,将请求地址NAT服务器 eth1 192.168.1.254的数据包全部转发到PC1 172.16.251.185主机上 iptables -t nat -A PREROUTING -d 192.168.254.61 -p tcp --dport 80 -j DNAT --to-destination 172.16.251.185:8080 #此条规则是将所有请求NAT服务器eth1 192.168.254.61地址的80号端口都转发到172.16.251.185主机上8080端口,当然前提必须在PC1上面监听8080端口 |
下一篇:Linux中阶—日志采集rsyslog(二)