dnsmasq,根据使用的接口提供不同的ip地址

dnsmasq,根据使用的接口提供不同的ip地址,dns,virtual-machine,dnsmasq,Dns,Virtual Machine,Dnsmasq,基本上我的情况是,我正在运行一个虚拟机来开发网站 主机的dns指向运行dnsmasq的VM,该VM解析各种开发站点的地址;i、 e.test.mysite.vm等 问题是,当我从工作网络转到家庭网络时,由于虚拟机的IP发生了变化,所有这些都会中断。是否可以根据请求来自哪个接口提供不同的IP地址?或者我应该尝试用一种完全不同的方式来解决这个问题 谢谢你的帮助 事实证明,有一种更简单的方法来解决这个问题…… 我现在在VM上设置了2个接口,不需要使用dnsmasq 第一个只是一个桥接/共享接口,它允

基本上我的情况是,我正在运行一个虚拟机来开发网站

主机的dns指向运行dnsmasq的VM,该VM解析各种开发站点的地址;i、 e.test.mysite.vm等

问题是,当我从工作网络转到家庭网络时,由于虚拟机的IP发生了变化,所有这些都会中断。是否可以根据请求来自哪个接口提供不同的IP地址?或者我应该尝试用一种完全不同的方式来解决这个问题

谢谢你的帮助


事实证明,有一种更简单的方法来解决这个问题……

我现在在VM上设置了2个接口,不需要使用dnsmasq

第一个只是一个桥接/共享接口,它允许VM使用主机可用的任何internet连接,每次我移动office时都会重新启动网络


第二个是到我的VM主机的私有连接,它有一个静态IP地址。这是我用来连接和绑定任何服务(如nginx、mysql等)的接口。

您可以运行两个
dnsmasq
实例,每个实例都有一个不同的监听接口。您可以使用
--interface=X
--bind interfaces
选项进行此操作。默认情况下,它还绑定环回设备
lo
,如果两个进程尝试绑定它,它将失败。使用
--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


正如我在评论中提到的,我不太确定这对您的情况有何帮助,但它可能会帮助任何试图在两个不同接口上获取两个不同地址范围的人。

尽管@kichik的答案可能很有效,实现这一点的更优雅的方法可能是使用
localisequerys
指令和单个
dnsmasq
服务器实例

我假设您已经为不同的接口配置了DHCP范围,并将
dnsmasq
绑定到这些接口

将(部分记录的)
本地化查询
选项添加到
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
因此,在这两种情况下,
dnsmasq
将仅为在该特定接口上接收到的查询提供与接口的IP和网络掩码匹配的IP

根据,这将执行以下操作:

-y、 --本地化查询

返回来自/etc/hosts的DNS查询的答案,这些主机取决于接收查询的接口。如果/etc/hosts中的名称具有多个与其关联的地址,并且其中至少有一个地址与向其发送查询的接口位于同一子网上,则只返回该子网上的地址。这允许服务器在/etc/hosts中有与其每个接口对应的多个地址,并且主机将根据其连接到的网络获得正确的地址。目前,此功能仅限于IPv4


在每个参数的开头添加接口对我来说很好。 示例(在dnsmasq.conf中):

我正在使用版本:

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

或者,您也可以在
/etc/dnsmasq.d/
下创建多个配置文件,每个接口对应一个配置文件

例如,如果您有两个名为
wlan0
wlan1
的无线接口,并且您希望借助dnsmasq为它们提供dhcp服务,则可以在
/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
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
/etc/dnsmasq.d/dnsmasq-wlan1.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
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

对我来说,这是一种非常干净的方式来配置您的系统,并在重启之间保持配置。

不同的接口在哪里发挥作用?您的家庭网络和工作网络是否使用两种不同的接口?为什么虚拟机的IP会发生变化?是的,家庭(192.168.0.*)和工作(10.0.0.*)有两个不同的接口。变化在工作IP和家庭IP之间。如果不运行多个实例,是否真的无法拥有两个网络?这真的让事情复杂化了。实际上,您可以在同一个命令中多次使用
--dhcp range=
,使其服务于两个网络。这可能也适用于原始问题。它可能会根据接口的IP地址扣除要服务的正确IP范围。请确保配置文件中的配置选项
listen address=0.0.0.0
被注释掉。e、 g.
#listen address=0.0.0.0
如果您的一个接口由外部DHCP服务器(如您的ISP)提供服务,另一种选择可能是使用
本地化查询
选项。有关详细信息,请参阅。很好的解决方案,但是,如何在引导时启用这两个实例?在使用
dhcp范围
而不是
dhcp主机
时,对我也适用。这应该是当前dnsmasq版本可接受的解决方案。@Dyna它可以工作,但没有记录或者我遗漏了什么吗?@user1885518我一直在努力解决同一个问题,直到我在中发现:
标记“bootp”是为bootp请求设置的,并且还设置了一个标签,其名称是请求到达的接口的名称。
。因此,总是有一个以请求到达的接口命名的标记。对于多vlan dhcp非常有用。使用单个dnsmasq实例为多个dhcp客户端提供服务的最佳解决方案。您能帮我解决类似的问题吗?我打不开电话