Skip to content

Instantly share code, notes, and snippets.



Last active Dec 14, 2015
What would you like to do?
Autostart unprivileged lxc containers
# Workaround to automatically start unprivileged containers on boot. This is
# required because unprivileged containers only seem to work when started from
# an interactive login session. Reference:
# This script can be called from the user crontab with the @reboot
# directive(runs once at startup). One way to achieve this is to save this
# script to /home/USER/lxc-autostart.tcl and add the following entry to
# `crontab -e`:
# @reboot /home/USER/lxc-autostart.tcl
# Requires "expect" installed(sudo apt-get install expect)
# Only timeout after 5 minutes, which should be enough to start all containers.
set timeout 300
# Start an interactive ssh session connected to localhost. Assumes no password
# or confirmation prompts will be displayed, which can be achieved by
# previously connecting with an unlocked key.
set ssh_session {ssh localhost}
spawn {*}$ssh_session
send {
# Send the lxct-autostart command, which starts all unprivileged containers
# configured with = 1
# Exit the ssh session
expect eof

This comment has been minimized.

Copy link

@ak2766 ak2766 commented Dec 14, 2015

I saw your post on and followed your link. However, your solution did not work for me as I have a passphrase protected private key. Even with:

set ssh_session {ssh -i /dev/null localhost}

I was still prompted for a passphrase! I had to create a private key pair without a passphrase to make it work:

ssh-keygen -f /home/me/.ssh/localuseonlyprivkey
cat /home/me/.ssh/ >> /home/me/.ssh/authorized_keys

Then with the expect script, I had this:

set ssh_session {ssh -i /home/me/.ssh/localuseonlyprivkey localhost}

Thanks for your contribution.


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