0

I have a Debian 10 server which gets its IPv4-v6 via DHCP which can be configured as static also:

root@host:~# ifconfig
ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 45.76.XX.XX  netmask 255.255.254.0  broadcast 45.76.XX.255
        inet6 2001:19f0:XXXX:XXX:XXXX:XXX:XXXX:XXXX  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::5400:2ff:fecf:999b  prefixlen 64  scopeid 0x20<link>
        ether 56:00:XX:XX:XX:XX  txqueuelen 1000  (Ethernet)
        RX packets 113  bytes 14499 (14.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 114  bytes 12413 (12.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 24  bytes 1896 (1.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24  bytes 1896 (1.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

I know how to configure nat-ed LXC instances with private IPv4 address. But in this fresh system I wish to create a LXC instance assigning it the main public IPv4 address of the host. Is it even possible? I can SSH into host using its IPv6 when its IPv4 is taken away from it and assigned to the container.

Thanks in advance for all your help.

3
  • 1
    Please get used to ip addr for managing network interfaces on Linux... it's not because 'ifconfig' and 'route' are merely old, it's because they can outright lie to you -- e.g. 'ifconfig' doesn't know that an interface can have more than one IPv4 address, and 'route' doesn't know that a route can have more than one gateway. (In your case, a very useful feature would be having an IPv4 default route with an IPv6 nexthop, which is also not understood by those tools.) Commented Jun 5, 2020 at 8:24
  • @user1686 Thanks :) Force of habit. Trying to change it.
    – Sam
    Commented Jun 5, 2020 at 8:30
  • If you perhaps have to use a specific MAC address, I recommend ipvlan.
    – Daniel B
    Commented Jun 5, 2020 at 10:14

1 Answer 1

2

It's easily possible if your LXC networking is using a layer-2 mode such as bridge or macvlan. That puts the container's namespace on the same layer-2 network as the host, and you can do the rest in the most obvious way: remove the IP address from the host and add it to the container. (If the address has to be assigned using DHCP, then the container can just run a DHCP client.)

(This however requires the container's MAC address or DHCP Client ID to be known by the local network's DHCP server. If this is a problem then you might need to assign the host a custom MAC address, and give the container the host's MAC.)


For example, if you want to use bridge mode, first create a standard Linux bridge – in this example named br0 – with your Ethernet interface as a member. Making it permanent is very distro-dependent (using ifcfg or /etc/network/interfaces or systemd-networkd), but temporary configuration can be done using:

ip link add br0 type bridge
ip link set br0 up
ip link set ens3 master br0

Note: If you only have remote access to the host server, then it's a very good idea to use KVM or serial console when making such changes.

Then your LXC configuration might look like this:

lxc.net.1.type = veth
lxc.net.1.flags = up
lxc.net.1.link = br0
lxc.net.1.hwaddr = 02:00:xx:yy:zz:tt

If static IP configuration is wanted:

lxc.net.1.ipv4.address = 45.76.XX.XX/23
lxc.net.1.ipv4.gateway = 45.76.YY.YY
lxc.net.1.ipv6.address = 2001:19f0:X:X:X:X:X:X/64
lxc.net.1.ipv6.gateway = 2001:19f0:X:X::1

If you want to use DHCP and/or SLAAC, then just leave out the address/gateway settings and install a standard DHCP client on the container.

Once you start the container, ip link ls master br0 and bridge link will show a second bridge port.

Note: I've heard claims that macvlan mode provides better performance, but I don't actually know how to configure it. Plus, it seems to require the host to also use a macvlan in order to be able to communicate with its own guests.


If you must use a routed mode, it's still doable in mostly the same way, but the container will not be able to use DHCP, and the host will additionally need to run Proxy-ARP to pretend that the IP address is still "on link" despite it being assigned to another system. (Routers don't forward ARP requests.) If DHCP is mandatory, then a special DHCP client configuration might be necessary for the host to acquire the address without actually assigning it to an interface.

10
  • Thanks @user1686 for your reply. Can you please provide the steps/code/pseudo-code for achieving this using layer-2 mode on this fresh system. Nothing has been changed/installed yet.
    – Sam
    Commented Jun 5, 2020 at 8:29
  • Those steps really helped. Thank you so much.
    – Sam
    Commented Jun 5, 2020 at 9:36
  • The static configuration for the container is working fine. But when I leave that IP/gateway lines commented out. DHCP isn't pulling any IPv4 address inside the container. My hosts's /etc/network/interfaces file in like this: ` auto lo iface lo inet loopback #allow-hotplug ens3 #iface ens3 inet dhcp #iface ens3 inet6 auto auto br0 iface br0 inet manual bridge_ports ens3 bridge_fd 0 bridge_maxwait 0 `
    – Sam
    Commented Jun 5, 2020 at 9:46
  • Which DHCP client are you using inside the container? Is it running? Does it get stuck after sending out the "DHCPDISCOVER" or does it report a different problem? And is the local DHCP server managed by you or is it managed by a hosting company? (My first guess is that the problem is related to the container having its own MAC address, and that might need arrangement with the network admin.) Commented Jun 5, 2020 at 9:48
  • I did not install any DHCP server specifically inside the guest OS. It came along with the debian 9 guest template (isc-dhcp-client 4.3.5-3+deb9u1). Guest OS's interfaces file is: auto eth0 iface eth0 inet dhcp DHCP server is of host (Vultr). Also please note that I have: "iface br0 inet manual" in the host's interfaces file so that it doesn't pull an IPv4 for itself.
    – Sam
    Commented Jun 5, 2020 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .