I know, that I can assign static IP manually, using /etc/network/interfaces
.
I also know, that I can read the MAC address of the LXC container (e.g. by looking for lxc.network.hwaddr
entry in /var/lib/lxc/<container-name>/config
and assign the IP based using entries dhcp-host=<mac-addr>,10.0.3.3
in /etc/dnsmasq.d/<some file>
.
In the file /etc/default/lxc-net
I read
# Uncomment the next line if you'd like to use a conf-file for the lxcbr0
# dnsmasq. For instance, you can use 'dhcp-host=mail1,10.0.3.100' to have
# container 'mail1' always get ip address 10.0.3.100.
#LXC_DHCP_CONFILE=/etc/lxc/dnsmasq.conf
That would suit my needs; unfortunately doing so has no effect.
I ran into this recently and I think I found an easy solution. I (only) tested it on Ubuntu 14.04.
First, uncomment this line /etc/default/lxc-net:
In /etc/lxc/dnsmasq.conf, define a dhcp-hostsfile:
Then add entries in /etc/lxc/dnsmasq-hosts.conf like this:
Beware: changes will become effective after you have restarted lxc-net (which restarts dnsmasq):
Afterwards you can modify /etc/lxc/dnsmasq-hosts.conf and send the SIGHUP signal to dnsmasq:
So yes, you need to restart lxc-net, but only once. Hope this helps.
It works fine in Ubuntu 14.04.1
Uncomment this line
/etc/default/lxc-net
stop all containers, restart lxc-net:
Configure ip addresses in
/etc/lxc/dnsmasq.conf
where
{NAME}
is the name of your LXC container:The Tombart's answer works if you are patient enough to wait for the DNS refresh AND you are willing to restart the container (the guest) afterwards.
What follows is the recipe that requires that all other possibly running lxc containers are shut down. If you cannot afford that, then I see no way of forcing new dnsmasq configuration. (For some reasone signaling HUP to dnsmasq's pid found in
/run/lxc/dnsmasq.pid
doesn't work either.)So if you want to have something that works instantly and there is no other lxc containers running, follow my answer.
$name
is the name of the node for which we want to reset the assignment, and$internalif
is the name of the LXC's bridged adapter. You can get the value of the$internalif
with e.g.augtool -L -A --transform "Shellvars incl /etc/default/lxc-net" get "/files/etc/default/lxc-net/LXC_BRIDGE" | sed -En 's/\/.* = (.*)/\1/p'
if you installaugeas-tools
but usually it is justlxcbr0
.Unfortunately, there is a bug (feature?) in the
/etc/init/lxc-net.conf
in Ubuntu 14.04 that prevents reloading thednsmasq
unless the bridge device is down for the host.This solution works by patching the lxc upstart scripts. It does split the complex task of bringing the lxcbr0 bridge up and starting a
dnsmasq
into two separate jobs. Now you don't need to restart the wholelxc-net
bridge to just reload thednsmasq
- reloading thesudo service restart lxc-dnsmasq
is sufficient and does not require shutting down the bridge.sudo service lxc-net stop
and make sure there is no lxcbr0 (or equivalent) bridge up./etc/init/lxc-net.conf
with the following contents:.
/etc/init/lxc-dnsmasq
with the following contents:.
Here is simple python script which releases LXC dnsmasq lease. You can run it from host machine or forge it from another container - yes that works!:
The perequisite for above is scapy python library:
Once run you should see in system log something like:
To confirm just check if entry is removed from
/var/lib/misc/dnsmasq.lxcbr0.leases
. The container itself will keep the IP so it should be stopped before starting any new container which should reuse the IP.I realize my answer is years late, but maybe it helps someone else. The issue was that you edited code specific to the LXC Ubuntu package (
write_lxc_net
function) that was meant to be written to another destination as a string, not processed within thelxc-net
script itself!As a result, the dnsmasq process didn't receive the config file you tried to tried to pass it, leaving you with "no effect", as you say.
Instead, you'd want to set this variable near the top of the script, among the rest: