How do I get the IP address of an LXC container?












19















I've written a few scripts to manage LXC containers, and I can get their IP addresses via ifconfig, assuming I'm connected to the console.



I now want to connect to these containers via ssh. How do I get their IP address in such a way that I can write a script? I also don't want to set the addresses manually (but I'll do it, if that's the only option).



So far, I've tried using lxc-start, but the machine doesn't have an IP address before I run /sbin/init.










share|improve this question























  • Same problem here, related to this bug-report, bugs.launchpad.net/ubuntu/+source/lxc/+bug/1389954 that contains corroborative info on this not working in Ubuntu 16.04 Server Edtions, but being ok in Desktops where dnsmasq is kicked of by NetworkManager (as suggested in; askubuntu.com/a/545265/599087 by 'forest'). osdir.com/ml/ubuntu-bugs/2016-10/msg05441.html

    – OpenITeX
    Oct 13 '16 at 0:31
















19















I've written a few scripts to manage LXC containers, and I can get their IP addresses via ifconfig, assuming I'm connected to the console.



I now want to connect to these containers via ssh. How do I get their IP address in such a way that I can write a script? I also don't want to set the addresses manually (but I'll do it, if that's the only option).



So far, I've tried using lxc-start, but the machine doesn't have an IP address before I run /sbin/init.










share|improve this question























  • Same problem here, related to this bug-report, bugs.launchpad.net/ubuntu/+source/lxc/+bug/1389954 that contains corroborative info on this not working in Ubuntu 16.04 Server Edtions, but being ok in Desktops where dnsmasq is kicked of by NetworkManager (as suggested in; askubuntu.com/a/545265/599087 by 'forest'). osdir.com/ml/ubuntu-bugs/2016-10/msg05441.html

    – OpenITeX
    Oct 13 '16 at 0:31














19












19








19


4






I've written a few scripts to manage LXC containers, and I can get their IP addresses via ifconfig, assuming I'm connected to the console.



I now want to connect to these containers via ssh. How do I get their IP address in such a way that I can write a script? I also don't want to set the addresses manually (but I'll do it, if that's the only option).



So far, I've tried using lxc-start, but the machine doesn't have an IP address before I run /sbin/init.










share|improve this question














I've written a few scripts to manage LXC containers, and I can get their IP addresses via ifconfig, assuming I'm connected to the console.



I now want to connect to these containers via ssh. How do I get their IP address in such a way that I can write a script? I also don't want to set the addresses manually (but I'll do it, if that's the only option).



So far, I've tried using lxc-start, but the machine doesn't have an IP address before I run /sbin/init.







12.04 lxc






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 18 '13 at 16:41









Stefano PalazzoStefano Palazzo

63.3k33183216




63.3k33183216













  • Same problem here, related to this bug-report, bugs.launchpad.net/ubuntu/+source/lxc/+bug/1389954 that contains corroborative info on this not working in Ubuntu 16.04 Server Edtions, but being ok in Desktops where dnsmasq is kicked of by NetworkManager (as suggested in; askubuntu.com/a/545265/599087 by 'forest'). osdir.com/ml/ubuntu-bugs/2016-10/msg05441.html

    – OpenITeX
    Oct 13 '16 at 0:31



















  • Same problem here, related to this bug-report, bugs.launchpad.net/ubuntu/+source/lxc/+bug/1389954 that contains corroborative info on this not working in Ubuntu 16.04 Server Edtions, but being ok in Desktops where dnsmasq is kicked of by NetworkManager (as suggested in; askubuntu.com/a/545265/599087 by 'forest'). osdir.com/ml/ubuntu-bugs/2016-10/msg05441.html

    – OpenITeX
    Oct 13 '16 at 0:31

















Same problem here, related to this bug-report, bugs.launchpad.net/ubuntu/+source/lxc/+bug/1389954 that contains corroborative info on this not working in Ubuntu 16.04 Server Edtions, but being ok in Desktops where dnsmasq is kicked of by NetworkManager (as suggested in; askubuntu.com/a/545265/599087 by 'forest'). osdir.com/ml/ubuntu-bugs/2016-10/msg05441.html

– OpenITeX
Oct 13 '16 at 0:31





Same problem here, related to this bug-report, bugs.launchpad.net/ubuntu/+source/lxc/+bug/1389954 that contains corroborative info on this not working in Ubuntu 16.04 Server Edtions, but being ok in Desktops where dnsmasq is kicked of by NetworkManager (as suggested in; askubuntu.com/a/545265/599087 by 'forest'). osdir.com/ml/ubuntu-bugs/2016-10/msg05441.html

– OpenITeX
Oct 13 '16 at 0:31










11 Answers
11






active

oldest

votes


















10














The easiest way to do this now is:



lxc-info -n container-name -iH


This returns the IP address with no other text.



The -i option specifies that the IP address should be returned and the -H option disables human readable output i.e. labels. For more info see the lxc-info man page.



EDIT for newer version of LXC:



lxc info container-name



Then you get detailed info. Look at the "Ips:" block, which should look like the one below. You might one to grab the first IPv4 address (10.121.48.241) in this case:



Ips:
eth0: inet 10.121.48.241 vethSBP4RR
eth0: inet6 fda5:b9a9:f3b9:ba32:216:3eff:fe4a:4d7d vethSBP4RR
eth0: inet6 fe80::216:3eff:fe4a:4d7d vethSBP4RR
lo: inet 127.0.0.1
lo: inet6 ::1





share|improve this answer


























  • Working with LXD and unprivileged containers, this command is not of use. How would you get that information with the default unprivilleged containers promoted by LXD at 2017?

    – jgomo3
    Jun 1 '17 at 14:28



















