macos Mac OS X 中的虚拟网络接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/87442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Virtual network interface in Mac OS X
提问by Hans Doggen
I know that you can make a virtual network interface in Windows (see here), and in Linux it is also pretty easy with ip-aliases, but does something similar exist for Mac OS X? I've been looking for loopback adapters, virtual interfaces and couldn't find a good solution.
我知道您可以在 Windows 中创建一个虚拟网络接口(请参阅此处),在 Linux 中使用 ip-aliases 也很容易,但是 Mac OS X 是否存在类似的东西?我一直在寻找环回适配器、虚拟接口,但找不到好的解决方案。
You can create a new interface in the networking panel, based on an existing interface, but it will not act as a real fully functional interface (if the original interface is inactive, then the derived one is also inactive).
您可以在网络面板中创建一个基于现有接口的新接口,但它不会充当真正的全功能接口(如果原始接口处于非活动状态,则派生接口也处于非活动状态)。
This scenario is needed when working in a completely disconnected situation. Even then, it makes sense to have networking capabilities when running servers in a VMWare installation. Those virtual machines can be reached by their IP address, but not by their DNS name, even if I run a DNS server in one of those virtual machines. By configuring an interface to use the virtual DNS server, I thought I could test some DNS scenario's. Unfortunately, no interface is resolving DNS names if none of them are inactive...
在完全断开连接的情况下工作时需要此方案。即便如此,在 VMWare 安装中运行服务器时具有网络功能也是有意义的。这些虚拟机可以通过它们的 IP 地址访问,但不能通过它们的 DNS 名称访问,即使我在其中一台虚拟机中运行 DNS 服务器也是如此。通过配置一个接口来使用虚拟 DNS 服务器,我想我可以测试一些 DNS 场景。不幸的是,如果没有一个接口处于非活动状态,则没有接口解析 DNS 名称......
回答by
The loopback adapter is always up.
环回适配器始终处于运行状态。
ifconfig lo0 alias 172.16.123.1will add an alias IP 172.16.123.1 to the loopback adapter
ifconfig lo0 alias 172.16.123.1会将别名 IP 172.16.123.1 添加到环回适配器
ifconfig lo0 -alias 172.16.123.1will remove it
ifconfig lo0 -alias 172.16.123.1将删除它
回答by bmasterswizzle
Replying in particular to:
特别回复:
You can create a new interface in the networking panel, based on an existing interface, but it will not act as a real fully functional interface (if the original interface is inactive, then the derived one is also inactive).
您可以在网络面板中创建一个基于现有接口的新接口,但它不会充当真正的全功能接口(如果原始接口处于非活动状态,则派生接口也处于非活动状态)。
This can be achieved using a Tun/Tap device as suggested by psv141, and manipulating the /Library/Preferences/SystemConfiguration/preferences.plist
file to add a NetworkService based on either a tun or tap interface. Mac OS X will not allow the creation of a NetworkService based on a virtual network interface, but one can directly manipulate the preferences.plist
file to add the NetworkService by hand. Basically you would open the preferences.plist
file in Xcode (or edit the XML directly, but Xcode is likely to be more fool-proof), and copy the configuration from an existing Ethernet interface. The place to create the new NetworkService is under "NetworkServices", and if your Mac has an Ethernet device the NetworkService profile will also be under this property entry. The Ethernet entry can be copied pretty much verbatim, the only fields you would actually be changing are:
这可以通过使用 psv141 建议的 Tun/Tap 设备来实现,并操作/Library/Preferences/SystemConfiguration/preferences.plist
文件以添加基于 tun 或 tap 接口的 NetworkService。Mac OS X 不允许基于虚拟网络接口创建 NetworkService,但可以直接操作preferences.plist
文件手动添加 NetworkService。基本上,您会preferences.plist
在 Xcode 中打开文件(或直接编辑 XML,但 Xcode 可能更万无一失),然后从现有以太网接口复制配置。创建新 NetworkService 的位置在“NetworkServices”下,如果您的 Mac 有以太网设备,NetworkService 配置文件也将位于此属性条目下。以太网条目几乎可以逐字复制,您实际更改的唯一字段是:
- UUID
- UserDefinedName
- IPv4 configuration and set the interface to your tun or tap device (i.e. tun0 or tap0).
- DNS server if needed.
- 用户名
- 用户定义名称
- IPv4 配置并将接口设置为您的tun 或tap 设备(即tun0 或tap0)。
- 如果需要,DNS 服务器。
Then you would also manipulate the particular Location you want this NetworkService for (remember Mac OS X can configure all network interfaces dependent on your "Location"). The default location UUID can be obtained in the root of the PropertyList as the key "CurrentSet". After figuring out which location (or set) you want, expand the Set property, and add entries under Global/IPv4/ServiceOrder with the UUID of the new NetworkService. Also under the Set property you need to expand the Service property and add the UUID here as a dictionary with one String entry with key __LINK__ and value as the UUID (use the other interfaces as an example).
然后,您还可以操作您希望此 NetworkService 用于的特定位置(请记住,Mac OS X 可以根据您的“位置”配置所有网络接口)。默认位置 UUID 可以在 PropertyList 的根目录中作为键“CurrentSet”获取。确定您想要的位置(或集合)后,展开 Set 属性,并在 Global/IPv4/ServiceOrder 下添加条目,并使用新 NetworkService 的 UUID。同样在 Set 属性下,您需要展开 Service 属性并在此处添加 UUID 作为字典,其中包含一个字符串条目,键为 __LINK__,值为 UUID(以其他接口为例)。
After you have modified your preferences.plist
file, just reboot, and the NetworkService will be available under SystemPreferences->Network. Note that we have mimicked an Ethernet device so Mac OS X layer of networking will note that "a cable is unplugged" and will not let you activate the interface through the GUI. However, since the underlying device is a tun/tap device and it has an IP address, the interface will become active and the proper routing will be added at the BSD level.
修改preferences.plist
文件后,只需重新启动,NetworkService 将在 SystemPreferences->Network 下可用。请注意,我们模拟了以太网设备,因此 Mac OS X 网络层会注意到“电缆已拔出”,并且不会让您通过 GUI 激活界面。然而,由于底层设备是一个 tun/tap 设备并且它有一个 IP 地址,接口将变为活动状态,并且将在 BSD 级别添加正确的路由。
As a reference this is used to do special routing magic.
作为参考,这用于执行特殊的路由魔术。
In case you got this far and are having trouble, you have to create the tun/tap device by opening one of the devices under /dev/. You can use any program to do this, but I'm a fan of good-old-fashioned C myself:
如果您走到这一步并且遇到问题,您必须通过打开 /dev/ 下的设备之一来创建 tun/tap 设备。您可以使用任何程序来执行此操作,但我本人是老式 C 的粉丝:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd = open("/dev/tun0", O_RDONLY);
if (fd < 0)
{
printf("Failed to open tun/tap device. Are you root? Are the drivers installed?\n");
return -1;
}
while (1)
{
sleep(100000);
}
return 0;
}
回答by Alex Gray
In regards to @bmasterswizzle
's BRILLIANT answer- more specifically - to @DanRamos
' question about how to force the new interface's link-state to "up".. I use this script, of whose origin I cannot recall, but which works fabulously (in coordination with @bmasterswizzles "Mona Lisa" of answers)...
关于@bmasterswizzle
' BRILLIANT 的回答- 更具体地说 - @DanRamos
' 关于如何强制新界面的链接状态“向上”的问题。@bmasterswizzles 的“蒙娜丽莎”答案)...
#!/bin/zsh
[[ "$UID" -ne "0" ]] && echo "You must be root. Goodbye..." && exit 1
echo "starting"
exec 4<>/dev/tap0
ifconfig tap0 10.10.10.1 10.10.10.255
ifconfig tap0 up
ping -c1 10.10.10.1
echo "ending"
export PS1="tap interface>"
dd of=/dev/null <&4 & # continuously reads from buffer and dumps to null
I am NOT quite sure I understand the alteration to the prompt at the end, or...
我不太确定我是否理解最后对提示的更改,或者...
dd of=/dev/null <&4 & # continuously reads from buffer and dumps to null
dd of=/dev/null <&4 & # continuously reads from buffer and dumps to null
but WHATEVER. it works. link light: green?. loves it.
但是无所谓。有用。链接灯:绿色?。 喜欢它。
回答by web-online
A few others seemed to hint at this, but the following demonstrates using ifconfig to create a vlan and test DNS on the virtual interface (using minidns) on OS X 10.9.5:
其他一些似乎暗示了这一点,但以下演示了使用 ifconfig在 OS X 10.9.5上的虚拟接口(使用minidns)创建 vlan 和测试 DNS :
$ sw_vers -productVersion
10.9.5
$ sudo ifconfig vlan169 create && echo vlan169 created
vlan169 created
$ sudo ifconfig vlan169 inet 169.254.169.254 netmask 255.255.255.255 && echo vlan169 configured
vlan169 configured
$ sudo ./minidns.py 169.254.169.254 &
[1] 35125
$ miniDNS :: * 60 IN A 169.254.169.254
$ dig @169.254.169.254 +short test.host
Request: test.host. -> 169.254.169.254
Request: test.host. -> 169.254.169.254
169.254.169.254
$ sudo kill 35125
$
[1]+ Exit 143 sudo ./minidns.py 169.254.169.254
$ sudo ifconfig vlan169 destroy && echo vlan169 destroyed
vlan169 destroyed
回答by psv141
It's possible to use TUN/TAP device. http://tuntaposx.sourceforge.net/
可以使用 TUN/TAP 设备。 http://tuntaposx.sourceforge.net/
回答by Mecki
What do you mean by
你是什么意思
"but it will not act as a real fully functional interface (if the original interface is inactive, then the derived one is also inactive"
“但它不会充当真正的全功能界面(如果原始界面处于非活动状态,那么派生的界面也处于非活动状态”
?
?
I can make a new interface, base it on an already existing one, then disable the existing one and the new one still works. Making a second interface does however not create a real interface (when you check with ifconfig), it will just assign a second IP to the already existing one (however, this one can be DHCP while the first one is hard coded for example).
我可以创建一个新的界面,以现有的界面为基础,然后禁用现有的界面,新界面仍然有效。然而,创建第二个接口并不会创建一个真正的接口(当您检查 ifconfig 时),它只会为已经存在的 IP 分配第二个 IP(但是,这个 IP 可以是 DHCP,而第一个是硬编码的,例如)。
So did I understand you right, that you want to create an interface, not bound to any real interface? How would this interface then be used? E.g. if you disconnect all WLAN and pull all network cables, where would this interface send traffic to, if you send traffic to it? Maybe your question is a bit unclear, it might help a lot if rephrase it, so it's clear what you are actually trying to do with this "virtual interface" once you have it.
所以我理解你的意思,你想创建一个接口,而不是绑定到任何真正的接口?那么这个接口将如何使用呢?例如,如果您断开所有 WLAN 并拔掉所有网线,如果您向该接口发送流量,该接口会将流量发送到哪里?也许你的问题有点不清楚,如果重新表述它可能会有很大帮助,所以一旦你有了这个“虚拟界面”,你实际上试图用它做什么就很清楚了。
As you mentioned "alias IP" in your question, this would mean an alias interface. But an alias interface is always bound to a real interface. The difference is in Linux such an interface really ISan interface (e.g. an alias interface for eth0 could be eth1), while on Mac, no realinterface is created, instead a virtual interface is created, that can configured and used independently, but it is still the same interface physically and thus no new named interface is generated (you just have two interfaces, that are both in fact en0, but both can be enabled/disabled and configured independently).
正如您在问题中提到的“别名 IP”,这意味着别名接口。但是别名接口始终绑定到真实接口。不同的是在Linux中这样的接口实际上是一个接口(例如eth0的别名接口可以是eth1),而在Mac上,没有创建真正的接口,而是创建了一个虚拟接口,可以独立配置和使用,但它物理上仍然是相同的接口,因此不会生成新的命名接口(您只有两个接口,它们实际上都是 en0,但两者都可以独立启用/禁用和配置)。
回答by Ariel Monaco
Take a look at this tutorial, it's for FreeBSD but also applies to OS X. http://people.freebsd.org/~arved/vlan/vlan_en.html
看看这个教程,它适用于 FreeBSD 但也适用于 OS X。http://people.freebsd.org/~arved/vlan/vlan_en.html
回答by arvin_v_s
if you are on a dev environment and want access some service already running on localhost/host machine. in docker for mac you have another option.use docker.for.mac.localhost instead of localhost in docker container. docker.for.mac.host.internal should be used instead of docker.for.mac.localhost from Docker Community Edition 17.12.0-ce-mac46 2018-01-09. this allows you to connect to service running on your on mac from within a docker container.please refer below links
如果您在开发环境中并且想要访问一些已经在本地主机/主机上运行的服务。在 docker for mac 你有另一个选项。在 docker 容器中使用 docker.for.mac.localhost 而不是 localhost。应该使用 docker.for.mac.host.internal 而不是 Docker Community Edition 17.12.0-ce-mac46 2018-01-09 中的 docker.for.mac.localhost。这允许您从 docker 容器内连接到在 Mac 上运行的服务。请参考以下链接
understanding the docker.for.mac.localhost behavior
回答by Henry 3 Dogg
Go to Network Preferences.
转到网络首选项。
At the bottom of the list of network adapters, click the +icons
在网络适配器列表的底部,单击+图标
Select the existing interface that you want to arp (say Ethernet 1), and give the Service Name that you want for the new port (say Ethernet 1.1) then press create.
选择要进行 arp 的现有接口(例如以太网 1),并为新端口(例如以太网 1.1)提供所需的服务名称,然后按创建。
Now you have the new virtual interface in the gui and can manage IP addresses etc it in the normal way.
现在您在 gui 中拥有了新的虚拟接口,并且可以以正常方式管理 IP 地址等。
ifconfig -a will confirm that you have multiple IPs on the interface, and these will still be there when you reboot.
ifconfig -a 将确认您在接口上有多个 IP,并且在您重新启动时这些 IP 仍然存在。
Its a Mac. Don't fight it, do it the easy way.
它是一台 Mac。不要打架,用简单的方法做。
回答by Alex Gray
i have resorted to running PFSense, a BSD based router/firewall to achieve this goal….
我已经求助于运行 PFSense,一个基于 BSD 的路由器/防火墙来实现这个目标......
why? because OS X Server gets so FREAKY without a Static IP…
为什么?因为 OS X Server 在没有静态 IP 的情况下变得如此怪异......
so after wrestling with it for DAYS to make NAT and DHCP and firewall and …
所以在与它搏斗了几天之后制作 NAT 和 DHCP 以及防火墙和......
I'm trying this is parallels…
我正在尝试这是相似的......
will let ya know how it goes...
会让你知道它是怎么回事...