Forked from scottjacobsen/git+clone+ssh+agent+forward+sudo
Created
February 6, 2018 22:35
-
-
Save hp9390/9db7c123b5fdd740c4b0bb18a9a9104e to your computer and use it in GitHub Desktop.
Git clone using ssh agent forwarding and sudo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SSH agent forwarding is great. It allows you to ssh from one server to | |
another all the while using the ssh-agent running on your local | |
workstation. The benefit is you don't need to generate ssh key pairs | |
on the servers you are connecting to in order to hop around. | |
When you ssh to a remote machine the remote machine talks to your | |
local ssh-agent through the socket referenced by the SSH_AUTH_SOCK | |
environment variable. | |
So you the remote server you can do something like: | |
> git clone git@github.com:my-github-account/my-repo.git | |
And git will make use of the ssh-agent running on your local | |
workstation to authenticate with github and clone your repo. | |
This fails if you do | |
> sudo git clone git@github.com:my-github-account/my-repo.git | |
because your environment variables are not available to the | |
commands running under sudo. | |
However, you can set the SSH_AUTH_SOCK variable for the command by | |
passing it on the command line like so | |
> sudo SSH_AUTH_SOCK=$SSH_AUTH_SOCK git clone git@github.com:my-github-account/my-repo.git | |
and all is well. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment