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.


11 Answers 11


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 ( in this case:

  eth0: inet   vethSBP4RR
  eth0: inet6   fda5:b9a9:f3b9:ba32:216:3eff:fe4a:4d7d  vethSBP4RR
  eth0: inet6   fe80::216:3eff:fe4a:4d7d    vethSBP4RR
  lo:   inet
  lo:   inet6   ::1
  • 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, 2017 at 14:28

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
  • 2
    For reference: new versions of LXC (i.e. the one in Raring) show the IP address in the output of lxc-ls --fancy. Sep 9, 2013 at 13:19
  • FYI, on Ubuntu the filename of this IP leases record contains lxc bridge name eg dnsmasq.lxcbr0.leases
    – Flint
    May 5, 2014 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!

  • 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, 2013 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. Mar 19, 2013 at 8:11
  • I had to use full path to ifconfig: lxc-attach -n container /sbin/ifconfig
    – Epeli
    Oct 19, 2013 at 8:58
  • As help.ubuntu.com/12.04/serverguide/lxc.html says, lxc-attach is not supported.
    – ciastek
    Jan 22, 2014 at 1:17

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
  • If calling via a script, you can even do lxc-info -n $name -Hi to get just the IP without extranneous whitespace Apr 1, 2015 at 18:50

Or query dnsmasq (which gives IPs to the containers)

dig @ $container-name +short

Python version to do it :

from pylxd import Client

client = Client(endpoint='', 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'])

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.


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 "inet\b" | awk '{print $2}' | cut -d/ -f1

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


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)

Simple answer is

sudo lxc-ls -f | grep "container_name"

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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