![VirtualBox - 具有互联网的“仅限主机”](https://linux22.com/image/115539/VirtualBox%20-%20%E5%85%B7%E6%9C%89%E4%BA%92%E8%81%94%E7%BD%91%E7%9A%84%E2%80%9C%E4%BB%85%E9%99%90%E4%B8%BB%E6%9C%BA%E2%80%9D.png)
好吧朋友们!这次我真的很感激一些帮助! =|
我在用着虚拟盒子虚拟器,我正在尝试让我的来宾机器可以使用以下方式访问互联网仅有的“仅主机”网络接口(VirtualBox)。
根据官方文档https://www.virtualbox.org/manual/ch06.html使用“仅主机”网络接口我有以下“网络模式”(访问)...
VM <-> Host | YES
VM1 <-> VM2 | YES
VM -> Internet | NO
然而, 有很多互联网上的文档通知您可以在主机上使用以下“技巧”/“解决方法”(示例)从访客计算机访问互联网(仅使用“仅主机”接口)...
sudo iptables -A FORWARD -o <HOST_INTERFACE_WITH_INTERNET> -i vboxnet0 -s 192.168.56.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A POSTROUTING -t nat -j MASQUERADE
sudo sysctl -w net.ipv4.ip_forward=1
我测试了很多很多的例子。这iptables.service
在我的主机上运行正常我究竟做错了什么?
我的主机是 Linux Manjaro(基于 Linux Arch)。
多谢!
以下是一些有关我提到的“技巧”/“解决方法”信息的来源......
http://archlinux.org.ru/forum/topic/2219/ http://nerdbynature.de/s9y/2015/06/09/VirtualBox-switching-to-Host-only-networking https://askubuntu.com/questions/293816/in-virtualbox-how-do-i-set-up-host-only-virtual-machines-that-can-access-the-in https://cuckoo.sh/docs/installation/host/routing.html https://downloads.cuckoosandbox.org/docs/installation/guest/network.html https://github.com/cuckoosandbox/cuckoo/issues/1376 https://jackal777.wordpress.com/2012/02/13/internet-access-in-virtualbox-host-only-networking/ https://kyrofa.com/posts/virtualbox-internet-access-with-host-only-network https:// precisionsec.com/virtualbox-host-only-network-cuckoo-sandbox-0-4-2/ https://superuser.com/questions/1223801/virtualbox-nat-and-host-only-connection/1223853 Virtualbox NAT + 仅主机适配器 https://www.howtogeek.com/howto/4922/week-in-geek-the-fixing-slow-internet-in-virtualbox-xp-guest-edition/ https://www.rffuste.com/tag/cuckoo/ https://www.virtualbox.org/manual/ch06.html
以下是一些许多我测试过的例子...
> --------------------------------------------
sudo iptables -t nat -I POSTROUTING -s 192.168.56.0/24 -j MASQUERADE
sudo sysctl net.ipv4.ip_forward=1
sudo iptables -P FORWARD ACCEPT
sudo iptables -t nat -P POSTROUTING ACCEPT
< --------------------------------------------
> --------------------------------------------
sudo iptables -A FORWARD -i vboxnet0 -s 192.168.56.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A POSTROUTING -t nat -j MASQUERADE
sudo sysctl -w net.ipv4.ip_forward=1
< --------------------------------------------
> --------------------------------------------
sudo iptables -A FORWARD -o <HOST_INTERFACE_WITH_INTERNET> -i vboxnet0 -s 192.168.56.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A POSTROUTING -t nat -j MASQUERADE
sudo sysctl -w net.ipv4.ip_forward=1
< --------------------------------------------
> --------------------------------------------
sudo iptables -A FORWARD -o <HOST_INTERFACE_WITH_INTERNET> -i vboxnet0 -s 192.168.56.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -o <HOST_INTERFACE_WITH_INTERNET> -j MASQUERADE
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
< --------------------------------------------
> --------------------------------------------
sudo iptables -A FORWARD -o <HOST_INTERFACE_WITH_INTERNET> -i vboxnet0 -s 192.168.56.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A POSTROUTING -t nat -j MASQUERADE
sudo sysctl -w net.ipv4.ip_forward=1
sudo sysctl -w net.ipv4.conf.all.proxy_arp=1
< --------------------------------------------
> --------------------------------------------
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -F
sudo iptables -t nat -A POSTROUTING -o <HOST_INTERFACE_WITH_INTERNET> -j MASQUERADE
sudo iptables -A FORWARD -i <HOST_INTERFACE_WITH_INTERNET> -o vboxnet0 -j ACCEPT
sudo iptables -A FORWARD -i vboxnet0 -o <HOST_INTERFACE_WITH_INTERNET> -j ACCEPT
< --------------------------------------------
> --------------------------------------------
sudo iptables -A FORWARD -i vboxnet0 -s 192.168.56.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A POSTROUTING -t nat -j MASQUERADE
sudo sysctl -w net.ipv4.ip_forward=1
< --------------------------------------------
> --------------------------------------------
sudo bash -c "printf \"net.ipv4.ip_forward = 1\nnet.ipv4.conf.all.proxy_arp = 1\n\" >> /etc/sysctl.conf"
sudo sysctl -p
sudo iptables -t filter -I FORWARD --in-interface vboxnet0 --out-interface <HOST_INTERFACE_WITH_INTERNET> --source 192.168.56.0/24 -j ACCEPT
sudo iptables -t filter -I FORWARD --in-interface <HOST_INTERFACE_WITH_INTERNET> --out-interface vboxnet0 --destination 192.168.56.0/24 -j ACCEPT
sudo iptables -t nat -I POSTROUTING -o <HOST_INTERFACE_WITH_INTERNET> -j MASQUERADE
< --------------------------------------------
> --------------------------------------------
sudo iptables -A POSTROUTING -o enp+ -t nat -j MASQUERADE
sudo iptables -A POSTROUTING -o wlp+ -t nat -j MASQUERADE
< --------------------------------------------
> --------------------------------------------
sudo iptables -A PREROUTING -t mangle -i vboxnet+ -j MARK --set-mark 1
sudo iptables -A POSTROUTING -o enp+ -t nat -m mark --mark 1 -j MASQUERADE
sudo iptables -A POSTROUTING -o wlp+ -t nat -m mark --mark 1 -j MASQUERADE
< --------------------------------------------
> --------------------------------------------
sudo iptables -t nat -A POSTROUTING -o <HOST_INTERFACE_WITH_INTERNET> -s 192.168.56.0/24 -j MASQUERADE
sudo iptables -P FORWARD DROP
sudo iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -s 192.168.56.0/24 -j ACCEPT
sudo iptables -A FORWARD -s 192.168.56.0/24 -d 192.168.56.0/24 -j ACCEPT
< --------------------------------------------
> --------------------------------------------
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -A FORWARD -o <HOST_INTERFACE_WITH_INTERNET> -i vboxnet0 -s 192.168.56.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -t nat -A POSTROUTING -s 192.168.56.0/24 -o <HOST_INTERFACE_WITH_INTERNET> -j MASQUERADE
sudo iptables -I INPUT 1 -i vboxnet0 -j ACCEPT
< --------------------------------------------
> --------------------------------------------
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
sudo sysctl -p /etc/sysctl.conf
sudo iptables -I FORWARD -i vboxnet0 -d 192.168.56.0/255.255.255.0 -j DROP
sudo iptables -A FORWARD -i vboxnet0 -s 192.168.56.0/255.255.255.0 -j ACCEPT
sudo iptables -A FORWARD -i <HOST_INTERFACE_WITH_INTERNET> -d 192.168.56.0/255.255.255.0 -j ACCEPT
sudo iptables -t nat -A POSTROUTING -o <HOST_INTERFACE_WITH_INTERNET> -j MASQUERADE
< --------------------------------------------
答案1
- 介绍:
这是使用 VirtualBox 上的单个网络接口(“仅主机”)在来宾上访问“VM <-> Host”、“VM1 <-> VM2”和“VM -> Internet”的完整指南。
重要的:以“root”身份运行所有命令。
- 执行于 主持人 :
笔记:我们使用 Manjaro(基于 Arch)主机作为模板。您可能需要对其他发行版进行调整和更改。
您需要复制 iptables 模板配置文件...
cp /etc/iptables/empty.rules /etc/iptables/iptables.rules
...这样你就可以启动“iptables.service”。
启用并启动“iptables.service”...
systemctl enable iptables.service
systemctl start iptables.service
启用 IP 转发...
sysctl -w net.ipv4.ip_forward=1
printf "net.ipv4.ip_forward=1\n" >> /etc/sysctl.d/30-ipforward.conf
添加以下 iptables 规则。这将通过主机(“vboxnet0”)将数据包转发到互联网......
模板一:
iptables -t filter -I FORWARD --in-interface vboxnet0 --out-interface <HOST_INTERFACE_WITH_INTERNET> --source 192.168.56.0/24 -j ACCEPT
iptables -t filter -I FORWARD --in-interface <HOST_INTERFACE_WITH_INTERNET> --out-interface vboxnet0 --destination 192.168.56.0/24 -j ACCEPT
iptables -t nat -I POSTROUTING -o <HOST_INTERFACE_WITH_INTERNET> -j MASQUERADE
...或者添加以下 iptables 规则...
模板二:
iptables -t nat -I POSTROUTING -s 192.168.56.0/24 -j MASQUERADE
iptables -P FORWARD ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
笔记:在“TEMPLATE II”上,您不需要告知主机接口的名称 ( <HOST_INTERFACE_WITH_INTERNET>
) 和 VirtualBox 接口的名称 ( vboxnet0
)。这样,任何具有互联网的主机接口都可以工作,也就是说,每当有互联网的接口发生变化时,我都不必调整它的名称。一个例子是当我们从有线接口(例如:enp4s0f2)更改为无线接口(例如:wlp3s0)时,反之亦然。
进一步的问题:我提出了两种配置“iptables”的方法,因为我不知道使用“TEMPLATE I”是否有任何优势。任何意见?
提示:要查找具有 Internet 的网络接口 ( ) 的名称,<HOST_INTERFACE_WITH_INTERNET>
请使用“ip a”命令。
将规则保存到 iptables 配置并重新启动服务...
iptables-save > /etc/iptables/iptables.rules
systemctl restart iptables.service
在主机中启用并启动“dnsmasq”...
systemctl enable dnsmasq.service
systemctl start dnsmasq.service
笔记:“dnsmasq”是一个小型缓存 DNS 代理和 DHCP/TFTP 服务器。
- 执行于 客人 :
笔记:我们使用 CentOS 7 guest 作为模板。您可能需要对其他发行版进行调整和更改。
根据型号配置网络接口...
笔记:网络配置文件位于“/etc/sysconfig/network-scripts/”文件夹路径中。
BOOTPROTO=static
DEVICE=<NETWORK_INTERFACE_NAME>
DNS1=<HOST-ONLY_HOST_IP>
GATEWAY=<HOST-ONLY_HOST_IP>
IPADDR=<HOST-ONLY_GUEST_IP>
IPV6INIT=NO
NETMASK=255.255.255.0
NM_CONTROLLED=yes
ONBOOT=yes
TYPE=Ethernet
USERCTL=NO
ZONE=
例如。:
BOOTPROTO=static
DEVICE=eno16777736
DNS1=192.168.56.1
GATEWAY=192.168.56.1
IPADDR=192.168.56.101
IPV6INIT=NO
NETMASK=255.255.255.0
NM_CONTROLLED=yes
ONBOOT=yes
TYPE=Ethernet
USERCTL=NO
ZONE=
重新启动网络服务...
systemctl restart network.service
去测试...
curl http://www.google.com
这就是大家! =D
- 参考:
https://jackal777.wordpress.com/2012/02/13/internet-access-in-virtualbox-host-only-networking/ https://askubuntu.com/questions/293816/in-virtualbox-how-do-i-set-up-host-only-virtual-machines-that-can-access-the-in https://kyrofa.com/posts/virtualbox-internet-access-with-host-only-network http://archlinux.org.ru/forum/topic/2219/ https://wiki.archlinux.org/index.php/Iptables https://wiki.archlinux.org/index.php/Internet_sharing