|
| 1 | +--- |
| 2 | +title:Using SSH Agent Forwarding | GitHub API |
| 3 | +--- |
| 4 | + |
| 5 | +#Using SSH agent forwarding |
| 6 | + |
| 7 | +* TOC |
| 8 | +{:toc} |
| 9 | + |
| 10 | +SSH agent forwarding can be used to make deploying to a server simple. It allows you to use your local SSH keys instead of leaving keys (without passphrases!) sitting on your server. |
| 11 | + |
| 12 | +If you've already set up an SSH key to interact with GitHub, you're probably familiar with`ssh-agent`. It's a program that runs in the background and keeps your key loaded into memory, so that you don't need to enter your passphrase every time you need to use the key. The nifty thing is, you can choose to let servers access your local`ssh-agent` as if they were already running on the server. This is sort of like asking a friend to enter their password so that you can use their computer. |
| 13 | + |
| 14 | +Check out[Steve Friedl's Tech Tips guide][tech-tips] for a more detailed explanation of SSH agent forwarding. |
| 15 | + |
| 16 | +##Setting up SSH agent forwarding |
| 17 | + |
| 18 | +Ensure that your own SSH key is set up and working. You can use[our guide on generating SSH keys][generating-keys] if you've not done this yet. |
| 19 | + |
| 20 | +You can test that your local key works by entering`ssh -T git@github.com` in the terminal: |
| 21 | + |
| 22 | +<preclass="terminal"> |
| 23 | +$ ssh -T git@github.com |
| 24 | +<spanclass="comment"># Attempt to SSH in to github</span> |
| 25 | +<spanclass="output">Hi <em>username</em>! You've successfully authenticated, but GitHub does not provide</span> |
| 26 | +<spanclass="output">shell access.</span> |
| 27 | +</pre> |
| 28 | + |
| 29 | +We're off to a great start. Let's set up SSH to allow agent forwarding to your server. |
| 30 | + |
| 31 | +1. Using your favorite text editor, open up the file at`~/.ssh/config`. If this file doesn't exist, you can create it by entering`touch ~/.ssh/config` in the terminal. |
| 32 | + |
| 33 | +2. Enter the following text into the file, replacing`example.com` with your server's domain name or IP: |
| 34 | + |
| 35 | + Host example.com |
| 36 | + ForwardAgent yes |
| 37 | + |
| 38 | +<divclass="warning"> |
| 39 | +<p> |
| 40 | +<strong>Warning</strong>: You may be tempted to use a wildcard like <code>Host*</code> to just apply this setting to all SSH connections. That's not really a good idea, as you'd be sharing your local SSH keys with <em>every</em> server you SSH into. They won't have direct access to the keys, but they will be able to use them <em>as you</em> while the connection is established. <strong>You should only add servers you trust and that you intend to use with agent forwarding.</strong> |
| 41 | +</p> |
| 42 | +</div> |
| 43 | + |
| 44 | +##Testing SSH agent forwarding |
| 45 | + |
| 46 | +To test that agent forwarding is working with your server, you can SSH into your server and run`ssh -T git@github.com` once more. If all is well, you'll get back the same prompt as you did locally. |
| 47 | + |
| 48 | +If you're unsure if your local key is being used, you can also inspect the`SSH_AUTH_SOCK` variable on your server: |
| 49 | + |
| 50 | +<preclass="terminal"> |
| 51 | +$ echo "$SSH_AUTH_SOCK" |
| 52 | +<spanclass="comment"># Print out the SSH_AUTH_SOCK variable</span> |
| 53 | +<spanclass="output">/tmp/ssh-4hNGMk8AZX/agent.79453</span> |
| 54 | +</pre> |
| 55 | + |
| 56 | +If the variable is not set, it means that agent forwarding is not working: |
| 57 | + |
| 58 | +<preclass="terminal"> |
| 59 | +$ echo "$SSH_AUTH_SOCK" |
| 60 | +<spanclass="comment"># Print out the SSH_AUTH_SOCK variable</span> |
| 61 | +<spanclass="output"><em>[No output]</em></span> |
| 62 | +$ ssh -T git@github.com |
| 63 | +<spanclass="comment"># Try to SSH to github</span> |
| 64 | +<spanclass="output">Permission denied (publickey).</span> |
| 65 | +</pre> |
| 66 | + |
| 67 | +##Troubleshooting SSH agent forwarding |
| 68 | + |
| 69 | +Here are some things to look out for when troubleshooting SSH agent forwarding. |
| 70 | + |
| 71 | +###Your SSH keys must work locally |
| 72 | + |
| 73 | +Before you can make your keys work through agent forwarding, they must work locally first.[Our guide on generating SSH keys][generating-keys] can help you set up your SSH keys locally. |
| 74 | + |
| 75 | +###Your system must allow SSH agent forwarding |
| 76 | + |
| 77 | +Sometimes, system configurations disallow SSH agent forwarding. You can check if a system configuration file is being used by entering the following command in the terminal: |
| 78 | + |
| 79 | +<preclass="terminal"> |
| 80 | +$ ssh -v <em>example.com</em> |
| 81 | +<spanclass="comment"># Connect to example.com with verbose debug output</span> |
| 82 | +<spanclass="output">OpenSSH_5.6p1, OpenSSL 0.9.8r 8 Feb 2011</span> |
| 83 | +<spanclass="output">debug1: Reading configuration data /Users/<em>you</em>/.ssh/config</span> |
| 84 | +<spanclass="output">debug1: Applying options for example.com</span> |
| 85 | +<spanclass="output">debug1: Reading configuration data /etc/ssh_config</span> |
| 86 | +<spanclass="output">debug1: Applying options for *</span> |
| 87 | +$ exit |
| 88 | +<spanclass="comment"># Returns to your local command prompt</span> |
| 89 | +</pre> |
| 90 | + |
| 91 | +In the example above, the file*~/.ssh/config* is loaded first, then*/etc/ssh_config* is read. We can inspect that file to see if it's overriding our options by running the following commands: |
| 92 | + |
| 93 | +<preclass="terminal"> |
| 94 | +$ cat /etc/ssh_config |
| 95 | +<spanclass="comment"># Print out the /etc/ssh_config file</span> |
| 96 | +<spanclass="output"> Host *</span> |
| 97 | +<spanclass="output"> SendEnv LANG LC_*</span> |
| 98 | +<spanclass="output"> ForwardAgent no</span> |
| 99 | +</pre> |
| 100 | + |
| 101 | +In this example, our*/etc/ssh_config* file specifically says`ForwardAgent no`, which is a way to block agent forwarding. Deleting this line from the file should get agent forwarding working once more. |
| 102 | + |
| 103 | +###Your server must allow SSH agent forwarding on inbound connections |
| 104 | + |
| 105 | +Agent forwarding may also be blocked on your server. You can check that agent forwarding is permitted by SSHing into the server and running`sshd_config`. The output from this command should indicate that`AllowAgentForwarding` is set. |
| 106 | + |
| 107 | +###Your local`ssh-agent` must be running |
| 108 | + |
| 109 | +On most computers, the operating system automatically launches`ssh-agent` for you. On Windows, however, you need to do this manually. We have[a guide on how to start`ssh-agent` whenever you open Git Bash][autolaunch-ssh-agent]. |
| 110 | + |
| 111 | +To verify that`ssh-agent` is running on your computer, type the following command in the terminal: |
| 112 | + |
| 113 | +<preclass="terminal"> |
| 114 | +$ echo "$SSH_AUTH_SOCK" |
| 115 | +<spanclass="comment"># Print out the SSH_AUTH_SOCK variable</span> |
| 116 | +<spanclass="output">/tmp/launch-kNSlgU/Listeners</span> |
| 117 | +</pre> |
| 118 | + |
| 119 | +###Your key must be available to`ssh-agent` |
| 120 | + |
| 121 | +You can check that your key is visible to`ssh-agent` by running the following command: |
| 122 | + |
| 123 | +<preclass="terminal"> |
| 124 | +ssh-add -L |
| 125 | +</pre> |
| 126 | + |
| 127 | +If the command says that no identity is available, you'll need to add your key: |
| 128 | + |
| 129 | +<preclass="terminal"> |
| 130 | +ssh-add <em>yourkey</em> |
| 131 | +</pre> |
| 132 | + |
| 133 | +[tech-tips]:http://www.unixwiz.net/techtips/ssh-agent-forwarding.html |
| 134 | +[generating-keys]:https://help.github.com/articles/generating-ssh-keys |
| 135 | +[ssh-passphrases]:https://help.github.com/ssh-key-passphrases/ |
| 136 | +[autolaunch-ssh-agent]:https://help.github.com/articles/working-with-ssh-key-passphrases#auto-launching-ssh-agent-on-msysgit |