2012-02-17 178 views
18

基本上我的問題是,我運行VM開發網站。的dnsmasq,服務於不同的IP地址的基礎上使用的接口

該主機具有其DNS在其運行的dnsmasq從而解決各種開發網站的地址指向虛擬機,即test.mysite.vm等

的問題是,當我從我的工作網絡去我的家庭網絡全部因爲虛擬機IP改變而中斷。是否可以根據請求來自哪個接口來提供不同的IP地址?或者我應該試圖以完全不同的方式解決這個問題?

感謝您的幫助!

+0

不同界面在哪裏起作用?你的家庭網絡和工作網絡使用兩種不同的接口嗎?爲什麼虛擬機的IP更改? – kichik 2012-11-08 18:59:49

+0

是的,它是2個不同的家庭(192.168.0。*)和工作(10.0.0。*)接口。工作IP和家庭IP之間的變化。 – 2012-11-13 10:10:00

回答

23

您可以用不同的接口,它監聽上運行的dnsmasq兩個實例,每個。您可以使用--interface=X--bind-interfaces選項。默認情況下,它還會綁定迴環設備lo,如果兩個進程嘗試綁定它,則會失敗。使用--except-interface=lo來避免這種情況。

dnsmasq --interface=eth0 --except-interface=lo --bind-interfaces --dhcp-range=192.168.0.2,192.168.0.10,12h 
dnsmasq --interface=eth1 --except-interface=lo --bind-interfaces --dhcp-range=10.0.0.2,10.0.0.10,12h 

確定您的配置文件在測試時爲空,因爲它總是覆蓋命令行。您也可以使用--conf-file=/dev/null

正如我在評論中提到的,我也不太清楚如何幫助您的情況,但它可能會幫助別人誰試圖讓兩個不同的接口,兩個不同的地址範圍。

+0

真的沒有辦法讓2個網絡沒有運行多個實例嗎?這確實使事情變得複雜 – TheLQ 2012-12-11 15:43:50

+3

您可以在同一個命令中實際使用'--dhcp-range ='多次來讓它爲兩個網絡服務。這也可能對原始問題起作用。它可能會根據接口的IP地址推斷正確的IP範圍。 – kichik 2012-12-11 20:11:06

+0

確保配置選項'listen-address = 0.0.0.0'在配置文件中被註釋掉。例如'#listen-address = 0.0.0.0' – 2016-04-24 01:02:24

13

雖然@ kichik的答案可能會奏效,但更好的方法可能是使用localise-queries指令和單個dnsmasq服務器實例。

我會假設你已經配置了DHCP範圍爲不同的接口,並結合dnsmasq那些。

(部分記錄)localise-queries選項添加到您的dnsmasq.conf文件。

# /etc/dnsmasq.conf 
localise-queries 

然後,請確保該文件的一個dnsmasq讀取你的主機(如/etc/hosts)中包含兩個網絡的IP地址的條目,如:

# /etc/hosts 
127.0.0.1  dev-vm 
192.168.1.1 dev-vm 
10.0.0.1  dev-vm 

的替代方法改變/etc/hosts文件在dnsmasq.conf文件,而不是到指定地址:

# /etc/dnsmasq.conf 
localise-queries 
host-record=dev-vm,127.0.0.1 
host-record=dev-vm,192.168.1.1 
host-record=dev-vm,10.0.0.1 

如b A結果超視距情況下,dnsmasq將只能是該接口的針對特定接口上收到的查詢IP地址和子網掩碼相匹配的IP。

按照man page,該執行以下操作:

-y,--localise查詢

返回答案,從/ etc/hosts中的DNS查詢依賴的接口在其上查詢已收到。如果/ etc/hosts中的名稱具有多個與之關聯的地址,並且這些地址中的至少一個與發送查詢的接口位於同一子網上,則只返回該子網上的地址(es) 。這允許服務器在每個接口對應的/ etc/hosts中有多個地址,並且主機將根據它們所連接的網絡來獲取正確的地址。目前該設施僅限於IPv4。

16

在每個參數的開頭添加接口對我來說工作正常。 例子(dnsmasq.conf):

dhcp-host=eth0,00:22:43:4b:18:43,192.168.0.7 
dhcp-host=eth1,00:22:43:4b:18:43,192.168.1.7 

我使用的版本:

$ dnsmasq --version 
Version de Dnsmasq 2.68 Copyright (c) 2000-2013 Simon Kelley 
+1

當使用'dhcp-range'而不是'dhcp-host'時也適用於我。這應該是當前dnsmasq版本的可接受解決方案。 – Dyna 2015-04-06 10:17:56

+0

@Dyna它的作品,但它沒有記錄或我錯過了什麼? – Murmel 2016-03-18 09:05:36

+1

@ user1885518我一直在努力解決同一個問題,直到我在[man page]中找到它(http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html):'The tag「bootp 「被設置爲BOOTP請求,並且名稱是請求到達的接口的名稱的標籤也被設置。所以總是有一個以請求到達的接口命名的標籤。對於多vlan dhcp非常有用。 – GnP 2016-08-01 19:51:02

4

另外,您還可以創建多個配置文件/etc/dnsmasq.d/,一個是你要投放的每個接口DHCP。

例如,如果你有一個名爲wlan0wlan1兩個無線接口,並且要服務於他們DHCP感謝的dnsmasq,您可以/etc/dnsmasq.d/下創建兩個文件來配置每個接口:

/etc/dnsmasq.d/dnsmasq-wlan0.conf

interface=wlan0   # Use interface wlan0 
listen-address=10.0.0.1 # Explicitly specify the address to listen on 
bind-interfaces   # Bind to the interface to make sure we aren't sending things elsewhere 
server=8.8.8.8   # Forward DNS requests to Google DNS 
domain-needed   # Don't forward short names 
bogus-priv    # Never forward addresses in the non-routed address spaces. 
dhcp-range=10.0.0.50,10.0.0.150,12h # Assign IP addresses between 10.0.0.50 and 10.0.0.150 with a 12 hour lease time 

/etc/dnsmasq.d/dnsmasq-wlan1.conf

interface=wlan1   # Use interface wlan0 
listen-address=20.0.0.1 # Explicitly specify the address to listen on 
bind-interfaces   # Bind to the interface to make sure we aren't sending things elsewhere 
server=8.8.8.8   # Forward DNS requests to Google DNS 
domain-needed   # Don't forward short names 
bogus-priv    # Never forward addresses in the non-routed address spaces. 
dhcp-range=20.0.0.50,20.0.0.150,12h # Assign IP addresses between 20.0.0.50 and 20.0.0.150 with a 12 hour lease time 

對我牛逼帽子是配置系統的一種非常乾淨的方式,並且配置在重新啓動之間保持不變。

+0

你能幫我解決類似的情況嗎?我無法破解解決方案。 http://serverfault.com/questions/825601/dnsmasq-config-for-captive-portal – 55597 2017-02-15 14:14:00

+0

這是行不通的,因爲dnsmasq只是按順序讀取這些文件,並且/etc/dnsmasq.d/中的所有文件都被認爲是單一配置。 – Dingo 2017-04-20 01:00:38

+0

@MariusMatutiae同意,一個非常好的答案 – KGCybeX 2017-07-04 18:50:08