10














Seeing as running things in containers doesn't appear to be supported in Ubuntu, my next best suggestion is to look at the IP address leases that dnsmasq is handing out. That's really simple:



$ cat /var/lib/misc/dnsmasq.leases
1363699477 00:16:3e:4a:ce:a4 10.0.3.83 containername *


There are only two parts there that are of any use, so we can format that up a lot nicer:



$ awk '{ print $4,$3 }' /var/lib/misc/dnsmasq.leases | column -t
containername 10.0.3.83





share|improve this answer
























  • That works perfectly!

    – Stefano Palazzo
    Mar 20 '13 at 15:12






  • 2





    For reference: new versions of LXC (i.e. the one in Raring) show the IP address in the output of lxc-ls --fancy.

    – Stefano Palazzo
    Sep 9 '13 at 13:19











  • FYI, on Ubuntu the filename of this IP leases record contains lxc bridge name eg dnsmasq.lxcbr0.leases

    – Flint
    May 5 '14 at 13:16





















7














Technically speaking you should be able to use lxc-attach to connect and fire in a command (and process the output), like so:



sudo lxc-attach --name containername -- ifconfig


This requires the container to be running.



Note: I couldn't get this to work. I installed LXC and tried this but just saw a mush of namespace errors, missing files and other nonsense. But my only experience with LXC is the 10 minutes I've spent on this question. It might work. It might not. Good luck!






share|improve this answer
























  • And of course, upgrade that to ifconfig eth0 | grep -Eo 'addr:[0-9.]+' | cut -d: -f2 if you just want the IP address.

    – Oli
    Mar 18 '13 at 17:33











  • Unfortunately I'm getting the same kind of errors. No such file or directory - failed to open '/proc/28741/ns/pid' and failed to enter the namespace. I found a bug that describes the problem precisely, but it's from 2010.

    – Stefano Palazzo
    Mar 19 '13 at 8:11













  • I had to use full path to ifconfig: lxc-attach -n container /sbin/ifconfig

    – Epeli
    Oct 19 '13 at 8:58













  • As help.ubuntu.com/12.04/serverguide/lxc.html says, lxc-attach is not supported.

    – ciastek
    Jan 22 '14 at 1:17



















5














This works on Ubuntu 14.04:



lxc-info -n $name -i


and if you want only the IP address (useful for scripts), (thanks @JulianHLam):



lxc-info -n $name -iH





share|improve this answer


























  • If calling via a script, you can even do lxc-info -n $name -Hi to get just the IP without extranneous whitespace

    – Julian H. Lam
    Apr 1 '15 at 18:50





















4














Or query dnsmasq (which gives IPs to the containers)



dig @10.0.3.1 $container-name +short





