允许 DNS 查询在 iptables 中绑定

允许 DNS 查询在 iptables 中绑定

我已经启动并运行了 DNS 服务器,并且配置了单个 DNS 区域示例.com 在服务器上运行以下命令返回配置的 DNS 区域记录:

# 挖掘 example.com @ns1.example.com

;; QUESTION SECTION:
;example.com.         IN      A

;; ANSWER SECTION:
example.com.  10800   IN      A       10.0.0.1

;; AUTHORITY SECTION:
example.com.  10800   IN      NS      ns2.example.com.
example.com.  10800   IN      NS      ns1.example.com.

;; ADDITIONAL SECTION:
ns1.example.com. 10800 IN     A       10.0.0.1
ns2.example.com. 10800 IN     A       10.0.0.1

我的/etc/hosts文件:

127.0.0.1 example.com www.example.com
127.0.0.1 ns1.example.com ns2..example.com

当我尝试从其他服务器查询 DNS 服务器时,我得到: $ dig example.com @10.0.0.1

;; connection timed out; no servers could be reached

服务器的实际公共 IP 地址被替换为 10.0.0.1 我相信问题很可能是由 iptables 过滤引起的,因为 DNS 服务响应本地查询。以下是我的 iptables 规则:

Chain INPUT (policy DROP 14 packets, 1498 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1      259  157K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
2      325 26717 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
3     287K  149M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
4    14721  872K ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22
5      165  7988 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:80

Chain FORWARD (policy DROP 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy DROP 48 packets, 2949 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1      259  157K ACCEPT     all  --  *      lo      0.0.0.0/0            0.0.0.0/0
2     292K   46M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp spt:22
3    16605 1195K ACCEPT     udp  --  *      *       0.0.0.0/0            8.8.8.8             udp dpt:53
4      130  9822 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
5      430 18880 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:443
6      342  148K ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp spt:80 state ESTABLISHED

我尝试使用以下规则允许端口 53 上的 TCP 和 UDP 传入/传出连接,但不幸的是 DNS 服务仍然无法从 Internet 访问:

iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -m conntrack --cstate NEW -j ACCEPT
iptables -A INPUT -p udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT
iptables -A INPUT -p udp -m state --state NEW --dport 53 -j ACCEPT

以下是一个 TCPDUMP:

[root@localhost ~]# tcpdump port 53 and host {my_ip}
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
08:03:43.011650 IP {my_ip} > {server_hostname}.domain: Flags [S], seq 3026720770, win 29200, options [mss 1460,sackOK,TS val 396679618 ecr 0,nop,wscale 6], length 0
08:03:44.006447 IP {my_ip} > {server_hostname}.domain: Flags [S], seq 3026720770, win 29200, options [mss 1460,sackOK,TS val 396679718 ecr 0,nop,wscale 6], length 0
08:03:46.006615 IP {my_ip} > {server_hostname}.domain: Flags [S], seq 3026720770, win 29200, options [mss 1460,sackOK,TS val 396679918 ecr 0,nop,wscale 6], length 0
08:03:50.016643 IP {my_ip} > {server_hostname}.domain: Flags [S], seq 3026720770, win 29200, options [mss 1460,sackOK,TS val 396680319 ecr 0,nop,wscale 6], length 0
08:03:58.026589 IP {my_ip} > {server_hostname}.domain: Flags [S], seq 3026720770, win 29200, options [mss 1460,sackOK,TS val 396681120 ecr 0,nop,wscale 6], length 0
08:04:14.066598 IP {my_ip} > {server_hostname}.domain: Flags [S], seq 3026720770, win 29200, options [mss 1460,sackOK,TS val 396682724 ecr 0,nop,wscale 6], length 0
08:04:46.186714 IP {my_ip} > {server_hostname}.domain: Flags [S], seq 3026720770, win 29200, options [mss 1460,sackOK,TS val 396685936 ecr 0,nop,wscale 6], length 0
7 packets captured
7 packets received by filter
0 packets dropped by kernel

任何提示都将不胜感激:)

答案1

我想我明白你的问题了,对应于 OUTPUT 链的 iptables 规则正在阻止来自已分配 10.0.0.1 ip 的接口的 udp 53 端口流量。

请使用以下命令允许传出 DNS 请求。

iptables -A OUTPUT -p udp -m udp --dport 53 -j ACCEPT

首次更新

请检查 bind 是否正在监听所有接口,即 named.conf 是否有 listen-on { any; };

相关内容