Skip to content

Instantly share code, notes, and snippets.

@mgwilliams
Last active September 30, 2020 13:50
Show Gist options
  • Save mgwilliams/4d929e10024912670152 to your computer and use it in GitHub Desktop.
Save mgwilliams/4d929e10024912670152 to your computer and use it in GitHub Desktop.
example of how to forward ssh-agent into an lxc
#!/bin/bash
# Example Script to forward ssh-agent socket into an unprivileged lxc
# This allows access to the ssh-agent by commands executed with lxc-attach
set -m
CONTAINER=$1 # e.g., test-container
REMOTE=$2 # e.g, user@example.com
bash -c "
trap '{ kill 0; exit; kill 0; }' SIGINT;
lxc-usernsexec rm .local/share/lxc/$CONTAINER/rootfs/tmp/ssh-agent-sock;
while true;
do socat UNIX:$SSH_AUTH_SOCK EXEC:'lxc-usernsexec socat STDIN UNIX-LISTEN\:.local/share/lxc/$CONTAINER/rootfs/tmp/ssh-agent-sock'
done
" &
pid=$!
# do your stuff here
lxc-attach -v SSH_AUTH_SOCK=/tmp/ssh-agent-sock -n $CONTAINER -- ssh $REMOTE
# clean up when done
kill -sigint -$pid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment