As has been mentioned in previous answers, a solution to this problem is to add a macvlan network adapter on the host. However, I felt that manually rewiring the routes to the macvlan adapter was kinda hacky, especially since I wanted IPv6 support and manually set routes may become a problem when the prefix changes. So here's my configuration which leaves the kernel in control of the routing table:
(The particular configuration here is Debian and Upstart specific, but the basic steps should work on any GNU/Linux.)
Creating the macvlan adapter on boot
First, you need to select a MAC address for your adapter. You may be able to just use a random one, but I suggest you manually create a macvlan adapter and use its MAC. This way, the MAC obliges to any conventions that might be there.
Setting a fixed MAC is advisable, since otherwise there is no way for e.g. an DHCP server to recognize your machine after a reboot and assign it the same IP address as before.
So create an adapter and look up the MAC:
root@host:~# ip link add link eth0 macvlan0 type macvlan mode bridge
root@host:~# ip addr show dev macvlan0
#: macvlan0@eth0: mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1
link/ether 12:34:56:78:90:ab brd ff:ff:ff:ff:ff:ff
...
The highlighted hexadecimal number is your MAC address.
Now you create an init script - which must be run before the networking is initalized - to create the macvlan adapter each startup. The command to do this is:
ip link add link eth0 address <MACADDRESS> macvlan0 type macvlan mode bridge
An example Upstart init script for this purpose would be:
start on starting networking
script
ip link add link eth0 address <MACADDRESS> macvlan0 type macvlan mode bridge
end script
Just put this in e.g. /etc/init/macvlan.conf
.
Setting up the network configuration
In /etc/network/interfaces
, set your physical network adapter to manual (but leave it auto) and move its previous configuration (typically DHCP or a static IP address) to your macvlan adapter. E.g.:
auto eth0
iface eth0 inet manual
auto macvlan0
iface macvlan0 inet dhcp
Disabling IPv6 for the physical adapter
Lastly, you do not want the physical adapter to obtain an IP address. For IPv4 setting the adapter to manual prevents it from obtaining an address. However, I have not found a configuration which stops the kernel from obtaining/assigning an IPv6 address for/to the adapter. When it does, it also adds routes for them, which may cause problems. So the best way seems to be to disable IPv6 for the physical adapter. You can do this by adding the line
net.ipv6.conf.eth0.disable_ipv6=1
to /etc/sysctl.conf
, by creating a file in /etc/sysctl.d/
with this line, or by adding
sysctl -w net.ipv6.conf.eth0.disable_ipv6=1
to your init script.
When you now reboot your machine, communicating from host to guest should work with both IPv4 and IPv6.
Be aware, that if you make a mistake while setting this up, your host may become unreachable via network even after a reboot. Only do this if you have physical access to the machine or other safeguards are in place so you can fix potential problems.