24

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 Answers11

13

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
Andy
  • 146
12

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
Oli
  • 299,380
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!

Oli
  • 299,380
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
vaab
  • 1,112
4

Or query dnsmasq (which gives IPs to the containers)

dig @10.0.3.1 $container-name +short
2

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
Ektor
  • 21
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.

Lucio
  • 19,191
  • 32
  • 112
  • 191
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 "inet\b" | awk '{print $2}' | cut -d/ -f1
0

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

ramigg
  • 101
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:~$
ʇsәɹoɈ
  • 964
  • 9
  • 12
0

Simple answer is

sudo lxc-ls -f | grep "container_name"

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