Skip to content

Instantly share code, notes, and snippets.

@ciastek
Created January 22, 2014 03:30
Show Gist options
  • Save ciastek/8553059 to your computer and use it in GitHub Desktop.
Save ciastek/8553059 to your computer and use it in GitHub Desktop.
Run script in LXC container, non-interactive
# create container with key authentication
sudo lxc-create -t ubuntu -n test -- -S /root/.ssh/id_rsa.pub
sudo lxc-start -n test -d
sudo lxc-wait -n test -s RUNNING
# enable passwordless sudo for ubuntu user
chroot /var/lib/lxc/test/rootfs sh -c "echo 'ubuntu ALL=NOPASSWD:ALL' > /etc/sudoers.d/ubuntu; chmod 440 /etc/sudoers.d/ubuntu"
# now you can run commands in container, example: install avahi-daemon
ssh `cat /var/lib/misc/dnsmasq.leases | grep --word-regexp test | awk '{print $3}'` -l ubuntu -o "StrictHostKeyChecking no" sudo apt-get install -y avahi-daemon
@gurubobnz
Copy link

Good gist thanks!
To get IP of running container you can use:

    CNAME=test
    IP=lxc-info -n $CNAME -i | cut -d: -f2 | sed -e 's/ //g'

Grepping it out of the leases worked fine once but not after I destroyed and recreated the container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment