Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 10 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 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
share|improve this answer
That works perfectly! – Stefano Palazzo Mar 20 '13 at 15:12
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

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 says, lxc-attach is not supported. – ciastek Jan 22 '14 at 1:17

Or query dnsmasq (which gives IPs to the containers)

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

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.

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

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:


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:


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


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

$ ssh ubuntu@mycontainer.lxc
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-39-generic x86_64)
share|improve this answer

Your Answer


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.