LXD containers and networking with static IP












6















I have done some Docker testing over the years, but for a specific kind of testing, I need a little more then just application containers. So I turned to LXC/LXD containers, which is great. After installing LXD I created my own container:



>> willem@ubuntu:/$ lxc launch images:centos/7/amd64 thing Creating thing
Starting thing
>> willem@ubuntu:/$ lxc list
+-------+---------+------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+-------+---------+------------------+------+------------+-----------+
| thing | RUNNING | 10.0.3.30 (eth0) | | PERSISTENT | 0 |
+-------+---------+------------------+------+------------+-----------+


Great. Only I don't seem to have any control over the IP address here. I found (with help of Google and locate) a control file:



>> root@ubuntu:/# locate lxc.conf 
/etc/init/lxc.conf
..
/var/log/lxd/thing/lxc.conf


in which you can put a (what looks like) static IP4-address. Only upon rebooting the container, the host seem to have forgotten my static IP, and takes the one from DHCP. Questions:




  • What is the right way of doing this (I mean, the latest Docker has an --ip switch in docker run which sets the IP address) ?










share|improve this question

























  • You might want to remove your last 2 bullet points. Requests for tutorials and books are off-topic.

    – DavidPostill
    Mar 2 '16 at 22:12


















6















I have done some Docker testing over the years, but for a specific kind of testing, I need a little more then just application containers. So I turned to LXC/LXD containers, which is great. After installing LXD I created my own container:



>> willem@ubuntu:/$ lxc launch images:centos/7/amd64 thing Creating thing
Starting thing
>> willem@ubuntu:/$ lxc list
+-------+---------+------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+-------+---------+------------------+------+------------+-----------+
| thing | RUNNING | 10.0.3.30 (eth0) | | PERSISTENT | 0 |
+-------+---------+------------------+------+------------+-----------+


Great. Only I don't seem to have any control over the IP address here. I found (with help of Google and locate) a control file:



>> root@ubuntu:/# locate lxc.conf 
/etc/init/lxc.conf
..
/var/log/lxd/thing/lxc.conf


in which you can put a (what looks like) static IP4-address. Only upon rebooting the container, the host seem to have forgotten my static IP, and takes the one from DHCP. Questions:




  • What is the right way of doing this (I mean, the latest Docker has an --ip switch in docker run which sets the IP address) ?










share|improve this question

























  • You might want to remove your last 2 bullet points. Requests for tutorials and books are off-topic.

    – DavidPostill
    Mar 2 '16 at 22:12
















6












6








6


2






I have done some Docker testing over the years, but for a specific kind of testing, I need a little more then just application containers. So I turned to LXC/LXD containers, which is great. After installing LXD I created my own container:



>> willem@ubuntu:/$ lxc launch images:centos/7/amd64 thing Creating thing
Starting thing
>> willem@ubuntu:/$ lxc list
+-------+---------+------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+-------+---------+------------------+------+------------+-----------+
| thing | RUNNING | 10.0.3.30 (eth0) | | PERSISTENT | 0 |
+-------+---------+------------------+------+------------+-----------+


Great. Only I don't seem to have any control over the IP address here. I found (with help of Google and locate) a control file:



>> root@ubuntu:/# locate lxc.conf 
/etc/init/lxc.conf
..
/var/log/lxd/thing/lxc.conf


in which you can put a (what looks like) static IP4-address. Only upon rebooting the container, the host seem to have forgotten my static IP, and takes the one from DHCP. Questions:




  • What is the right way of doing this (I mean, the latest Docker has an --ip switch in docker run which sets the IP address) ?










share|improve this question
















I have done some Docker testing over the years, but for a specific kind of testing, I need a little more then just application containers. So I turned to LXC/LXD containers, which is great. After installing LXD I created my own container:



>> willem@ubuntu:/$ lxc launch images:centos/7/amd64 thing Creating thing
Starting thing
>> willem@ubuntu:/$ lxc list
+-------+---------+------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+-------+---------+------------------+------+------------+-----------+
| thing | RUNNING | 10.0.3.30 (eth0) | | PERSISTENT | 0 |
+-------+---------+------------------+------+------------+-----------+


Great. Only I don't seem to have any control over the IP address here. I found (with help of Google and locate) a control file:



>> root@ubuntu:/# locate lxc.conf 
/etc/init/lxc.conf
..
/var/log/lxd/thing/lxc.conf


in which you can put a (what looks like) static IP4-address. Only upon rebooting the container, the host seem to have forgotten my static IP, and takes the one from DHCP. Questions:




  • What is the right way of doing this (I mean, the latest Docker has an --ip switch in docker run which sets the IP address) ?







