linux防火墙是在内核进行数据包隔离的,tcp,udp协议盏也是在linux内核,因为本机目前使用的是可以进行上网,需要向其他服务器发数据包,并能获取到其他服务器传递过来的数据包,所以我的iptables设置是这样的:
root@huangxudong-X456UR:/etc/init.d# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- anywhere anywhere tcp dpt:ssh
DROP tcp -- anywhere anywhere tcp dpt:telnet
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:https
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT tcp -- anywhere anywhere tcp dpt:http
ACCEPT tcp -- anywhere anywhere tcp dpt:http-alt
上面配置的意思是,这台机器可以访问外部服务器的资源,为什么这样配置阿,首先要知道tcp,udp协议的规则,本机和服务器80,443端口通信,本机会随即生成一个端口用来接受服务器第二次握手的时候,以及以后每次传递心跳包,所以这里output 设置成了ACCEPT,INPUT 成了ACCEPT,原理就是这样的,tcp三次握手以及数据传输都是带目标端口和源端口的;上面只是过滤了tcp,udp协议的数据包,过滤arp协议需要安装arptables,设置本机arp只和网关通信:
root@huangxudong-X456UR:/etc/init.d# cat arptables.sh
#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# 进站规则:如果来源 mac 不是 11111111111111 则丢弃数据包
arptables -A INPUT -i wlp3s0 --src-mac ! 111111111111 -j DROP
# 进站规则:如果来源 mac 不是 222222222 且来源 IP 不是 192.168.1.1 则丢弃数据包
arptables -A INPUT -i wlp3s0 --src-ip 192.168.1.1 --src-mac ! 222222222222 -j DROP
# 允许所有的出站请求
arptables -A OUTPUT --destination-mac ff:ff:ff:ff:ff:ff -j ACCEPT
root@huangxudong-X456UR:/etc/init.d#
大致思路就这样,需要开放什么,自己设置,很方便!