Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3670831
  • 博文数量: 880
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 6155
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-11 09:12
个人简介

To be a better coder

文章分类

全部博文(880)

文章存档

2022年(5)

2021年(60)

2020年(175)

2019年(207)

2018年(210)

2017年(142)

2016年(81)

分类: LINUX

2017-09-26 20:04:06

转载自 http://blog.csdn.net/rheostat/article/details/7831020

Dnsmasq (简体中文)

From ArchWiki
Jump to: ,
翻译状态: 本文是英文页面 的,最后翻译时间:2012-06-18,点击可以查看翻译后英文页面的改动。

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

Contents

 [] 

安装

从 中

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=

# 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

DNS 缓存设置

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

listen-address=127.0.0.1

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

listen-address=192.168.1.1 # Example IP

DHCP 设置

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

使用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 默认直接使用 而不是 现在不再导入 /etc/resolv.conf.head 和 /etc/resolv.conf.tail 名称解析设置。有如下几种选项:

第一个选择是在 NetworkManager dispatcher 中使用脚本将 localhost 加入 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

这种为可执行:

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

第二个选项是进入 NetworkManagers 设置并手动输入。选中一个 profile,选择 DHCP 类型为 'Automatic (specify addresses).' DNS 地址需要手动输入:127.0.0.1, DNS-server-one, ....

可以用 软件包让 NetworkManager 使用 dhclient。

启动dnsmasq守护进程

Dnsmasq以守护进程方式运行。但是在启动它之前,让我们看看我们现在解析的速度是多少。输入以下命令:(dig 是 软件包的一部分) :

$ dig archlinux.org | grep "Query time"

现在我们启动它:

# /etc/rc.d/dnsmasq start

要让dnsmasq开机启动,添加dnsmasq到/etc/rc.conf的守护进程列表里面:

DAEMONS=(network 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 (位于 软件包):

$ dig archlinux.org | grep "Query time"

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

DHCP 服务器

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

小技巧

避免 OpenDNS 重定向 Google 请求

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

server=/

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

查看租约

cat /var/lib/misc/dnsmasq.leases
阅读(4237) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~