osx - How to save ssh identity settings in Snow Leopard?

17
2014-04
  • Jaded

    I'm using ssh settings from my work machine on laptop at home for unattended git repository connection. I've copied private and public keys and use ssh-agent w/ ssh-add identity command. Is there any way to save this settings permanently? Every time after reboot i need to execute above commands again that is kinda annoying. Thanks in advance.

  • Answers
  • executor21

    Add the ssh private keys to your keychain.

    ssh-add -K /path/to/private/ssh/key
    

    More here.


  • Related Question

    osx - How can I get ssh-agent working over ssh and in tmux (on OS X)?
  • Rich

    I have a private key set up for my github account, the passphrase to which is, I believe, stored in OS X's keychain. I certainly don't have to type it in when I open a terminal window and enter ssh [email protected].

    However, when I'm running bash over an ssh session, or locally inside a tmux session, I have to type in the passphrase every single time I attempt to ssh to github.

    This question suggests that a similar problem exists with screen, but I don't really understand the issue well enough to fix it in tmux. There's also this page which includes a fairly complicated solution, but for zsh.

    EDIT:

    In response to @Mikel's answer, from a local terminal I get the following output:

    [~]
    $ echo $SSH_AUTH_SOCK
    /tmp/launch-S4HBD6/Listeners
    [~] 
    $ ssh-add -l
    2048 [my key fingerprint] /Users/richie/.ssh/id_rsa (RSA)
    [~]
    $ typeset -p SSH_AUTH_SOCK
    declare -x SSH_AUTH_SOCK="/tmp/launch-S4HBD6/Listeners"
    

    Whereas over ssh or in tmux I get:

    [~]
    $ echo $SSH_AUTH_SOCK
    
    [~]
    $ ssh-add -l
    Could not open a connection to your authentication agent.
    [~]
    $ typeset -p SSH_AUTH_SOCK
    bash: typeset: SSH_AUTH_SOCK: not found
    

    echo $SSH_AGENT_PID returns nothing whatever shell I run it from.


  • Related Answers
  • 8088

    My colleague created some bash functions to assist with finding a live agent: https://github.com/wwalker/ssh-find-agent

    He uses it mainly for connecting between systems (laptop to desktop, etc), but I use it most often for local tmux sessions where you logout/in from your window manager (OS X for myself).

    Usage

    1. Download ssh-find-agent.bash (git clone git://github.com/wwalker/ssh-find-agent.git works).

    2. Add the following to ~/.bashrc:

      . /path/to/ssh-find-agent.bash
      
    3. Then you can type the following to set SSH_AUTH_SOCK in your current shell:

      set_ssh_agent_socket
      
  • Trevor Powell

    In your .tmux.conf configuration file, add this line:

    set -g update-environment "SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION"

    This causes these environment variables to be copied from your main shell to any shells opened within tmux, which then allows ssh-agent to work properly within those tmux shells.

  • mislav

    An elegant solution, picked up from dagit.o:

    Create ~/.ssh/rc

    #!/bin/bash
    if [ -S "$SSH_AUTH_SOCK" ]; then
        ln -sf $SSH_AUTH_SOCK ~/.ssh/ssh_auth_sock
    fi
    

    Add to ~/.tmux.conf

    set -g update-environment "DISPLAY SSH_ASKPASS SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY"
    set-environment -g 'SSH_AUTH_SOCK' ~/.ssh/ssh_auth_sock
    
  • Mikel

    What does:

    echo $SSH_AUTH_SOCK
    echo $SSH_AGENT_PID
    ssh-add -l
    

    print?

    Run it in your normal terminal, then run it inside your tmux session. They should print the same thing.

  • cwjohnston

    Not sure if you are using bash or another shell, but this guy's tmux setup looks like it would work for bash. Personally, I am using zsh with oh-my-zsh, and I found that ssh-agent started working in tmux after I added

    zstyle :omz:plugins:ssh-agent agent-forwarding on
    

    to my .zshrc file and reloaded the config in my running zsh sessions. I also found this guy's zsh-oriented solution, but it turned out to be unnecessary for me.