share|improve this answer































    1














    The command below replaces lxc-attach example in previous post



    sudo lxc-execute --name containername --rcfile /usr/share/doc/lxc/examples/lxc-macvlan.conf /sbin/ifconfig


    It runs ifconfig inside the container and shows the output.



    The --rcfile argument might not be required. Without it the command failed with 

    lxc-execute: No such file or directory - failed to exec /usr/lib/lxc/lxc-init

    lxc-execute: invalid sequence number 1. expected 2


    It sounds like something is not configured properly. As a workaround I've used the predefined configuration template provided by the LXC documentation to make it work without further investigation.






    share|improve this answer

































      1














      If you are running LXD, you may find this command useful for obtaining the IP address of a running container



      lxc exec <container-name> -- ip addr show eth0 | grep "inetb" | awk '{print $2}' | cut -d/ -f1





      share|improve this answer































        1














        Python version to do it :



        #!/usr/bin/python
        from pylxd import Client

        client = Client(endpoint='https://10.185.96.208:8443', verify=False, cert=('.config/lxc/client.crt', '.config/lxc/client.key'))

        myCtr = client.containers.get('YOUR_CTR_NAME')
        addresses = myCtr.state().network['eth0']['addresses']
        for a in addresses:
        if(a['scope'] == 'global'):
        print "Found IP [%s]" %(a['address'])
        break





        share|improve this answer































          0














          sudo lxc-ls --fancy -F ipv4 $my_container_name | tail -1






          share|improve this answer































            0














            Let dnsmasq do it for you.



            Configure your host machine's dnsmasq instance to query lxc's dnsmasq instance for the .lxc top-level domain.



            In /etc/default/lxc-net, uncomment this line:



            LXC_DOMAIN="lxc"


            If your host's dnsmasq instance is launched by NetworkManager (as is the case with most current Ubuntu desktop installations) create a file called /etc/NetworkManager/dnsmasq.d/lxc.conf with this line inside:



            server=/lxc/10.0.3.1


            If your host's dnsmasq is launched by something other than NetworkManager, add that line to /etc/dnsmasq.d-available/lxc instead:



            server=/lxc/10.0.3.1


            Then restart things so they pick up the changes:



            service lxc-net stop
            service lxc-net start
            service network-manager restart


            You might have to restart your lxc containers or make them request new DHCP leases before they appear in DNS. (I don't remember whether it was necessary when I did this.) It's also worth mentioning that I saw a bug report about lxc-net not picking up dnsmasq changes when it was restarted, so you might want to reboot your host system just to be sure.



            Then try it:



            $ host mycontainer.lxc
            mycontainer.lxc has address 10.0.3.21

            $ ssh ubuntu@mycontainer.lxc
            Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-39-generic x86_64)
            ubuntu@mycontainer:~$





            share|improve this answer

































              0














              Simple answer is



              sudo lxc-ls -f | grep "container_name"


              If you dont remember the container_name just type sudo lxc-ls -f.






              share|improve this answer























                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "89"
                };
                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%2faskubuntu.com%2fquestions%2f269588%2fhow-do-i-get-the-ip-address-of-an-lxc-container%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                11 Answers
                11






                active

                oldest

                votes








                11 Answers
                11






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                10














                The easiest way to do this now is:



                lxc-info -n container-name -iH


                This returns the IP address with no other text.



                The -i option specifies that the IP address should be returned and the -H option disables human readable output i.e. labels. For more info see the lxc-info man page.



                EDIT for newer version of LXC:



                lxc info container-name



                Then you get detailed info. Look at the "Ips:" block, which should look like the one below. You might one to grab the first IPv4 address (10.121.48.241) in this case:



                Ips:
                eth0: inet 10.121.48.241 vethSBP4RR
                eth0: inet6 fda5:b9a9:f3b9:ba32:216:3eff:fe4a:4d7d vethSBP4RR
                eth0: inet6 fe80::216:3eff:fe4a:4d7d vethSBP4RR
                lo: inet 127.0.0.1
                lo: inet6 ::1





                share|improve this answer


























                • Working with LXD and unprivileged containers, this command is not of use. How would you get that information with the default unprivilleged containers promoted by LXD at 2017?

                  – jgomo3
                  Jun 1 '17 at 14:28
















                10














                The easiest way to do this now is:



                lxc-info -n container-name -iH


                This returns the IP address with no other text.



                The -i option specifies that the IP address should be returned and the -H option disables human readable output i.e. labels. For more info see the lxc-info man page.



                EDIT for newer version of LXC:



                lxc info container-name



                Then you get detailed info. Look at the "Ips:" block, which should look like the one below. You might one to grab the first IPv4 address (10.121.48.241) in this case:



                Ips:
                eth0: inet 10.121.48.241 vethSBP4RR
                eth0: inet6 fda5:b9a9:f3b9:ba32:216:3eff:fe4a:4d7d vethSBP4RR
                eth0: inet6 fe80::216:3eff:fe4a:4d7d vethSBP4RR
                lo: inet 127.0.0.1
                lo: inet6 ::1





                share|improve this answer


























                • Working with LXD and unprivileged containers, this command is not of use. How would you get that information with the default unprivilleged containers promoted by LXD at 2017?

                  – jgomo3
                  Jun 1 '17 at 14:28














                10












                10








                10







                The easiest way to do this now is:



                lxc-info -n container-name -iH


                This returns the IP address with no other text.



                The -i option specifies that the IP address should be returned and the -H option disables human readable output i.e. labels. For more info see the lxc-info man page.



                EDIT for newer version of LXC:



                lxc info container-name



                Then you get detailed info. Look at the "Ips:" block, which should look like the one below. You might one to grab the first IPv4 address (10.121.48.241) in this case:



                Ips:
                eth0: inet 10.121.48.241 vethSBP4RR
                eth0: inet6 fda5:b9a9:f3b9:ba32:216:3eff:fe4a:4d7d vethSBP4RR
                eth0: inet6 fe80::216:3eff:fe4a:4d7d vethSBP4RR
                lo: inet 127.0.0.1
                lo: inet6 ::1





                share|improve this answer















                The easiest way to do this now is:



                lxc-info -n container-name -iH


                This returns the IP address with no other text.



                The -i option specifies that the IP address should be returned and the -H option disables human readable output i.e. labels. For more info see the lxc-info man page.



                EDIT for newer version of LXC:



                lxc info container-name



                Then you get detailed info. Look at the "Ips:" block, which should look like the one below. You might one to grab the first IPv4 address (10.121.48.241) in this case:



                Ips:
                eth0: inet 10.121.48.241 vethSBP4RR
                eth0: inet6 fda5:b9a9:f3b9:ba32:216:3eff:fe4a:4d7d vethSBP4RR
                eth0: inet6 fe80::216:3eff:fe4a:4d7d vethSBP4RR
                lo: inet 127.0.0.1
                lo: inet6 ::1






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 8 hours ago









                Tamás Márton

                1034




                1034










                answered May 14 '15 at 13:02









                AndyAndy

                11612




                11612













                • Working with LXD and unprivileged containers, this command is not of use. How would you get that information with the default unprivilleged containers promoted by LXD at 2017?

                  – jgomo3
                  Jun 1 '17 at 14:28



















                • Working with LXD and unprivileged containers, this command is not of use. How would you get that information with the default unprivilleged containers promoted by LXD at 2017?

                  – jgomo3
                  Jun 1 '17 at 14:28

















                Working with LXD and unprivileged containers, this command is not of use. How would you get that information with the default unprivilleged containers promoted by LXD at 2017?

                – jgomo3
                Jun 1 '17 at 14:28





                Working with LXD and unprivileged containers, this command is not of use. How would you get that information with the default unprivilleged containers promoted by LXD at 2017?

                – jgomo3
                Jun 1 '17 at 14:28













                10














                Seeing as running things in containers doesn't appear to be supported in Ubuntu, my next best suggestion is to look at the IP address leases that dnsmasq is handing out. That's really simple:



                $ cat /var/lib/misc/dnsmasq.leases
                1363699477 00:16:3e:4a:ce:a4 10.0.3.83 containername *


                There are only two parts there that are of any use, so we can format that up a lot nicer:



                $ awk '{ print $4,$3 }' /var/lib/misc/dnsmasq.leases | column -t
                containername 10.0.3.83





                share|improve this answer
























                • That works perfectly!

                  – Stefano Palazzo
                  Mar 20 '13 at 15:12






                • 2





                  For reference: new versions of LXC (i.e. the one in Raring) show the IP address in the output of lxc-ls --fancy.

                  – Stefano Palazzo
                  Sep 9 '13 at 13:19











                • FYI, on Ubuntu the filename of this IP leases record contains lxc bridge name eg dnsmasq.lxcbr0.leases

                  – Flint
                  May 5 '14 at 13:16


















                10














                Seeing as running things in containers doesn't appear to be supported in Ubuntu, my next best suggestion is to look at the IP address leases that dnsmasq is handing out. That's really simple:



                $ cat /var/lib/misc/dnsmasq.leases
                1363699477 00:16:3e:4a:ce:a4 10.0.3.83 containername *


                There are only two parts there that are of any use, so we can format that up a lot nicer:



                $ awk '{ print $4,$3 }' /var/lib/misc/dnsmasq.leases | column -t
                containername 10.0.3.83





                share|improve this answer
























                • That works perfectly!

                  – Stefano Palazzo
                  Mar 20 '13 at 15:12






                • 2





                  For reference: new versions of LXC (i.e. the one in Raring) show the IP address in the output of lxc-ls --fancy.

                  – Stefano Palazzo
                  Sep 9 '13 at 13:19











                • FYI, on Ubuntu the filename of this IP leases record contains lxc bridge name eg dnsmasq.lxcbr0.leases

                  – Flint
                  May 5 '14 at 13:16
















                10












                10








                10







                Seeing as running things in containers doesn't appear to be supported in Ubuntu, my next best suggestion is to look at the IP address leases that dnsmasq is handing out. That's really simple:



                $ cat /var/lib/misc/dnsmasq.leases
                1363699477 00:16:3e:4a:ce:a4 10.0.3.83 containername *


                There are only two parts there that are of any use, so we can format that up a lot nicer:



                $ awk '{ print $4,$3 }' /var/lib/misc/dnsmasq.leases | column -t
                containername 10.0.3.83





                share|improve this answer













                Seeing as running things in containers doesn't appear to be supported in Ubuntu, my next best suggestion is to look at the IP address leases that dnsmasq is handing out. That's really simple:



                $ cat /var/lib/misc/dnsmasq.leases
                1363699477 00:16:3e:4a:ce:a4 10.0.3.83 containername *


                There are only two parts there that are of any use, so we can format that up a lot nicer:



                $ awk '{ print $4,$3 }' /var/lib/misc/dnsmasq.leases | column -t
                containername 10.0.3.83






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 19 '13 at 13:03









                OliOli

                222k88563764




                222k88563764













                • That works perfectly!

                  – Stefano Palazzo
                  Mar 20 '13 at 15:12






                • 2





                  For reference: new versions of LXC (i.e. the one in Raring) show the IP address in the output of lxc-ls --fancy.

                  – Stefano Palazzo
                  Sep 9 '13 at 13:19











                • FYI, on Ubuntu the filename of this IP leases record contains lxc bridge name eg dnsmasq.lxcbr0.leases

                  – Flint
                  May 5 '14 at 13:16





















                • That works perfectly!

                  – Stefano Palazzo
                  Mar 20 '13 at 15:12






                • 2





                  For reference: new versions of LXC (i.e. the one in Raring) show the IP address in the output of lxc-ls --fancy.

                  – Stefano Palazzo
                  Sep 9 '13 at 13:19











                • FYI, on Ubuntu the filename of this IP leases record contains lxc bridge name eg dnsmasq.lxcbr0.leases

                  – Flint
                  May 5 '14 at 13:16



















                That works perfectly!

                – Stefano Palazzo
                Mar 20 '13 at 15:12





                That works perfectly!

                – Stefano Palazzo
                Mar 20 '13 at 15:12




                2




                2





                For reference: new versions of LXC (i.e. the one in Raring) show the IP address in the output of lxc-ls --fancy.

                – Stefano Palazzo
                Sep 9 '13 at 13:19





                For reference: new versions of LXC (i.e. the one in Raring) show the IP address in the output of lxc-ls --fancy.

                – Stefano Palazzo
                Sep 9 '13 at 13:19













                FYI, on Ubuntu the filename of this IP leases record contains lxc bridge name eg dnsmasq.lxcbr0.leases

                – Flint
                May 5 '14 at 13:16







                FYI, on Ubuntu the filename of this IP leases record contains lxc bridge name eg dnsmasq.lxcbr0.leases

                – Flint
                May 5 '14 at 13:16













                7














                Technically speaking you should be able to use lxc-attach to connect and fire in a command (and process the output), like so:



                sudo lxc-attach --name containername -- ifconfig


                This requires the container to be running.



                Note: I couldn't get this to work. I installed LXC and tried this but just saw a mush of namespace errors, missing files and other nonsense. But my only experience with LXC is the 10 minutes I've spent on this question. It might work. It might not. Good luck!






                share|improve this answer
























                • And of course, upgrade that to ifconfig eth0 | grep -Eo 'addr:[0-9.]+' | cut -d: -f2 if you just want the IP address.

                  – Oli
                  Mar 18 '13 at 17:33











                • Unfortunately I'm getting the same kind of errors. No such file or directory - failed to open '/proc/28741/ns/pid' and failed to enter the namespace. I found a bug that describes the problem precisely, but it's from 2010.

                  – Stefano Palazzo
                  Mar 19 '13 at 8:11













                • I had to use full path to ifconfig: lxc-attach -n container /sbin/ifconfig

                  – Epeli
                  Oct 19 '13 at 8:58













                • As help.ubuntu.com/12.04/serverguide/lxc.html says, lxc-attach is not supported.

                  – ciastek
                  Jan 22 '14 at 1:17
















                7














                Technically speaking you should be able to use lxc-attach to connect and fire in a command (and process the output), like so:



                sudo lxc-attach --name containername -- ifconfig


                This requires the container to be running.



                Note: I couldn't get this to work. I installed LXC and tried this but just saw a mush of namespace errors, missing files and other nonsense. But my only experience with LXC is the 10 minutes I've spent on this question. It might work. It might not. Good luck!






                share|improve this answer
























                • And of course, upgrade that to ifconfig eth0 | grep -Eo 'addr:[0-9.]+' | cut -d: -f2 if you just want the IP address.

                  – Oli
                  Mar 18 '13 at 17:33











                • Unfortunately I'm getting the same kind of errors. No such file or directory - failed to open '/proc/28741/ns/pid' and failed to enter the namespace. I found a bug that describes the problem precisely, but it's from 2010.

                  – Stefano Palazzo
                  Mar 19 '13 at 8:11













                • I had to use full path to ifconfig: lxc-attach -n container /sbin/ifconfig

                  – Epeli
                  Oct 19 '13 at 8:58













                • As help.ubuntu.com/12.04/serverguide/lxc.html says, lxc-attach is not supported.

                  – ciastek
                  Jan 22 '14 at 1:17














                7












                7








                7







                Technically speaking you should be able to use lxc-attach to connect and fire in a command (and process the output), like so:



                sudo lxc-attach --name containername -- ifconfig


                This requires the container to be running.



                Note: I couldn't get this to work. I installed LXC and tried this but just saw a mush of namespace errors, missing files and other nonsense. But my only experience with LXC is the 10 minutes I've spent on this question. It might work. It might not. Good luck!






                share|improve this answer













                Technically speaking you should be able to use lxc-attach to connect and fire in a command (and process the output), like so:



                sudo lxc-attach --name containername -- ifconfig


                This requires the container to be running.



                Note: I couldn't get this to work. I installed LXC and tried this but just saw a mush of namespace errors, missing files and other nonsense. But my only experience with LXC is the 10 minutes I've spent on this question. It might work. It might not. Good luck!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 18 '13 at 17:21









                OliOli

                222k88563764




                222k88563764













                • And of course, upgrade that to ifconfig eth0 | grep -Eo 'addr:[0-9.]+' | cut -d: -f2 if you just want the IP address.

                  – Oli
                  Mar 18 '13 at 17:33











                • Unfortunately I'm getting the same kind of errors. No such file or directory - failed to open '/proc/28741/ns/pid' and failed to enter the namespace. I found a bug that describes the problem precisely, but it's from 2010.

                  – Stefano Palazzo
                  Mar 19 '13 at 8:11













                • I had to use full path to ifconfig: lxc-attach -n container /sbin/ifconfig

                  – Epeli
                  Oct 19 '13 at 8:58













                • As help.ubuntu.com/12.04/serverguide/lxc.html says, lxc-attach is not supported.

                  – ciastek
                  Jan 22 '14 at 1:17



















                • And of course, upgrade that to ifconfig eth0 | grep -Eo 'addr:[0-9.]+' | cut -d: -f2 if you just want the IP address.

                  – Oli
                  Mar 18 '13 at 17:33











                • Unfortunately I'm getting the same kind of errors. No such file or directory - failed to open '/proc/28741/ns/pid' and failed to enter the namespace. I found a bug that describes the problem precisely, but it's from 2010.

                  – Stefano Palazzo
                  Mar 19 '13 at 8:11













                • I had to use full path to ifconfig: lxc-attach -n container /sbin/ifconfig

                  – Epeli
                  Oct 19 '13 at 8:58













                • As help.ubuntu.com/12.04/serverguide/lxc.html says, lxc-attach is not supported.

                  – ciastek
                  Jan 22 '14 at 1:17

















                And of course, upgrade that to ifconfig eth0 | grep -Eo 'addr:[0-9.]+' | cut -d: -f2 if you just want the IP address.

                – Oli
                Mar 18 '13 at 17:33





                And of course, upgrade that to ifconfig eth0 | grep -Eo 'addr:[0-9.]+' | cut -d: -f2 if you just want the IP address.

                – Oli
                Mar 18 '13 at 17:33













                Unfortunately I'm getting the same kind of errors. No such file or directory - failed to open '/proc/28741/ns/pid' and failed to enter the namespace. I found a bug that describes the problem precisely, but it's from 2010.

                – Stefano Palazzo
                Mar 19 '13 at 8:11







                Unfortunately I'm getting the same kind of errors. No such file or directory - failed to open '/proc/28741/ns/pid' and failed to enter the namespace. I found a bug that describes the problem precisely, but it's from 2010.

                – Stefano Palazzo
                Mar 19 '13 at 8:11















                I had to use full path to ifconfig: lxc-attach -n container /sbin/ifconfig

                – Epeli
                Oct 19 '13 at 8:58







                I had to use full path to ifconfig: lxc-attach -n container /sbin/ifconfig

                – Epeli
                Oct 19 '13 at 8:58















                As help.ubuntu.com/12.04/serverguide/lxc.html says, lxc-attach is not supported.

                – ciastek
                Jan 22 '14 at 1:17





                As help.ubuntu.com/12.04/serverguide/lxc.html says, lxc-attach is not supported.

                – ciastek
                Jan 22 '14 at 1:17











                5














                This works on Ubuntu 14.04:



                lxc-info -n $name -i


                and if you want only the IP address (useful for scripts), (thanks @JulianHLam):



                lxc-info -n $name -iH





                share|improve this answer


























                • If calling via a script, you can even do lxc-info -n $name -Hi to get just the IP without extranneous whitespace

                  – Julian H. Lam
                  Apr 1 '15 at 18:50


















                5














                This works on Ubuntu 14.04:



                lxc-info -n $name -i


                and if you want only the IP address (useful for scripts), (thanks @JulianHLam):



                lxc-info -n $name -iH





                share|improve this answer


























                • If calling via a script, you can even do lxc-info -n $name -Hi to get just the IP without extranneous whitespace

                  – Julian H. Lam
                  Apr 1 '15 at 18:50
















                5












                5








                5







                This works on Ubuntu 14.04:



                lxc-info -n $name -i


                and if you want only the IP address (useful for scripts), (thanks @JulianHLam):



                lxc-info -n $name -iH





                share|improve this answer















                This works on Ubuntu 14.04:



                lxc-info -n $name -i


                and if you want only the IP address (useful for scripts), (thanks @JulianHLam):



                lxc-info -n $name -iH






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 2 '15 at 2:31

























                answered Apr 11 '14 at 11:07









                vaabvaab

                729815




                729815













                • If calling via a script, you can even do lxc-info -n $name -Hi to get just the IP without extranneous whitespace

                  – Julian H. Lam
                  Apr 1 '15 at 18:50





















                • If calling via a script, you can even do lxc-info -n $name -Hi to get just the IP without extranneous whitespace

                  – Julian H. Lam
                  Apr 1 '15 at 18:50



















                If calling via a script, you can even do lxc-info -n $name -Hi to get just the IP without extranneous whitespace

                – Julian H. Lam
                Apr 1 '15 at 18:50







                If calling via a script, you can even do lxc-info -n $name -Hi to get just the IP without extranneous whitespace

                – Julian H. Lam
                Apr 1 '15 at 18:50













                4














                Or query dnsmasq (which gives IPs to the containers)



                dig @10.0.3.1 $container-name +short





                share|improve this answer




























                  4














                  Or query dnsmasq (which gives IPs to the containers)



                  dig @10.0.3.1 $container-name +short





                  share|improve this answer


























                    4












                    4








                    4







                    Or query dnsmasq (which gives IPs to the containers)



                    dig @10.0.3.1 $container-name +short





                    share|improve this answer













                    Or query dnsmasq (which gives IPs to the containers)



                    dig @10.0.3.1 $container-name +short






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 21 '13 at 11:12









                    Carl HörbergCarl Hörberg

                    1636




                    1636























                        1














                        The command below replaces lxc-attach example in previous post



                        sudo lxc-execute --name containername --rcfile /usr/share/doc/lxc/examples/lxc-macvlan.conf /sbin/ifconfig


                        It runs ifconfig inside the container and shows the output.



                        The --rcfile argument might not be required. Without it the command failed with 

                        lxc-execute: No such file or directory - failed to exec /usr/lib/lxc/lxc-init

                        lxc-execute: invalid sequence number 1. expected 2


                        It sounds like something is not configured properly. As a workaround I've used the predefined configuration template provided by the LXC documentation to make it work without further investigation.






                        share|improve this answer






























                          1














                          The command below replaces lxc-attach example in previous post



                          sudo lxc-execute --name containername --rcfile /usr/share/doc/lxc/examples/lxc-macvlan.conf /sbin/ifconfig


                          It runs ifconfig inside the container and shows the output.



                          The --rcfile argument might not be required. Without it the command failed with 

                          lxc-execute: No such file or directory - failed to exec /usr/lib/lxc/lxc-init

                          lxc-execute: invalid sequence number 1. expected 2


                          It sounds like something is not configured properly. As a workaround I've used the predefined configuration template provided by the LXC documentation to make it work without further investigation.






                          share|improve this answer




























                            1












                            1








                            1







                            The command below replaces lxc-attach example in previous post



                            sudo lxc-execute --name containername --rcfile /usr/share/doc/lxc/examples/lxc-macvlan.conf /sbin/ifconfig


                            It runs ifconfig inside the container and shows the output.



                            The --rcfile argument might not be required. Without it the command failed with 

                            lxc-execute: No such file or directory - failed to exec /usr/lib/lxc/lxc-init

                            lxc-execute: invalid sequence number 1. expected 2


                            It sounds like something is not configured properly. As a workaround I've used the predefined configuration template provided by the LXC documentation to make it work without further investigation.






                            share|improve this answer















                            The command below replaces lxc-attach example in previous post



                            sudo lxc-execute --name containername --rcfile /usr/share/doc/lxc/examples/lxc-macvlan.conf /sbin/ifconfig


                            It runs ifconfig inside the container and shows the output.



                            The --rcfile argument might not be required. Without it the command failed with 

                            lxc-execute: No such file or directory - failed to exec /usr/lib/lxc/lxc-init

                            lxc-execute: invalid sequence number 1. expected 2


                            It sounds like something is not configured properly. As a workaround I've used the predefined configuration template provided by the LXC documentation to make it work without further investigation.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 31 '14 at 18:01









                            Lucio

                            12.6k2385161




                            12.6k2385161










                            answered Jan 31 '14 at 17:29









                            cfalguierecfalguiere

                            112




                            112























                                1














                                If you are running LXD, you may find this command useful for obtaining the IP address of a running container



                                lxc exec <container-name> -- ip addr show eth0 | grep "inetb" | awk '{print $2}' | cut -d/ -f1





                                share|improve this answer




























                                  1














                                  If you are running LXD, you may find this command useful for obtaining the IP address of a running container



                                  lxc exec <container-name> -- ip addr show eth0 | grep "inetb" | awk '{print $2}' | cut -d/ -f1





                                  share|improve this answer


























                                    1












                                    1








                                    1







                                    If you are running LXD, you may find this command useful for obtaining the IP address of a running container



                                    lxc exec <container-name> -- ip addr show eth0 | grep "inetb" | awk '{print $2}' | cut -d/ -f1





                                    share|improve this answer













                                    If you are running LXD, you may find this command useful for obtaining the IP address of a running container



                                    lxc exec <container-name> -- ip addr show eth0 | grep "inetb" | awk '{print $2}' | cut -d/ -f1






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jul 14 '16 at 0:09









                                    anastymousanastymous

                                    111




                                    111























                                        1














                                        Python version to do it :



                                        #!/usr/bin/python
                                        from pylxd import Client

                                        client = Client(endpoint='https://10.185.96.208:8443', verify=False, cert=('.config/lxc/client.crt', '.config/lxc/client.key'))

                                        myCtr = client.containers.get('YOUR_CTR_NAME')
                                        addresses = myCtr.state().network['eth0']['addresses']
                                        for a in addresses:
                                        if(a['scope'] == 'global'):
                                        print "Found IP [%s]" %(a['address'])
                                        break





                                        share|improve this answer




























                                          1














                                          Python version to do it :



                                          #!/usr/bin/python
                                          from pylxd import Client

                                          client = Client(endpoint='https://10.185.96.208:8443', verify=False, cert=('.config/lxc/client.crt', '.config/lxc/client.key'))

                                          myCtr = client.containers.get('YOUR_CTR_NAME')
                                          addresses = myCtr.state().network['eth0']['addresses']
                                          for a in addresses:
                                          if(a['scope'] == 'global'):
                                          print "Found IP [%s]" %(a['address'])
                                          break





                                          share|improve this answer


























                                            1












                                            1








                                            1







                                            Python version to do it :



                                            #!/usr/bin/python
                                            from pylxd import Client

                                            client = Client(endpoint='https://10.185.96.208:8443', verify=False, cert=('.config/lxc/client.crt', '.config/lxc/client.key'))

                                            myCtr = client.containers.get('YOUR_CTR_NAME')
                                            addresses = myCtr.state().network['eth0']['addresses']
                                            for a in addresses:
                                            if(a['scope'] == 'global'):
                                            print "Found IP [%s]" %(a['address'])
                                            break





                                            share|improve this answer













                                            Python version to do it :



                                            #!/usr/bin/python
                                            from pylxd import Client

                                            client = Client(endpoint='https://10.185.96.208:8443', verify=False, cert=('.config/lxc/client.crt', '.config/lxc/client.key'))

                                            myCtr = client.containers.get('YOUR_CTR_NAME')
                                            addresses = myCtr.state().network['eth0']['addresses']
                                            for a in addresses:
                                            if(a['scope'] == 'global'):
                                            print "Found IP [%s]" %(a['address'])
                                            break






                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Nov 4 '16 at 10:55









                                            EktorEktor

                                            111




                                            111























                                                0














                                                sudo lxc-ls --fancy -F ipv4 $my_container_name | tail -1






                                                share|improve this answer




























                                                  0














                                                  sudo lxc-ls --fancy -F ipv4 $my_container_name | tail -1






                                                  share|improve this answer


























                                                    0












                                                    0








                                                    0







                                                    sudo lxc-ls --fancy -F ipv4 $my_container_name | tail -1






                                                    share|improve this answer













                                                    sudo lxc-ls --fancy -F ipv4 $my_container_name | tail -1







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered May 5 '14 at 12:16









                                                    ramiggramigg

                                                    1011




                                                    1011























                                                        0














                                                        Let dnsmasq do it for you.



                                                        Configure your host machine's dnsmasq instance to query lxc's dnsmasq instance for the .lxc top-level domain.



                                                        In /etc/default/lxc-net, uncomment this line:



                                                        LXC_DOMAIN="lxc"


                                                        If your host's dnsmasq instance is launched by NetworkManager (as is the case with most current Ubuntu desktop installations) create a file called /etc/NetworkManager/dnsmasq.d/lxc.conf with this line inside:



                                                        server=/lxc/10.0.3.1


                                                        If your host's dnsmasq is launched by something other than NetworkManager, add that line to /etc/dnsmasq.d-available/lxc instead:



                                                        server=/lxc/10.0.3.1


                                                        Then restart things so they pick up the changes:



                                                        service lxc-net stop
                                                        service lxc-net start
                                                        service network-manager restart


                                                        You might have to restart your lxc containers or make them request new DHCP leases before they appear in DNS. (I don't remember whether it was necessary when I did this.) It's also worth mentioning that I saw a bug report about lxc-net not picking up dnsmasq changes when it was restarted, so you might want to reboot your host system just to be sure.



                                                        Then try it:



                                                        $ host mycontainer.lxc
                                                        mycontainer.lxc has address 10.0.3.21

                                                        $ ssh ubuntu@mycontainer.lxc
                                                        Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-39-generic x86_64)
                                                        ubuntu@mycontainer:~$





                                                        share|improve this answer






























                                                          0














                                                          Let dnsmasq do it for you.



                                                          Configure your host machine's dnsmasq instance to query lxc's dnsmasq instance for the .lxc top-level domain.



                                                          In /etc/default/lxc-net, uncomment this line:



                                                          LXC_DOMAIN="lxc"


                                                          If your host's dnsmasq instance is launched by NetworkManager (as is the case with most current Ubuntu desktop installations) create a file called /etc/NetworkManager/dnsmasq.d/lxc.conf with this line inside:



                                                          server=/lxc/10.0.3.1


                                                          If your host's dnsmasq is launched by something other than NetworkManager, add that line to /etc/dnsmasq.d-available/lxc instead:



                                                          server=/lxc/10.0.3.1


                                                          Then restart things so they pick up the changes:



                                                          service lxc-net stop
                                                          service lxc-net start
                                                          service network-manager restart


                                                          You might have to restart your lxc containers or make them request new DHCP leases before they appear in DNS. (I don't remember whether it was necessary when I did this.) It's also worth mentioning that I saw a bug report about lxc-net not picking up dnsmasq changes when it was restarted, so you might want to reboot your host system just to be sure.



                                                          Then try it:



                                                          $ host mycontainer.lxc
                                                          mycontainer.lxc has address 10.0.3.21

                                                          $ ssh ubuntu@mycontainer.lxc
                                                          Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-39-generic x86_64)
                                                          ubuntu@mycontainer:~$





                                                          share|improve this answer




























                                                            0












                                                            0








                                                            0







                                                            Let dnsmasq do it for you.



                                                            Configure your host machine's dnsmasq instance to query lxc's dnsmasq instance for the .lxc top-level domain.



                                                            In /etc/default/lxc-net, uncomment this line:



                                                            LXC_DOMAIN="lxc"


                                                            If your host's dnsmasq instance is launched by NetworkManager (as is the case with most current Ubuntu desktop installations) create a file called /etc/NetworkManager/dnsmasq.d/lxc.conf with this line inside:



                                                            server=/lxc/10.0.3.1


                                                            If your host's dnsmasq is launched by something other than NetworkManager, add that line to /etc/dnsmasq.d-available/lxc instead:



                                                            server=/lxc/10.0.3.1


                                                            Then restart things so they pick up the changes:



                                                            service lxc-net stop
                                                            service lxc-net start
                                                            service network-manager restart


                                                            You might have to restart your lxc containers or make them request new DHCP leases before they appear in DNS. (I don't remember whether it was necessary when I did this.) It's also worth mentioning that I saw a bug report about lxc-net not picking up dnsmasq changes when it was restarted, so you might want to reboot your host system just to be sure.



                                                            Then try it:



                                                            $ host mycontainer.lxc
                                                            mycontainer.lxc has address 10.0.3.21

                                                            $ ssh ubuntu@mycontainer.lxc
                                                            Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-39-generic x86_64)
                                                            ubuntu@mycontainer:~$





                                                            share|improve this answer















                                                            Let dnsmasq do it for you.



                                                            Configure your host machine's dnsmasq instance to query lxc's dnsmasq instance for the .lxc top-level domain.



                                                            In /etc/default/lxc-net, uncomment this line:



                                                            LXC_DOMAIN="lxc"


                                                            If your host's dnsmasq instance is launched by NetworkManager (as is the case with most current Ubuntu desktop installations) create a file called /etc/NetworkManager/dnsmasq.d/lxc.conf with this line inside:



                                                            server=/lxc/10.0.3.1


                                                            If your host's dnsmasq is launched by something other than NetworkManager, add that line to /etc/dnsmasq.d-available/lxc instead:



                                                            server=/lxc/10.0.3.1


                                                            Then restart things so they pick up the changes:



                                                            service lxc-net stop
                                                            service lxc-net start
                                                            service network-manager restart


                                                            You might have to restart your lxc containers or make them request new DHCP leases before they appear in DNS. (I don't remember whether it was necessary when I did this.) It's also worth mentioning that I saw a bug report about lxc-net not picking up dnsmasq changes when it was restarted, so you might want to reboot your host system just to be sure.



                                                            Then try it:



                                                            $ host mycontainer.lxc
                                                            mycontainer.lxc has address 10.0.3.21

                                                            $ ssh ubuntu@mycontainer.lxc
                                                            Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-39-generic x86_64)
                                                            ubuntu@mycontainer:~$






                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Nov 3 '14 at 20:22

























                                                            answered Nov 3 '14 at 20:17









                                                            ʇsәɹoɈʇsәɹoɈ

                                                            20917




                                                            20917























                                                                0














                                                                Simple answer is



                                                                sudo lxc-ls -f | grep "container_name"


                                                                If you dont remember the container_name just type sudo lxc-ls -f.






                                                                share|improve this answer




























                                                                  0














                                                                  Simple answer is



                                                                  sudo lxc-ls -f | grep "container_name"


                                                                  If you dont remember the container_name just type sudo lxc-ls -f.






                                                                  share|improve this answer


























                                                                    0












                                                                    0








                                                                    0







                                                                    Simple answer is



                                                                    sudo lxc-ls -f | grep "container_name"


                                                                    If you dont remember the container_name just type sudo lxc-ls -f.






                                                                    share|improve this answer













                                                                    Simple answer is



                                                                    sudo lxc-ls -f | grep "container_name"


                                                                    If you dont remember the container_name just type sudo lxc-ls -f.







                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Jan 26 '17 at 7:12









                                                                    user646873user646873

                                                                    1




                                                                    1






























                                                                        draft saved

                                                                        draft discarded




















































                                                                        Thanks for contributing an answer to Ask Ubuntu!


                                                                        • 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%2faskubuntu.com%2fquestions%2f269588%2fhow-do-i-get-the-ip-address-of-an-lxc-container%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

                                                                        How did Captain America manage to do this?

                                                                        迪纳利

                                                                        南乌拉尔铁路局