使用iptables记录谁访问了HDFS
iptables -t filter -F
iptables -t filter -A INPUT -p tcp –dport 22 –match state –state NEW -j LOG
如果使用syslog,那么编辑/etc/syslog.conf,添加:
kern.warning /var/log/iptables.log
如果使用rsyslog,那么编辑/etc/rsyslog.d/60-iptables.conf,添加:
kern.warning /var/log/iptables.log
上面的方法虽然正确,但是在真实300+datanode的环境中,服务器启动iptables以及log功能后,系统资源会紧张,具体是什么紧张我还没有数据。
而且kernel.warning这种粗狂的方式貌似dmesg , tty0系统终端, /var/log/message里面都是这种log,当然还有自己指定的文件 /var/log/iptables下面。
rsyslog采用另外一种方式:
:msg,contains,"IPTABLES: " /var/log/iptables.log
对应的iptables配置如下
iptables -t filter -F
iptables -t filter -A INPUT -p tcp --dport 9000 --match state --state NEW -j LOG --log-prefix "IPTABLES: " --log-level 7
iptables -t filter -L -n --line-numbers
我怀疑是写文件太浪费磁盘io了吧,把日志直接写到远端rsyslog服务器试试。
远程的先不测试了。
换用了另外一种方式
#!/usr/bin/env bash
# @author: zzlyzq@gmail.com
# @date: 2015.03.05
# @description :对于hdfs master9000tcp端口实行白名单限制,避免恶意主机接入hdfs机群造成这样或者那样的破坏。
# @usage: 该sh脚本对于当前文件夹下的所有*.txt文件进行读取,每一行作为一个输入,然后产生
# iptables -t filter -A INPUT -s $ip -j ACCEPT
# 清空所有filter配置策略,重新配置
iptables -t filter -F
# 下面的三条是为了提高性能,因为我们主要是限制tcp port 9000,所以对于其他的udp icmp 和 非tcp 9000可以直接ACCEPT通过,无需匹配INPUT的默认ACCEPT策略
iptables -t filter -A INPUT -p icmp -j ACCEPT
iptables -t filter -A INPUT -p udp -j ACCEPT
iptables -t filter -A INPUT -p tcp -m multiport ! --dport 9000 -j ACCEPT
for file in `ls *.txt`
do
# echo $file
for ip in `cat $file`
do
if [ ${#ip} -lt 7 ];then
# 简单过滤,如果一行的size小于7,三个点+4个字儿,那么就继续
echo "find less than7"
continue
fi
command="iptables -t filter -A INPUT -s $ip -p tcp --dport 9000 -j ACCEPT"
echo $command
$command
done
done
iptables -t filter -A INPUT -p tcp --dport 9000 -j LOG --log-prefix "IPTABLES: "