【git】ssh代理實現免密登錄

使用ssh密鑰,如果有人可以訪問您的計算機,他們也可以訪問使用該密鑰的每個系統。爲了保證安全,可以向ssh密鑰中添加密碼。通過ssh代理來安全地保存您的密碼,這樣就不必重新輸入它。

1.添加或更改祕鑰短語

可以通過鍵入以下命令來更改現有私鑰的密碼,而不必重新生成密鑰對:

$ ssh-keygen -p

如果您的密鑰已經有一個密碼,則在您可以更改爲新密碼之前,系統會提示您輸入密碼。否則,您可以添加新的密碼到祕鑰中。會出現如下提示內容,請按照[]中的內容操作:

Start the SSH key creation process
Enter file in which the key is (/Users/you/.ssh/id_rsa): [回車]
Key has comment '/Users/you/.ssh/id_rsa'
Enter new passphrase (empty for no passphrase): [輸入您的密碼]
Enter same passphrase again: [再次輸入密碼]
Your identification has been saved with the new passphrase.

2.在git上自動啓動ssh代理

  • Github桌面應用程序用戶:則請忽略本節操作,因爲Github桌面自動爲您啓動SSH代理。
  • Git shell用戶:請按照以下步驟自動運行SSH代理。

首先,將下面的內容複製到粘貼到您的電腦上的~/.profile~/.bashrc文件中,即Administrator目錄下的.profile文件中。若不存在該,則新建。這裏,~代表當前用戶目錄,默認是Administrator

env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
fi

unset env

如果您的私鑰不存儲在默認位置之一~/.ssh/id_rsa~/.ssh/id_dsa)中,則需要告訴您的SSH身份驗證代理在哪裏找到它。要向SSH代理添加密鑰,請鍵入ssh-add ~/path/to/my_key。有關更多信息,請參見“生成一個新的SSH密鑰並將其添加到SSH代理”。

這裏,如果此時我們重新打開一個git bash,可以看到:

本文翻譯自:Working with SSH key passphrases

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章