Difference between revisions of "Dnsmasq (简体中文)"

From ArchWiki
Jump to: navigation, search
m (DNS 缓存设置)
(自定义配置)
Line 49: Line 49:
 
如果目录不存在,需要手动创建:
 
如果目录不存在,需要手动创建:
 
  mkdir /etc/NetworkManager/dnsmasq.d
 
  mkdir /etc/NetworkManager/dnsmasq.d
 +
 +
===== Other methods =====
 +
{{translateme (简体中文)|待翻译}}
 +
If using the dnsmasq daemon, then it is necessary to add the localhost address to {{ic|resolv.conf}} (which NetworkManager will be overriding).
 +
 +
Since the upgrade of [[NetworkManager]] to 0.7, Arch Linux now calls {{Pkg|dhcpcd}} directly instead of the common default with {{Pkg|dhclient}}.  Because of the arguments set with {{Pkg|dhcpcd}}, it no longer sources the {{ic|/etc/resolv.conf.head}}, and {{ic|/etc/resolv.conf.tail}} settings for insertion of name servers.  Several options are available.
 +
 +
The first option would be to add a script to the NetworkManager dispatcher to prepend localhost to {{ic|resolv.conf}}:
 +
 +
{{hc|/etc/NetworkManager/dispatcher.d/localhost-prepend|<nowiki>
 +
#!/bin/bash                                     
 +
# Prepend localhost to resolv.conf for dnsmasq
 +
 +
if [[ ! $(grep 127.0.0.1 /etc/resolv.conf) ]]; then
 +
  sed -i '1s|^|nameserver 127.0.0.1\n|' /etc/resolv.conf
 +
fi</nowiki>}}
 +
 +
and make it executable:
 +
 +
# chmod +x /etc/NetworkManager/dispatcher.d/localhost-prepend
 +
 +
The second option be to go into NetworkManagers' settings (usually by right-clicking the applet) and entering settings manually.  Setting up will depending on the type of front-end used; the process usually involves right-clicking on the applet, editing (or creating) a profile, and then choosing DHCP type as 'Automatic (specify addresses).'  The DNS addresses will need to be entered and are usually in this form: {{ic|127.0.0.1, DNS-server-one, ...}}.
 +
 +
Lastly, NetworkManager with dhclient can be used ({{AUR|networkmanager-dhclient}}).
  
 
===== 自定义配置 =====
 
===== 自定义配置 =====

Revision as of 13:59, 9 December 2012

翻译状态: 本文是英文页面 Dnsmasq翻译,最后翻译时间:2012-11-21,点击这里可以查看翻译后英文页面的改动。

Dnsmasq 提供 DNS 缓存和 DHCP 服务功能。作为域名解析服务器(DNS),dnsmasq可以通过缓存 DNS 请求来提高对访问过的网址的连接速度。作为DHCP 服务器,dnsmasq 可以为局域网电脑提供内网ip地址和路由。DNS和DHCP两个功能可以同时或分别单独实现。dnsmasq轻量且易配置,适用于个人用户或少于50台主机的网络。此外它还自带了一个 PXE 服务器。

安装

官方仓库安装 dnsmasq

DNS 缓存设置

要在单台电脑上以守护进程方式启动dnsmasq做DNS缓存服务器,编辑/etc/dnsmasq.conf,添加监听地址:

listen-address=127.0.0.1

如果用此计算机作为默认 DNS,请用固定 IP 地址:

listen-address=192.168.1.1 # Example IP

DNS 地址文件

在配置好dnsmasq后,你需要编辑/etc/resolv.conf让DHCP客户端首先将本地地址(localhost)加入 DNS 文件(/etc/resolv.conf),然后再通过其他DNS服务器解析地址。配置好DHCP客户端后需要重新启动网络来使设置生效。

使用dhcpcd

dhcpcd 可以是通过创建(或编辑)/etc/resolv.conf.head文件或 /etc/resolv.conf.tail文件来指定dns服务器,使/etc/resolv.conf不会被每次都被dhcpcd重写

echo "nameserver 127.0.0.1" > /etc/resolv.conf.head #设置dns服务器为127.0.0.1

使用dhclient

要使用 dhclient, 取消 /etc/dhclient.conf 文件中如下行的注释:

prepend domain-name-servers 127.0.0.1;

使用NetworkManager

Networkmanager 默认直接使用 dhcpcd 而不是 dhclientdhcpcd 现在不再导入 /etc/resolv.conf.head/etc/resolv.conf.tail 名称解析设置。有如下几种选项:

