首页 > 技术文章 > 搞定:Enter passphrase for key提示

jevan 2013-06-27 16:43 原文

使用ssh-genkey生成公用key,但是自己使用时会多次提示,Enter passphrase for key,这儿给出如何解决。

在${HOME}/.bashrc中增加如下代码:

  1. alias auto_passphrase=auto_passphrase
  2.    
  3.    
  4. SSH_ENV=~/.ssh/environment
  5. # start the ssh-agent
  6. function start_agent {
  7.    echo "Initializing new SSH agent…"
  8.    # spawn ssh-agent
  9.    ssh-agent | sed 's/^echo/#echo/' > "$SSH_ENV"
  10.    echo succeeded
  11.    chmod 600 "$SSH_ENV"
  12.    . "$SSH_ENV" > /dev/null
  13.    ssh-add
  14. }
  15.    
  16. # test for identities
  17. function test_identities {
  18.    # test whether standard identities have been added to the agent already
  19.    ssh-add -l | grep "The agent has no identities" > /dev/null
  20.    if [ $? -eq 0 ]; then
  21.    ssh-add
  22.       # $SSH_AUTH_SOCK broken so we start a new proper agent
  23.       if [ $? -eq 2 ];then
  24.       start_agent
  25.       fi
  26.    fi
  27. }
  28.    
  29. #auto_ssh
  30. function auto_passphrase {
  31.    # check for running ssh-agent with proper $SSH_AGENT_PID
  32.    if [ -n "$SSH_AGENT_PID" ]; then
  33.       ps -ef | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
  34.       if [ $? -eq 0 ]; then
  35.       test_identities
  36.       fi
  37.    # if $SSH_AGENT_PID is not properly set, we might be able to load one from
  38.    # $SSH_ENV
  39.    else
  40.       if [ -f "$SSH_ENV" ]; then
  41.       . "$SSH_ENV" > /dev/null
  42.       fi
  43.       ps -ef | grep "$SSH_AGENT_PID" | grep -v grep | grep ssh-agent > /dev/null
  44.       if [ $? -eq 0 ]; then
  45.          test_identities
  46.       else
  47.          start_agent
  48.       fi
  49.    fi
  50. }

 

 

原始文章:http://wooley.me/archives/589

推荐阅读