linux networking ubuntu lxc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 21 '16 at 21:57









jeteon

1054




1054










asked Mar 2 '16 at 22:02









WillemWillem

2391615




2391615













  • You might want to remove your last 2 bullet points. Requests for tutorials and books are off-topic.

    – DavidPostill
    Mar 2 '16 at 22:12





















  • You might want to remove your last 2 bullet points. Requests for tutorials and books are off-topic.

    – DavidPostill
    Mar 2 '16 at 22:12



















You might want to remove your last 2 bullet points. Requests for tutorials and books are off-topic.

– DavidPostill
Mar 2 '16 at 22:12







You might want to remove your last 2 bullet points. Requests for tutorials and books are off-topic.

– DavidPostill
Mar 2 '16 at 22:12












5 Answers
5






active

oldest

votes


















3














These configurations are to be placed in the file /var/lib/lxc/ContainerName/config. The possible values of the parameters are specified in the Manual. They pertain to different areas, hostname, network, console, ttys, mount point, cgroups, capabilities,...



Under network, you will find all you need. Sensible values are automatically produced for unspecified variables. The part most relevant to your question is the following:




lxc.network.name



the interface name is dynamically allocated, but if another name is needed because the configuration files being used by the container use a generic name, eg. eth0, this option will rename the interface in the container.



lxc.network.hwaddr



the interface mac address is dynamically allocated by default to the virtual interface, but in some cases, this is needed to resolve a mac address conflict or to always have the same link-local ipv6 address



lxc.network.ipv4



specify the ipv4 address to assign to the virtualized interface. Several lines specify several ipv4 addresses. The address is in format x.y.z.t/m, eg. 192.168.1.123/24. The broadcast address should be specified on the same line, right after the ipv4 address.



lxc.network.ipv4.gateway



specify the ipv4 address to use as the gateway inside the container. The address is in format x.y.z.t, eg. 192.168.1.123. Can also have the special value auto, which means to take the primary address from the bridge interface (as specified by the lxc.network.link option) and use that as the gateway. auto is only available when using the veth and macvlan network types.







share|improve this answer
























  • Is this answer pertinent to LXD-managed containers as well? I don't have /var/lib/lxc, and all I have in /var/lib/lxd/<ContainerName> is metadata.yaml, templates and rootfs. (I'm on Ubuntu Xenial, LXD version 2.0.0.)

    – Jonathan Y.
    May 15 '16 at 21:43











  • I haven't tried it, but probably you can give these flags via the --config option to lxc launch. I haven't figured out how to create custom profiles yet.

    – Per Johansson
    Jun 1 '16 at 14:13



















5














If you want to specify ip addresses for containers in LXD, look at the /etc/default/lxd-bridge file. There you will find a spot to include an external dnsmasq configuration file.



Assuming you are on Ubuntu 16.04,



Open up /etc/default/lxd-bridge in your favorite editor. You will need to use sudo.



At around line 16,



LXD_CONFILE=""



Add an entry that points to a dnsmasq configuration file. You are going to have to create this file. So name it whatever you wish. Something like lxd_bridge.conf.



Then create and edit the file that you have named above.



Add a line for each container that you want to assign a specific ip address to.



Like this:



dhcp-host=containername,ipaddress



Then you will need to restart lxd-bridge and then restart the containers.



Here's a detailed article about this:



LXD Static IPs



Note that if you are running Alpine linux in your containers, additional steps need to be taken to make this work.
alpine






share|improve this answer


























  • Welcome to Super User! Can you paraphrase the instructions for that link? This is just to prevent any adverse effects for link rot, in accordance with our policy. Thank you!

    – oldmud0
    Aug 3 '16 at 19:25











  • No problem. Thanks for not booting me.

    – Jason Trickett
    Aug 3 '16 at 20:48



















3














System info: lxc-3.0.1 on Ubuntu Server 18.04.1.



After a lot of searching, I found this simple command to assign a static ip to a container:



lxc config set [container] raw.lxc 'lxc.net.[i].ipv4.address = [ip]/[subnet-mask]'


where [container], [i], [ip] and [subnet-mask] are the container name, network interface number, desired static ip address, and the CIDR for the desired subnet mask, respectively.



For instance, if you want to assign 240.10.0.20 with subnet mask 255.255.255.0 to the 0'th network interface of the container named hello, you can use:



lxc config set hello raw.lxc 'lxc.net.0.ipv4.address = 240.10.0.20/24'


Take a look here for the acceptable configuration keys appendable to lxc.net.[i]. This webpage contains information on the CIDR notation.



You will probably need to restart the container after executing the command.



