Skip to content

Instantly share code, notes, and snippets.

@yuriescl
Created February 5, 2021 13:08
Show Gist options
  • Select an option

  • Save yuriescl/acfaf9eecfa1f2870dbbc41106443833 to your computer and use it in GitHub Desktop.

Select an option

Save yuriescl/acfaf9eecfa1f2870dbbc41106443833 to your computer and use it in GitHub Desktop.
GitHub SSH agent forward
# Scenario 1:
# Your repo is at https://github.com/myuser/myrepo
# You have your SSH key added to GitHub.
# You want to use git commands in a server but don't wanna copy your private key to the server.
# first we test our SSH pubkey auth to GitHub, it should print something like:
# Hi user! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T git@github.com
# then, we authenticate and forward the agent
ssh -A user@server
# then, we can use git commands as it will use the same SSH agent session (which is stored in $SSH_AUTH_SOCK)
git clone git@github.com:myuser/myrepo
# Scenario 2:
# Same as Scenario 1, except you wanna run the git commands as another user called otheruser
# You'd want this to avoid filesystem permission issues when cloning, pulling, etc.
USER=otheruser
setfacl -m $USER:x $(dirname "$SSH_AUTH_SOCK") && setfacl -m $USER:rwx "$SSH_AUTH_SOCK" && sudo --preserve-env=SSH_AUTH_SOCK -u $USER bash -c "cd \$HOME; bash"
git clone git@github.com:myuser/myrepo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment