当从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子文件夹下。
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\
”文件夹,即可对所有用户生效。
方法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对密钥进行管理,我这里测试了很多次,都不能成功。
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配置文件的存放位置介绍