[lxc-users] What's the easiest way to spin up a container, create a user, and arrange for easy ssh?

Dan Kegel dank at kegel.com
Fri Jan 1 17:16:42 UTC 2016


Here's what I do.  It's a big mess.  What's the right way?

sitename=d8demo

# Return IP address, or nonzero status if none
get_ip() {
    ip=`sudo lxc-ls -f | awk "/$sitename/"' {print $3}'`
    case $ip in
    [0-9]*) echo $ip; return 0;;
    *) return 1;;
    esac
}

do_create() {
    # We want to do ssh-copy-id later, so make sure there's an id
    if ! test -f ~/.ssh/id_rsa.pub && ! test -f ~/.ssh/id_dsa.pub
    then
       ssh-keygen
    fi

    echo "Creating LXC container named $sitename, and creating user
$LOGNAME inside it"
    sudo lxc-create -t download -n $sitename -- --dist ubuntu
--release wily --arch amd64
    # Create a user, add him to sudo group, install ssh
    sudo chroot /var/lib/lxc/$sitename/rootfs adduser $LOGNAME
    sudo chroot /var/lib/lxc/$sitename/rootfs sed -i
"/sudo/s/\$/,$LOGNAME/;s/:,/:/" /etc/group
    sudo chroot /var/lib/lxc/$sitename/rootfs apt-get install -y openssh-server
    # Start it and add it to /etc/hosts
    sudo lxc-start -n $sitename
    while ! get_ip
    do
        sleep 1
    done
    siteip=`get_ip`
    # Remove old entry, if any
    sudo sed -i "/$sitename/d" /etc/hosts
    # Add new entry
    echo "$siteip $sitename" | sudo tee -a /etc/hosts
    echo "Arranging for passwordless ssh to container $sitename for
user $LOGNAME"
    ssh-copy-id $sitename
}


More information about the lxc-users mailing list