上几次视频我们做了端口方面的设置, 如果你发现不能解析一些域名,说明DNS不可用,
而DNS不能用了,有几种可能性,其中一种就是防火墙把DNS端口给关闭了。

下面我们来看看如何设置iptables来打开DNS端口,DNS端口对应的是53

大家看到我现在的情况了吧,只开放22和80端口, 我现在看看能不能解析域名。

host www.google.com 输入这个命令后,一直等待,说明DNS不通

出现下面提示 :
;; connection timed out; no servers could be reached

ping 一下域名也是不通
[root@localhost ~]# ping www.google.com
ping: unknown host www.google.com

我这里的原因就是 iptables 限制了53端口。

有些服务器,特别是Web服务器减慢,DNS其实也有关系的,无法发送包到DNS服务器导致的。

下面演示下如何使用 iptables 来设置DNS 53这个端口,如果你不知道 域名服务端口号,你

可以用命令 : grep domain /etc/services

[root@localhost ~]# grep domain /etc/services
domain 53/tcp # name-domain server
domain 53/udp
domaintime 9909/tcp # domaintime
domaintime 9909/udp # domaintime

看到了吧, 我们一般使用 udp 协议。

好了, 开始设置。。。

iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
这是我们 ping 一个域名,数据就是从本机出去,所以我们先设置 OUTPUT,
我们按照ping这个流程来设置。

然后 DNS 服务器收到我们发出去的包,就回应一个回来
iptables -A INPUT -p udp --sport 53 -j ACCEPT

同时还要设置
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --sport 53 -j ACCEPT

好了, 下面开始测试下, 可以用 iptables -L -n 查看设置情况,确定没有问题就可以测试了

[root@localhost ~]# iptables -L -n
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp spt:53
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:53

Chain FORWARD (policy DROP)
target prot opt source destination

Chain OUTPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:22 state ESTABLISHED
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:80 state ESTABLISHED
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:53
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp spt:53

可以测试一下 是否 DNS 可以通过iptables 了。

[root@localhost ~]# host www.google.com
www.google.com is an alias for www.l.google.com.
www.l.google.com is an alias for www-china.l.google.com.
www-china.l.google.com has address 64.233.189.104
www-china.l.google.com has address 64.233.189.147
www-china.l.google.com has address 64.233.189.99

正常可以解析 google 域名。

ping 方面可能还要设置些东西。

用 nslookup 看看吧

[root@localhost ~]# nslookup
> www.google.com
Server: 192.168.1.1
Address: 192.168.1.1#53

Non-authoritative answer:
www.google.com canonical name = www.l.google.com.
www.l.google.com canonical name = www-china.l.google.com.
Name: www-china.l.google.com
Address: 64.233.189.147
Name: www-china.l.google.com
Address: 64.233.189.99
Name: www-china.l.google.com
Address: 64.233.189.104

说明本机DNS正常, iptables 允许53这个端口的访问。
关于 iptables 对 DNS 的设置就到这里, 谢谢观看!!


“iptables防火墙设置视频演示 06(DNS端口53设置)”没有评论

有任何疑问或建议,可以给作者留言:



公告:

  • 2010年5月之前的视频是文字解说演示,没有声音。
  • 2010年5月以后的视频全部带声音。