1. 程式人生 > >Win10 cmd命令列,Powershell,Linux子系統Ubuntu bash自動啟動ssh-agent

Win10 cmd命令列,Powershell,Linux子系統Ubuntu bash自動啟動ssh-agent

        當從win10的cmd命令列,PowerShell,或者Ubuntu子系統訪問SSH伺服器或者SSH Git Server,可以使用證書登入。但是預設情況下,需要每次都輸入證書密碼(passphrase),很不方便。

        使用Visual Studio Code的終端,可以設定為使用以上三種Shell之一【可參考 設定Visual Studio Code的預設SHELL (cmd / PowerShell / Ubuntu bash)】,在命令列下可以方便地訪問SSH Git Server,但是每次push也仍然需要輸入證書密碼。

        本文就是為了解決這個問題, 分別說明在cmd命令列,Powershell,Linux子系統Ubuntu bash如何自動啟動ssh-agent。首先,確認id_rsa檔案已經存放到使用者主目錄的.ssh子資料夾下。

1. 在PowerShell的profile中設定ssh-agent自啟動

Win10 PowerShell預設沒有建立profile檔案,執行 Test-Path $profile 命令可以檢視,如果當前沒有預設profile檔案,會返回False:

PS C:\Users\simonliu> Test-Path $profile
False

然後執行 New-Item -path $profile -type file –force 就可以為當前使用者建立一個新的profile檔案,檔名為:

Microsoft.PowerShell_profile.ps1

PS C:\Users\simonliu> New-Item -path $profile -type file -force


    目錄: E:\Documents\WindowsPowerShell


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018/12/25     22:22              0 Microsoft.PowerShell_profile.ps1


PS C:\Users\simonliu>

預設路徑是 Documents(我的文件)資料夾的WindowsPowerShell子資料夾下,(我已經把“我的文件”遷移到“E:\Documents”資料夾)。

雙擊此檔案,新增一行內容即可:

start-ssh-agent

如果此前沒有執行過ssh-agent,那麼需要把金鑰檔案新增一次 (注意檔案路徑)

C:\Users\simonliu\.ssh>ssh-add id_rsa

首次新增可能需要輸入密碼。 

但是我們現在啟動PowerShell視窗,會提示:

  : 無法載入檔案 E:\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1,因為在此係統上禁止執行指令碼。有關詳細
資訊,請參閱 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字元: 3
+ . 'e:\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

此時我們執行 get-executionpolicy ,會顯示 “Restricted”。Restricted 執行策略不允許任何指令碼執行。

PS C:\Windows\system32> get-executionpolicy
Restricted

 

我們需要執行PowerShell(管理員)輸入 set-executionpolicy remotesigned

set-executionpolicy remotesigned

然後隨後輸入"Y"確認。

PS C:\Windows\system32> set-executionpolicy remotesigned

執行策略更改
執行策略可幫助你防止執行不信任的指令碼。更改執行策略可能會產生安全風險,如 https:/go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 幫助主題所述。是否要更改執行策略?
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暫停(S)  [?] 幫助 (預設值為“N”): y
PS C:\Windows\system32>

然後每次啟動PowerShell視窗就會執行start-ssh-agent,訪問ssh伺服器再也不需要每次手動輸入密碼了。

根據其他文章的資料,把這個檔案重新命名為profile.ps1檔案並放到“%windir%\system32\WindowsPowerShell\v1.0\
”資料夾,即可對所有使用者生效。

2. Ubuntu 子系統 bash 下的ssh-agent自啟動設定

方法1:(在Win10 Professional 1809 Ubuntu 18.04.1 LTS 測試通過):在bash profile檔案(~/.bashrc)裡面新增如下內容:

if [ ! -S ~/.ssh/ssh_auth_sock ]; then
  eval `ssh-agent`
  ln -sf "$SSH_AUTH_SOCK" ~/.ssh/ssh_auth_sock
fi
export SSH_AUTH_SOCK=~/.ssh/ssh_auth_sock
ssh-add -l > /dev/null || ssh-add

方法2: 在bash profile檔案(~/.bashrc)裡面新增如下內容:

start_ssh_agent() {
    # Try to use an existing agent
    save=~/.ssh-agent
    if [[ -e "$save" ]]
    then
        . "$save" > /dev/null
    fi
    # No existing agent, start a new one
    if [[ -z "$SSH_AGENT_PID" || ! -e "/proc/$SSH_AGENT_PID" ]]
    then
        ssh-agent > "$save"
        . "$save" > /dev/null
        ssh-add
    fi
}
start_ssh_agent

方法3:(不推薦):網上的很多資料,都只說在bash profile檔案(~/.bashrc)裡面新增兩行:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

其中 eval "$(ssh-agent -s)" 代表啟動ssh-agent並放入後臺。

但是這樣的設定,也只對當前的bash session有效,新開一個bash就需要重新輸入密碼了。

Agent pid 18715
Enter passphrase for /home/simonliu/.ssh/id_rsa:

 方法4:(多次測試不能成功驗證):通過keychain對金鑰進行管理,我這裡測試了很多次,都不能成功。 

3. cmd 命令列的profile

Windows 命令提示符的預設 Profile 檔案位置可以通過在如下注冊表鍵來新增自定義的 Bat 批處理檔案的位置即可完成對命令提示符的自定義:

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

應該在bat檔案里加上 ssh-agent和ssh-add C:\Users\simonliu\.ssh\id_rsa即可,我沒有做進一步測試。本人主要是為了使用git,所以不打算用cmd命令行了。

 

參考文獻:

1. How can I run ssh-add automatically, without password prompt?

2. PowerShell for Beginners (Part 6): PowerShell Profiles and the ISE

3. PowerShell因為在此係統中禁止執行指令碼解決方法

4. Keychain

5. Straight forward way to run ssh-agent and ssh-add on login via SSH?

6. 定製自己的命令列環境

7. Generating a new SSH key and adding it to the ssh-agent

8. Running SSH Agent when starting Git Bash on Windows

9. Persistent ssh-agent on Bash on Ubuntu on Windows

10. windows-bash-ssh-agent

11. Windows subsystem for linux - share ssh-agent?

12. How can I run ssh-add automatically, without password prompt?

13. Powershell Profiles配置檔案的存放位置介紹