NetworkManager 配置为启动 dnsmasq,将 dns=dnsmasq 加入 NetworkManager.conf[main]部分,并禁用 dsmasq 的开机启动:

/etc/NetworkManager/NetworkManager.conf
[main]
plugins=keyfile
dns=dnsmasq

如果目录不存在,需要手动创建:

mkdir /etc/NetworkManager/dnsmasq.d
Other methods

Tango-preferences-desktop-locale.png本页面需要更新翻译,内容可能已经与英文脱节。要贡献翻译,请访问简体中文翻译组Tango-preferences-desktop-locale.png

附注: 待翻译

If using the dnsmasq daemon, then it is necessary to add the localhost address to resolv.conf (which NetworkManager will be overriding).

Since the upgrade of NetworkManager to 0.7, Arch Linux now calls dhcpcd directly instead of the common default with dhclient. Because of the arguments set with dhcpcd, it no longer sources the /etc/resolv.conf.head, and /etc/resolv.conf.tail settings for insertion of name servers. Several options are available.

The first option would be to add a script to the NetworkManager dispatcher to prepend localhost to resolv.conf:

/etc/NetworkManager/dispatcher.d/localhost-prepend
#!/bin/bash                                       
# Prepend localhost to resolv.conf for dnsmasq

if [[ ! $(grep 127.0.0.1 /etc/resolv.conf) ]]; then
  sed -i '1s|^|nameserver 127.0.0.1\n|' /etc/resolv.conf
fi

and make it executable:

# chmod +x /etc/NetworkManager/dispatcher.d/localhost-prepend

The second option be to go into NetworkManagers' settings (usually by right-clicking the applet) and entering settings manually. Setting up will depending on the type of front-end used; the process usually involves right-clicking on the applet, editing (or creating) a profile, and then choosing DHCP type as 'Automatic (specify addresses).' The DNS addresses will need to be entered and are usually in this form: 127.0.0.1, DNS-server-one, ....

Lastly, NetworkManager with dhclient can be used (networkmanager-dhclientAUR).

自定义配置

从 NetworkManager 0.9.6 开始,dnsmasq 自定义配置可以在 /etc/NetworkManager/dnsmasq.d/ 中配置。

DHCP 服务器设置

dnsmasq 配置文件(/etc/dnsmasq.conf),必要的配置如下:

# Only listen to routers' LAN NIC.  Doing so opens up tcp/udp port 53 to
# localhost and udp port 67 to world:
interface=<LAN-NIC>

# dnsmasq will open tcp/udp port 53 and udp port 67 to world to help with
# dynamic interfaces (assigning dynamic ips). Dnsmasq will discard world
# requests to them, but the paranoid might like to close them and let the 
# kernel handle them:
bind-interfaces

# Dynamic range of IPs to make available to LAN pc
dhcp-range=192.168.111.50,192.168.111.100,12h

# If you’d like to have dnsmasq assign static IPs, bind the LAN computer's
# NIC MAC address:
dhcp-host=aa:bb:cc:dd:ee:ff,192.168.111.50

启动守护进程

现在我们启动它:

# systemctl start dnsmasq

要让dnsmasq开机启动:

# systemctl enable dnsmasq

查看dnsmasq是否启动正常,检查 /var/log/messages.log 日志文件。

你需要重启 network 这样 dhcpd 会重新建立 /etc/resolv.conf

# /etc/rc.d/network restart

现在让我们测试一下 DNS 查询然后测量响应时间:

$ dig archlinux.org | grep "Query time"

Query time 应该会减少。

并且如果你移除 grep,你可以看到使用的服务器(Query time 下一行),现在它应该是 localhost 也就是 127.0.0.1。

测试

DNS 缓存

要测试查询速度,请访问一个 dnsmasq 启动后没有访问过的网站,执行 (dig (位于 dnsutils 软件包):

$ dig archlinux.org | grep "Query time"

再次运行命令,因为使用了缓存,查询时间应该大大缩短。

DHCP 服务器

从一个连接到使用了 dnsmasq 的计算机的计算机,配置它使用 DHCP 自动获取 IP 地址,然后尝试连接到你平时使用的网络。

小技巧

避免 OpenDNS 重定向 Google 请求

要避免 OpenDNS 重定向所有 Google 请求到他们自己的搜索服务器,添加以下内容到 /etc/dnsmasq.conf

server=/www.google.com/X.X.X.X

用你的 ISP 的 DNS 服务器/路由器的 IP 替代 X.X.X.X 。

查看租约

cat /var/lib/misc/dnsmasq.leases