Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

7 Answers 7

up vote 9 down vote accepted

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 at 13:16

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 at 1:17

Or query dnsmasq (which gives IPs to the containers)

dig @10.0.3.1 $container-name +short
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

This works on Ubuntu 14.04:

lxc-info -n $name -i
share|improve this answer

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.