iptables 打开dns访问端口

来源:互联网 发布:银河麒麟软件下载 编辑:程序博客网 时间:2024/06/05 20:47

iptables 打开dns访问端口
今天在测试本机DNS的时候发现无法解释域名,经查原因出在iptables上边。
我立即加了 iptables -t filter -A INPUT -p udp --dport 53 -j ACCEPT的链路,发现还是

不行。一般认为DNS就是通过访问DNS服务器的53端口进行域名解释的。本机端口应该是任意的,

可是就是不通。
没有办法再添加iptables -t filter -j LOG -p udp --log-prefix "DNS monitor:" 添加一

个日志功能。

添加了以上记录后,还不能记录那个功能。还需要再在
/etc/syslog.conf
添加一个记录,
kern.warning /var/log/iptables.log

kern是设备名,warning 是记录级别,/var/log/iptables.log 记录日志文件位置。
其中的具体设置如果大家有兴趣可以另外查找一下其它的文档。
最后重启一下syslog服务。

service syslog restart

现在就可以了。

使用nslookup www.sina.com.cn

然后我们看一下/var/log/iptables.log文件
:10:db:69:43:50:08:00 SRC=219.141.140.10 DST=192.2.8.185 LEN=169TOS=0x00 PREC=0x00

TTL=241 ID=63822 DF PROTO=UDP SPT=53 DPT=45623 LEN=149

里面的SPT=53是53,也就是我们需要开放53端口。
iptables -t filter -A INPUT -p udp -m udp -j ACCEPT

nslookup www.sina.com.cn
Server:        219.141.140.10
Address:       219.141.140.10#53

现在再执行nslookup www.sina.com.cn

Non-authoritative answer:
www.sina.com.cn canonical name= jupiter.sina.com.cn.
jupiter.sina.com.cn    canonical name = hydra.sina.com.cn.
Name:   hydra.sina.com.cn
Address: 218.30.108.189
Name:   hydra.sina.com.cn
Address: 218.30.108.190
Name:   hydra.sina.com.cn
Address: 218.30.108.191

  最后要单独说明一下,DNS默认使用端口是UDP53 不是TCP,看下面

[root@ncunicom sysconfig]# iptables -L -n
Chain INPUT (policy ACCEPT)
target    prot optsource              destination        
ACCEPT    all  -- 0.0.0.0/0           0.0.0.0/0          state RELATED,ESTABLISHED
ACCEPT    icmp -- 0.0.0.0/0           0.0.0.0/0          
ACCEPT    all  -- 0.0.0.0/0           0.0.0.0/0          
ACCEPT    tcp  -- 0.0.0.0/0           0.0.0.0/0          state NEW tcp dpt:22
ACCEPT    tcp  -- 0.0.0.0/0           0.0.0.0/0          state NEW tcp dpt:21
ACCEPT    tcp  -- 0.0.0.0/0           0.0.0.0/0          state NEW tcp dpt:20
ACCEPT    tcp  -- 0.0.0.0/0           0.0.0.0/0          state NEW tcp dpt:53
ACCEPT    udp  -- 0.0.0.0/0           0.0.0.0/0          udp dpt:53
REJECT    all  -- 0.0.0.0/0           0.0.0.0/0          reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target    prot optsource              destination        
REJECT    all  -- 0.0.0.0/0           0.0.0.0/0          reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target    prot optsource              destination        
起作用的是这条:ACCEPT    udp  -- 0.0.0.0/0           0.0.0.0/0          udp dpt:53

0 0