Dnsmasq (簡體中文)

來源:互聯網
上載者:User
Dnsmasq (簡體中文) From ArchWiki Jump to: navigation, search 翻譯狀態: 本文是英文頁面 Dnsmasq 的 翻譯,最後翻譯時間:2012-06-18,點擊 這裡可以查看翻譯後英文頁面的改動。

Dnsmasq 提供 DNS 緩衝和 DHCP 服務功能。作為網域名稱解析伺服器(DNS),dnsmasq可以通過緩衝 DNS 請求來提高對訪問過的網址的連線速度。作為DHCP 伺服器,dnsmasq 可以為區域網路電腦提供內網ip地址和路由。DNS和DHCP兩個功能可以同時或分別單獨實現。dnsmasq輕量且易配置,適用於個人使用者或少於50台主機的網路。

Contents  [hide]  1 安裝 2 DHCP 伺服器設定 3 DNS 緩衝設定 3.1 DHCP 設定 3.1.1 使用dhcpcd 3.1.2 使用dhclient 3.1.3 使用NetworkManager 4 啟動dnsmasq守護進程 5 測試 5.1 DNS 緩衝 5.2 DHCP 伺服器 6 小技巧 6.1 避免 OpenDNS 重新導向 Google 請求 6.2 查看租約
安裝

從官方倉庫 中安裝 dnsmasq。 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 pcdhcp-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

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 而不是 dhclient。dhcpcd 現在不再匯入 /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 dnsmasqif [[ ! $(grep 127.0.0.1 /etc/resolv.conf) ]]; then   sed -i ’1s|^|nameserver 127.0.0.1\n|’ /etc/resolv.conffi

這種為可執行:

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

第二個選項是進入 NetworkManagers 設定並手動輸入。選中一個 profile,選擇 DHCP 類型為 'Automatic (specify addresses).' DNS 地址需要手動輸入:127.0.0.1, DNS-server-one, ....

可以用 networkmanager-dhclient 軟體包讓 NetworkManager 使用 dhclient。 啟動dnsmasq守護進程

Dnsmasq以守護進程方式運行。但是在啟動它之前,讓我們看看我們現在解析的速度是多少。輸入以下命令:(dig 是 dnsutils 軟體包的一部分) :

$ 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 (位於 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.