Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forward local keypair in a SSH session?

Tags:

ssh

ssh-keys

I manually deploy websites through SSH, I manage source code in github/bitbucket. For every new site I'm currently generating a new keypair on the server and adding it to github/bitbucket, so that I can pull chances from server.

I came across a feature in capistrano to use local machine's key pair for pulling updates to server, which is ssh_options[:forward_agent] = true

How can I do something like this and forward my local machine's keypair to the server I'm SSH-ing into, so that I can avoid adding keys into github/bitbucket for every new site.

like image 533
Sathish Manohar Avatar asked Sep 04 '12 06:09

Sathish Manohar


People also ask

How do I forward an agent using SSH?

From the configuration, go to Connection > SSH > Auth and enable “Allow agent forwarding.” You can also add your private key file from the same pane. PuTTY will handle the SSH agent for you, so you don't have to mess around with any config files.

How does SSH key forwarding work?

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. If you've already set up an SSH key to interact with GitHub, you're probably familiar with ssh-agent .


3 Answers

This turned out to be very simple, complete guide is here Using SSH Forwarding

In essence, you need to create a ~/.ssh/config file, if it doesn't exist.

Then, add the hosts (either domain name or IP address in the file and set ForwardAgent yes)

Sample Code:

Host example.com
    ForwardAgent yes

Makes SSH life a lot easier.

like image 106
Sathish Manohar Avatar answered Oct 19 '22 14:10

Sathish Manohar


  1. Create ~/.ssh/config
  2. Fill it with (host address is the address of the host you want to allow creds to be forwarded to):

    Host [host address]
         ForwardAgent yes
    
  3. If you haven't already run ssh-agent, run it:

    ssh-agent
    
  4. Take the output from that command and paste it into the terminal. This will set the environment variables that need to be set for agent forwarding to work. Optionally, you can replace this and step 3 with:

    eval "$(ssh-agent)"
    
  5. Add the key you want forwarded to the ssh agent:

    ssh-add [path to key if there is one]/[key_name].pem
    
  6. Log into the remote host:

    ssh -A [user]@[hostname]
    
  7. From here, if you log into another host that accepts that key, it will just work:

    ssh [user]@[hostname]
    
like image 50
sdconrox Avatar answered Oct 19 '22 16:10

sdconrox


To use it simply with the default identity (id_rsa) you can use the following couple of command:

ssh-add
ssh -A [username]@[server-address]
like image 4
Nek Avatar answered Oct 19 '22 15:10

Nek