When using VirtualBox on a Mac (over WiFi?) this requires a different approach because of how WiFi bridging works (https://docs.oracle.com/en/virtualization/virtualbox/6.0/user/network_bridged.html).
The key in this setup is not to bridge eth0
and don't use lxc-net
. On the host, /etc/network/interfaces is standard:
auto eth0
iface eth0 inet static
address 192.168.0.8
netmask 255.255.255.0
gateway 192.168.0.1
A bridge is not needed (no lxc-net
) but set the container config to create a virtual interface thusly:
lxc.net.0.type = veth
lxc.net.0.veth.pair = veth0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:xx:xx:xx
lxc.net.0.ipv4.address = 192.168.0.64/32
lxc.net.0.ipv4.gateway = 192.168.0.8
lxc.net.0.script.up = /var/lib/lxc/netup.sh 192.168.0.64
lxc.net.0.script.down = /var/lib/lxc/netdown.sh 192.168.0.64
Some notes on this config: (1) there is no lxc.net.0.link
since we don't want a bridge, (2) the lxc.net.0.ipv4.gateway
address is the host's IP address, (3) note the netmask is /32
, (4) the scripts are explained below.
The netup.sh
script routes incoming IP traffic to the container and creates an ARP entry so that eth0
will accept traffic for it:
#!/bin/sh
ip route add ${1}/32 dev veth0
arp -i eth0 -Ds ${1} eth0 pub
The netdown.sh
script simply removes the ARP entry (the IP route will go away automatically when veth0
is destroyed).
#!/bin/sh
arp -d -i eth0 ${1} pub
On the guest, /etc/network/interfaces can be empty, since in this case the setup was done in the container config file.
The end result on the host:
# ip route
default via 192.168.0.1 dev eth0 metric 202
192.168.0.0/24 dev eth0 scope link src 192.168.0.8
192.168.0.64 dev veth0 scope link
# arp -a
...
? (192.168.0.64) at 00:16:3e:xx:xx:xx [ether] on veth0
? (192.168.0.64) at * PERM PUP on eth0
# ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:xx:xx:xx
inet addr:192.168.0.8 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:11476 errors:0 dropped:0 overruns:0 frame:0
TX packets:10425 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1253928 (1.1 MiB) TX bytes:1328460 (1.2 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:1 errors:0 dropped:0 overruns:0 frame:0
TX packets:1 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:112 (112.0 B) TX bytes:112 (112.0 B)
veth0 Link encap:Ethernet HWaddr FE:9D:3D:14:4B:87
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:20 errors:0 dropped:0 overruns:0 frame:0
TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1748 (1.7 KiB) TX bytes:1748 (1.7 KiB)
And in the container:
# ip route
default via 192.168.0.8 dev eth0
192.168.0.8 dev eth0 scope link
# ifconfig
eth0 Link encap:Ethernet HWaddr 00:16:3E:xx:xx:xx
inet addr:192.168.0.64 Bcast:255.255.255.255 Mask:255.255.255.255
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:247 errors:0 dropped:0 overruns:0 frame:0
TX packets:247 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:23322 (22.7 KiB) TX bytes:23322 (22.7 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
# ping -c 3 1.1.1.1
PING 1.1.1.1 (1.1.1.1): 56 data bytes
64 bytes from 1.1.1.1: seq=0 ttl=58 time=43.458 ms
64 bytes from 1.1.1.1: seq=1 ttl=58 time=41.121 ms
64 bytes from 1.1.1.1: seq=2 ttl=58 time=40.891 ms
--- 1.1.1.1 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 40.891/41.823/43.458 ms
I know this was stated in the question, but for anyone starting from scratch, make sure forwarding is enabled, echo 1 > /proc/sys/net/ipv4/ip_forward ; echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
.
My other answer works for KVM and might be useful for others so I won't edit it, but this one is more specific to VirtualBox and WiFi.