Unfortunately, I cannot remember where I first found this solution. Here is a relevant GitHub issue that might contain useful information.






share|improve this answer

































    0














    For anybody who is still struggling how to assign static ip adresses to the lxd containers.



    My first attempt was to do it over dnsmasq so i added the ip adresses of the containers in /var/lib/lxd/networks/lxdbr0/dnsmasq.hosts/<container> and reloaded dnsmasq service. It did really work but did not persist after restarting the Host.



    I thought it should be possible to achieve that with lxd tool. In fact it was mentioned in the lcd-doc. I didn't really know how to configure the key user.network-config using the command lxc config set <container> user.network-config <value>. Since i couldn't inject multiple key:values in the value argument.





    1. check the container subnet



      lxc network show lxdbr0



    2. fist create a yml (network.yml) file with the following value



      version: 1
      config:
      - type: physical
      name: eth1
      subnets:
      - type: static
      ipv4: true
      address: <ip>
      netmask: 255.255.255.0
      gateway: <gateway> // ends often with .1
      control: auto
      - type: nameserver
      address: 8.8.8.8 // for example



    3. create a container with this network configurations



      lxc launch ubuntu:18.04 test --config=user.network-config="$(cat network.yml)"    



    voalá :)



    I tried to change the ip address after creating the container



       lxc config set <container> user.network-config "$(cat network.yml)"


    i restarted the container but the ip didn't update. If any body knows how to get it work after creating the container i will very grateful.



    lxd version 3.0.1






    share|improve this answer































      0














      I found a solution here



      Thanks to Stéphane Graber.



      Quote:



      "If running a modern LXD with an LXD managed bridge, then you can just set the ipv4.address property on the network interface of the container.




      • lxc stop c1

      • lxc network attach lxdbr0 c1 eth0 eth0

      • lxc config device set c1 eth0 ipv4.address 10.99.10.42

      • lxc start c1


      "






      share|improve this answer























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "3"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1047891%2flxd-containers-and-networking-with-static-ip%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        These configurations are to be placed in the file /var/lib/lxc/ContainerName/config. The possible values of the parameters are specified in the Manual. They pertain to different areas, hostname, network, console, ttys, mount point, cgroups, capabilities,...



        Under network, you will find all you need. Sensible values are automatically produced for unspecified variables. The part most relevant to your question is the following:




        lxc.network.name



        the interface name is dynamically allocated, but if another name is needed because the configuration files being used by the container use a generic name, eg. eth0, this option will rename the interface in the container.



        lxc.network.hwaddr



        the interface mac address is dynamically allocated by default to the virtual interface, but in some cases, this is needed to resolve a mac address conflict or to always have the same link-local ipv6 address



        lxc.network.ipv4



        specify the ipv4 address to assign to the virtualized interface. Several lines specify several ipv4 addresses. The address is in format x.y.z.t/m, eg. 192.168.1.123/24. The broadcast address should be specified on the same line, right after the ipv4 address.



        lxc.network.ipv4.gateway



        specify the ipv4 address to use as the gateway inside the container. The address is in format x.y.z.t, eg. 192.168.1.123. Can also have the special value auto, which means to take the primary address from the bridge interface (as specified by the lxc.network.link option) and use that as the gateway. auto is only available when using the veth and macvlan network types.







        share|improve this answer
























        • Is this answer pertinent to LXD-managed containers as well? I don't have /var/lib/lxc, and all I have in /var/lib/lxd/<ContainerName> is metadata.yaml, templates and rootfs. (I'm on Ubuntu Xenial, LXD version 2.0.0.)

          – Jonathan Y.
          May 15 '16 at 21:43











        • I haven't tried it, but probably you can give these flags via the --config option to lxc launch. I haven't figured out how to create custom profiles yet.

          – Per Johansson
          Jun 1 '16 at 14:13
















        3














        These configurations are to be placed in the file /var/lib/lxc/ContainerName/config. The possible values of the parameters are specified in the Manual. They pertain to different areas, hostname, network, console, ttys, mount point, cgroups, capabilities,...



        Under network, you will find all you need. Sensible values are automatically produced for unspecified variables. The part most relevant to your question is the following:




        lxc.network.name



        the interface name is dynamically allocated, but if another name is needed because the configuration files being used by the container use a generic name, eg. eth0, this option will rename the interface in the container.



        lxc.network.hwaddr



        the interface mac address is dynamically allocated by default to the virtual interface, but in some cases, this is needed to resolve a mac address conflict or to always have the same link-local ipv6 address



        lxc.network.ipv4



        specify the ipv4 address to assign to the virtualized interface. Several lines specify several ipv4 addresses. The address is in format x.y.z.t/m, eg. 192.168.1.123/24. The broadcast address should be specified on the same line, right after the ipv4 address.



        lxc.network.ipv4.gateway



        specify the ipv4 address to use as the gateway inside the container. The address is in format x.y.z.t, eg. 192.168.1.123. Can also have the special value auto, which means to take the primary address from the bridge interface (as specified by the lxc.network.link option) and use that as the gateway. auto is only available when using the veth and macvlan network types.







        share|improve this answer
























        • Is this answer pertinent to LXD-managed containers as well? I don't have /var/lib/lxc, and all I have in /var/lib/lxd/<ContainerName> is metadata.yaml, templates and rootfs. (I'm on Ubuntu Xenial, LXD version 2.0.0.)

          – Jonathan Y.
          May 15 '16 at 21:43











        • I haven't tried it, but probably you can give these flags via the --config option to lxc launch. I haven't figured out how to create custom profiles yet.

          – Per Johansson
          Jun 1 '16 at 14:13














        3












        3








        3







        These configurations are to be placed in the file /var/lib/lxc/ContainerName/config. The possible values of the parameters are specified in the Manual. They pertain to different areas, hostname, network, console, ttys, mount point, cgroups, capabilities,...



        Under network, you will find all you need. Sensible values are automatically produced for unspecified variables. The part most relevant to your question is the following:




        lxc.network.name



        the interface name is dynamically allocated, but if another name is needed because the configuration files being used by the container use a generic name, eg. eth0, this option will rename the interface in the container.



        lxc.network.hwaddr



        the interface mac address is dynamically allocated by default to the virtual interface, but in some cases, this is needed to resolve a mac address conflict or to always have the same link-local ipv6 address



        lxc.network.ipv4



        specify the ipv4 address to assign to the virtualized interface. Several lines specify several ipv4 addresses. The address is in format x.y.z.t/m, eg. 192.168.1.123/24. The broadcast address should be specified on the same line, right after the ipv4 address.



        lxc.network.ipv4.gateway



        specify the ipv4 address to use as the gateway inside the container. The address is in format x.y.z.t, eg. 192.168.1.123. Can also have the special value auto, which means to take the primary address from the bridge interface (as specified by the lxc.network.link option) and use that as the gateway. auto is only available when using the veth and macvlan network types.







        share|improve this answer













        These configurations are to be placed in the file /var/lib/lxc/ContainerName/config. The possible values of the parameters are specified in the Manual. They pertain to different areas, hostname, network, console, ttys, mount point, cgroups, capabilities,...



        Under network, you will find all you need. Sensible values are automatically produced for unspecified variables. The part most relevant to your question is the following:




        lxc.network.name



        the interface name is dynamically allocated, but if another name is needed because the configuration files being used by the container use a generic name, eg. eth0, this option will rename the interface in the container.



        lxc.network.hwaddr



        the interface mac address is dynamically allocated by default to the virtual interface, but in some cases, this is needed to resolve a mac address conflict or to always have the same link-local ipv6 address



        lxc.network.ipv4



        specify the ipv4 address to assign to the virtualized interface. Several lines specify several ipv4 addresses. The address is in format x.y.z.t/m, eg. 192.168.1.123/24. The broadcast address should be specified on the same line, right after the ipv4 address.



        lxc.network.ipv4.gateway



        specify the ipv4 address to use as the gateway inside the container. The address is in format x.y.z.t, eg. 192.168.1.123. Can also have the special value auto, which means to take the primary address from the bridge interface (as specified by the lxc.network.link option) and use that as the gateway. auto is only available when using the veth and macvlan network types.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 3 '16 at 9:42









        MariusMatutiaeMariusMatutiae

        38.7k953100




        38.7k953100













        • Is this answer pertinent to LXD-managed containers as well? I don't have /var/lib/lxc, and all I have in /var/lib/lxd/<ContainerName> is metadata.yaml, templates and rootfs. (I'm on Ubuntu Xenial, LXD version 2.0.0.)

          – Jonathan Y.
          May 15 '16 at 21:43











        • I haven't tried it, but probably you can give these flags via the --config option to lxc launch. I haven't figured out how to create custom profiles yet.

          – Per Johansson
          Jun 1 '16 at 14:13



















        • Is this answer pertinent to LXD-managed containers as well? I don't have /var/lib/lxc, and all I have in /var/lib/lxd/<ContainerName> is metadata.yaml, templates and rootfs. (I'm on Ubuntu Xenial, LXD version 2.0.0.)

          – Jonathan Y.
          May 15 '16 at 21:43











        • I haven't tried it, but probably you can give these flags via the --config option to lxc launch. I haven't figured out how to create custom profiles yet.

          – Per Johansson
          Jun 1 '16 at 14:13

















        Is this answer pertinent to LXD-managed containers as well? I don't have /var/lib/lxc, and all I have in /var/lib/lxd/<ContainerName> is metadata.yaml, templates and rootfs. (I'm on Ubuntu Xenial, LXD version 2.0.0.)

        – Jonathan Y.
        May 15 '16 at 21:43





        Is this answer pertinent to LXD-managed containers as well? I don't have /var/lib/lxc, and all I have in /var/lib/lxd/<ContainerName> is metadata.yaml, templates and rootfs. (I'm on Ubuntu Xenial, LXD version 2.0.0.)

        – Jonathan Y.
        May 15 '16 at 21:43













        I haven't tried it, but probably you can give these flags via the --config option to lxc launch. I haven't figured out how to create custom profiles yet.

        – Per Johansson
        Jun 1 '16 at 14:13





        I haven't tried it, but probably you can give these flags via the --config option to lxc launch. I haven't figured out how to create custom profiles yet.

        – Per Johansson
        Jun 1 '16 at 14:13













        5














        If you want to specify ip addresses for containers in LXD, look at the /etc/default/lxd-bridge file. There you will find a spot to include an external dnsmasq configuration file.



        Assuming you are on Ubuntu 16.04,



        Open up /etc/default/lxd-bridge in your favorite editor. You will need to use sudo.



        At around line 16,



        LXD_CONFILE=""



        Add an entry that points to a dnsmasq configuration file. You are going to have to create this file. So name it whatever you wish. Something like lxd_bridge.conf.



        Then create and edit the file that you have named above.



        Add a line for each container that you want to assign a specific ip address to.



        Like this:



        dhcp-host=containername,ipaddress



        Then you will need to restart lxd-bridge and then restart the containers.



        Here's a detailed article about this:



        LXD Static IPs



        Note that if you are running Alpine linux in your containers, additional steps need to be taken to make this work.
        alpine






        share|improve this answer


























        • Welcome to Super User! Can you paraphrase the instructions for that link? This is just to prevent any adverse effects for link rot, in accordance with our policy. Thank you!

          – oldmud0
          Aug 3 '16 at 19:25











        • No problem. Thanks for not booting me.

          – Jason Trickett
          Aug 3 '16 at 20:48
















        5














        If you want to specify ip addresses for containers in LXD, look at the /etc/default/lxd-bridge file. There you will find a spot to include an external dnsmasq configuration file.



        Assuming you are on Ubuntu 16.04,



        Open up /etc/default/lxd-bridge in your favorite editor. You will need to use sudo.



        At around line 16,



        LXD_CONFILE=""



        Add an entry that points to a dnsmasq configuration file. You are going to have to create this file. So name it whatever you wish. Something like lxd_bridge.conf.



        Then create and edit the file that you have named above.



        Add a line for each container that you want to assign a specific ip address to.



        Like this:



        dhcp-host=containername,ipaddress



        Then you will need to restart lxd-bridge and then restart the containers.



        Here's a detailed article about this:



        LXD Static IPs



        Note that if you are running Alpine linux in your containers, additional steps need to be taken to make this work.
        alpine






        share|improve this answer


























        • Welcome to Super User! Can you paraphrase the instructions for that link? This is just to prevent any adverse effects for link rot, in accordance with our policy. Thank you!

          – oldmud0
          Aug 3 '16 at 19:25











        • No problem. Thanks for not booting me.

          – Jason Trickett
          Aug 3 '16 at 20:48














        5












        5








        5







        If you want to specify ip addresses for containers in LXD, look at the /etc/default/lxd-bridge file. There you will find a spot to include an external dnsmasq configuration file.



        Assuming you are on Ubuntu 16.04,



        Open up /etc/default/lxd-bridge in your favorite editor. You will need to use sudo.



        At around line 16,



        LXD_CONFILE=""



        Add an entry that points to a dnsmasq configuration file. You are going to have to create this file. So name it whatever you wish. Something like lxd_bridge.conf.



        Then create and edit the file that you have named above.



        Add a line for each container that you want to assign a specific ip address to.



        Like this:



        dhcp-host=containername,ipaddress



        Then you will need to restart lxd-bridge and then restart the containers.



        Here's a detailed article about this:



        LXD Static IPs



        Note that if you are running Alpine linux in your containers, additional steps need to be taken to make this work.
        alpine






        share|improve this answer















        If you want to specify ip addresses for containers in LXD, look at the /etc/default/lxd-bridge file. There you will find a spot to include an external dnsmasq configuration file.



        Assuming you are on Ubuntu 16.04,



        Open up /etc/default/lxd-bridge in your favorite editor. You will need to use sudo.



        At around line 16,



        LXD_CONFILE=""



        Add an entry that points to a dnsmasq configuration file. You are going to have to create this file. So name it whatever you wish. Something like lxd_bridge.conf.



        Then create and edit the file that you have named above.



        Add a line for each container that you want to assign a specific ip address to.



        Like this:



        dhcp-host=containername,ipaddress



        Then you will need to restart lxd-bridge and then restart the containers.



        Here's a detailed article about this:



        LXD Static IPs



        Note that if you are running Alpine linux in your containers, additional steps need to be taken to make this work.
        alpine







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 19 '16 at 18:37

























        answered Aug 3 '16 at 18:11









        Jason TrickettJason Trickett

        7113




        7113













        • Welcome to Super User! Can you paraphrase the instructions for that link? This is just to prevent any adverse effects for link rot, in accordance with our policy. Thank you!

          – oldmud0
          Aug 3 '16 at 19:25











        • No problem. Thanks for not booting me.

          – Jason Trickett
          Aug 3 '16 at 20:48



















        • Welcome to Super User! Can you paraphrase the instructions for that link? This is just to prevent any adverse effects for link rot, in accordance with our policy. Thank you!

          – oldmud0
          Aug 3 '16 at 19:25











        • No problem. Thanks for not booting me.

          – Jason Trickett
          Aug 3 '16 at 20:48

















        Welcome to Super User! Can you paraphrase the instructions for that link? This is just to prevent any adverse effects for link rot, in accordance with our policy. Thank you!

        – oldmud0
        Aug 3 '16 at 19:25





        Welcome to Super User! Can you paraphrase the instructions for that link? This is just to prevent any adverse effects for link rot, in accordance with our policy. Thank you!

        – oldmud0
        Aug 3 '16 at 19:25













        No problem. Thanks for not booting me.

        – Jason Trickett
        Aug 3 '16 at 20:48





        No problem. Thanks for not booting me.

        – Jason Trickett
        Aug 3 '16 at 20:48











        3














        System info: lxc-3.0.1 on Ubuntu Server 18.04.1.



        After a lot of searching, I found this simple command to assign a static ip to a container:



        lxc config set [container] raw.lxc 'lxc.net.[i].ipv4.address = [ip]/[subnet-mask]'


        where [container], [i], [ip] and [subnet-mask] are the container name, network interface number, desired static ip address, and the CIDR for the desired subnet mask, respectively.



        For instance, if you want to assign 240.10.0.20 with subnet mask 255.255.255.0 to the 0'th network interface of the container named hello, you can use:



        lxc config set hello raw.lxc 'lxc.net.0.ipv4.address = 240.10.0.20/24'


        Take a look here for the acceptable configuration keys appendable to lxc.net.[i]. This webpage contains information on the CIDR notation.



        You will probably need to restart the container after executing the command.



        Unfortunately, I cannot remember where I first found this solution. Here is a relevant GitHub issue that might contain useful information.






        share|improve this answer






























          3














          System info: lxc-3.0.1 on Ubuntu Server 18.04.1.



          After a lot of searching, I found this simple command to assign a static ip to a container:



          lxc config set [container] raw.lxc 'lxc.net.[i].ipv4.address = [ip]/[subnet-mask]'


          where [container], [i], [ip] and [subnet-mask] are the container name, network interface number, desired static ip address, and the CIDR for the desired subnet mask, respectively.



          For instance, if you want to assign 240.10.0.20 with subnet mask 255.255.255.0 to the 0'th network interface of the container named hello, you can use:



          lxc config set hello raw.lxc 'lxc.net.0.ipv4.address = 240.10.0.20/24'


          Take a look here for the acceptable configuration keys appendable to lxc.net.[i]. This webpage contains information on the CIDR notation.



          You will probably need to restart the container after executing the command.



          Unfortunately, I cannot remember where I first found this solution. Here is a relevant GitHub issue that might contain useful information.






          share|improve this answer




























            3












            3








            3







            System info: lxc-3.0.1 on Ubuntu Server 18.04.1.



            After a lot of searching, I found this simple command to assign a static ip to a container:



            lxc config set [container] raw.lxc 'lxc.net.[i].ipv4.address = [ip]/[subnet-mask]'


            where [container], [i], [ip] and [subnet-mask] are the container name, network interface number, desired static ip address, and the CIDR for the desired subnet mask, respectively.



            For instance, if you want to assign 240.10.0.20 with subnet mask 255.255.255.0 to the 0'th network interface of the container named hello, you can use:



            lxc config set hello raw.lxc 'lxc.net.0.ipv4.address = 240.10.0.20/24'


            Take a look here for the acceptable configuration keys appendable to lxc.net.[i]. This webpage contains information on the CIDR notation.



            You will probably need to restart the container after executing the command.



            Unfortunately, I cannot remember where I first found this solution. Here is a relevant GitHub issue that might contain useful information.






            share|improve this answer















            System info: lxc-3.0.1 on Ubuntu Server 18.04.1.



            After a lot of searching, I found this simple command to assign a static ip to a container:



            lxc config set [container] raw.lxc 'lxc.net.[i].ipv4.address = [ip]/[subnet-mask]'


            where [container], [i], [ip] and [subnet-mask] are the container name, network interface number, desired static ip address, and the CIDR for the desired subnet mask, respectively.



            For instance, if you want to assign 240.10.0.20 with subnet mask 255.255.255.0 to the 0'th network interface of the container named hello, you can use:



            lxc config set hello raw.lxc 'lxc.net.0.ipv4.address = 240.10.0.20/24'


            Take a look here for the acceptable configuration keys appendable to lxc.net.[i]. This webpage contains information on the CIDR notation.



            You will probably need to restart the container after executing the command.



            Unfortunately, I cannot remember where I first found this solution. Here is a relevant GitHub issue that might contain useful information.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 14 '18 at 21:56

























            answered Sep 14 '18 at 21:34









            Mohamed LaradjiMohamed Laradji

            438




            438























                0














                For anybody who is still struggling how to assign static ip adresses to the lxd containers.



                My first attempt was to do it over dnsmasq so i added the ip adresses of the containers in /var/lib/lxd/networks/lxdbr0/dnsmasq.hosts/<container> and reloaded dnsmasq service. It did really work but did not persist after restarting the Host.



                I thought it should be possible to achieve that with lxd tool. In fact it was mentioned in the lcd-doc. I didn't really know how to configure the key user.network-config using the command lxc config set <container> user.network-config <value>. Since i couldn't inject multiple key:values in the value argument.





                1. check the container subnet



                  lxc network show lxdbr0



                2. fist create a yml (network.yml) file with the following value



                  version: 1
                  config:
                  - type: physical
                  name: eth1
                  subnets:
                  - type: static
                  ipv4: true
                  address: <ip>
                  netmask: 255.255.255.0
                  gateway: <gateway> // ends often with .1
                  control: auto
                  - type: nameserver
                  address: 8.8.8.8 // for example



                3. create a container with this network configurations



                  lxc launch ubuntu:18.04 test --config=user.network-config="$(cat network.yml)"    



                voalá :)



                I tried to change the ip address after creating the container



                   lxc config set <container> user.network-config "$(cat network.yml)"


                i restarted the container but the ip didn't update. If any body knows how to get it work after creating the container i will very grateful.



                lxd version 3.0.1






                share|improve this answer




























                  0














                  For anybody who is still struggling how to assign static ip adresses to the lxd containers.



                  My first attempt was to do it over dnsmasq so i added the ip adresses of the containers in /var/lib/lxd/networks/lxdbr0/dnsmasq.hosts/<container> and reloaded dnsmasq service. It did really work but did not persist after restarting the Host.



                  I thought it should be possible to achieve that with lxd tool. In fact it was mentioned in the lcd-doc. I didn't really know how to configure the key user.network-config using the command lxc config set <container> user.network-config <value>. Since i couldn't inject multiple key:values in the value argument.





                  1. check the container subnet



                    lxc network show lxdbr0



                  2. fist create a yml (network.yml) file with the following value



                    version: 1
                    config:
                    - type: physical
                    name: eth1
                    subnets:
                    - type: static
                    ipv4: true
                    address: <ip>
                    netmask: 255.255.255.0
                    gateway: <gateway> // ends often with .1
                    control: auto
                    - type: nameserver
                    address: 8.8.8.8 // for example



                  3. create a container with this network configurations



                    lxc launch ubuntu:18.04 test --config=user.network-config="$(cat network.yml)"    



                  voalá :)



                  I tried to change the ip address after creating the container



                     lxc config set <container> user.network-config "$(cat network.yml)"


                  i restarted the container but the ip didn't update. If any body knows how to get it work after creating the container i will very grateful.



                  lxd version 3.0.1






                  share|improve this answer


























                    0












                    0








                    0







                    For anybody who is still struggling how to assign static ip adresses to the lxd containers.



                    My first attempt was to do it over dnsmasq so i added the ip adresses of the containers in /var/lib/lxd/networks/lxdbr0/dnsmasq.hosts/<container> and reloaded dnsmasq service. It did really work but did not persist after restarting the Host.



                    I thought it should be possible to achieve that with lxd tool. In fact it was mentioned in the lcd-doc. I didn't really know how to configure the key user.network-config using the command lxc config set <container> user.network-config <value>. Since i couldn't inject multiple key:values in the value argument.





                    1. check the container subnet



                      lxc network show lxdbr0



                    2. fist create a yml (network.yml) file with the following value



                      version: 1
                      config:
                      - type: physical
                      name: eth1
                      subnets:
                      - type: static
                      ipv4: true
                      address: <ip>
                      netmask: 255.255.255.0
                      gateway: <gateway> // ends often with .1
                      control: auto
                      - type: nameserver
                      address: 8.8.8.8 // for example



                    3. create a container with this network configurations



                      lxc launch ubuntu:18.04 test --config=user.network-config="$(cat network.yml)"    



                    voalá :)



                    I tried to change the ip address after creating the container



                       lxc config set <container> user.network-config "$(cat network.yml)"


                    i restarted the container but the ip didn't update. If any body knows how to get it work after creating the container i will very grateful.



                    lxd version 3.0.1






                    share|improve this answer













                    For anybody who is still struggling how to assign static ip adresses to the lxd containers.



                    My first attempt was to do it over dnsmasq so i added the ip adresses of the containers in /var/lib/lxd/networks/lxdbr0/dnsmasq.hosts/<container> and reloaded dnsmasq service. It did really work but did not persist after restarting the Host.



                    I thought it should be possible to achieve that with lxd tool. In fact it was mentioned in the lcd-doc. I didn't really know how to configure the key user.network-config using the command lxc config set <container> user.network-config <value>. Since i couldn't inject multiple key:values in the value argument.





                    1. check the container subnet



                      lxc network show lxdbr0



                    2. fist create a yml (network.yml) file with the following value



                      version: 1
                      config:
                      - type: physical
                      name: eth1
                      subnets:
                      - type: static
                      ipv4: true
                      address: <ip>
                      netmask: 255.255.255.0
                      gateway: <gateway> // ends often with .1
                      control: auto
                      - type: nameserver
                      address: 8.8.8.8 // for example



                    3. create a container with this network configurations



                      lxc launch ubuntu:18.04 test --config=user.network-config="$(cat network.yml)"    



                    voalá :)



                    I tried to change the ip address after creating the container



                       lxc config set <container> user.network-config "$(cat network.yml)"


                    i restarted the container but the ip didn't update. If any body knows how to get it work after creating the container i will very grateful.



                    lxd version 3.0.1







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 4 '18 at 17:08









                    alixanderalixander

                    248




                    248























                        0














                        I found a solution here



                        Thanks to Stéphane Graber.



                        Quote:



                        "If running a modern LXD with an LXD managed bridge, then you can just set the ipv4.address property on the network interface of the container.




                        • lxc stop c1

                        • lxc network attach lxdbr0 c1 eth0 eth0

                        • lxc config device set c1 eth0 ipv4.address 10.99.10.42

                        • lxc start c1


                        "






                        share|improve this answer




























                          0














                          I found a solution here



                          Thanks to Stéphane Graber.



                          Quote:



                          "If running a modern LXD with an LXD managed bridge, then you can just set the ipv4.address property on the network interface of the container.




                          • lxc stop c1

                          • lxc network attach lxdbr0 c1 eth0 eth0

                          • lxc config device set c1 eth0 ipv4.address 10.99.10.42

                          • lxc start c1


                          "






                          share|improve this answer


























                            0












                            0








                            0







                            I found a solution here



                            Thanks to Stéphane Graber.



                            Quote:



                            "If running a modern LXD with an LXD managed bridge, then you can just set the ipv4.address property on the network interface of the container.




                            • lxc stop c1

                            • lxc network attach lxdbr0 c1 eth0 eth0

                            • lxc config device set c1 eth0 ipv4.address 10.99.10.42

                            • lxc start c1


                            "






                            share|improve this answer













                            I found a solution here



                            Thanks to Stéphane Graber.



                            Quote:



                            "If running a modern LXD with an LXD managed bridge, then you can just set the ipv4.address property on the network interface of the container.




                            • lxc stop c1

                            • lxc network attach lxdbr0 c1 eth0 eth0

                            • lxc config device set c1 eth0 ipv4.address 10.99.10.42

                            • lxc start c1


                            "







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 11 at 11:33









                            Dmitry SomovDmitry Somov

                            11




                            11






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Super User!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1047891%2flxd-containers-and-networking-with-static-ip%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                                Mangá

                                 ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