windows启动ssh-agent
2022/6/14大约 2 分钟约 622 字
windows启动ssh-agent
Windows10自带了一个非常方便的ssh-agent服务,我们只需要在Windows的服务列表里面启用该服务就能在CMD或PowerShell里直接使用ssh-add添加密钥,实现ssh免密登录。
以管理员打开PowerShell,通过Set-Service -StartupType Automatic ssh-agent将ssh-agent服务的启动类型设为自启,随后键入Start-Service ssh-agent启动当前会话下的ssh-agent服务。
PS C:\windows\system32> Set-Service -StartupType Automatic ssh-agent
PS C:\windows\system32> Start-Service ssh-agent
PS C:\windows\system32> Get-Service ssh-agent
Status Name DisplayName
------ ---- -----------
Running ssh-agent OpenSSH Authentication Agent
PS C:\windows\system32> (Get-Service ssh-agent).startType
Automatic将私钥添加到ssh-agent
ssh-agent是OpenSSH身份认证代理,存储用于公钥身份验证的私钥。每当从客户端进行身份验证需要使用私钥时,ssh-agent都会自动检索代理存储的本地私钥,并将其传递到你的 SSH客户端。
ssh-add将私钥加载进ssh-agent。
使用管理员身份打开终端
注意区分不同的SSH,不同SSH的ssh-agent是相互独立的,这意味着如果你在Windows SSH的ssh-agent中添加了你登录GitHub的私钥,在Git Bash中登录GitHub时仍然可能无法免密登录.
但不同SSH共用客户端配置文件:
Windows上是~/.ssh/configLinux上是/etc/ssh/ssh_config
# 查看 ssh-agent 服务状态,这里输出:Stopped
Get-Service ssh-agent
# 查看 ssh-agent 服务的启动类型,这里输出:Disabled
Get-Service ssh-agent | Select StartType
# 将 ssh-agent 服务的启动类型修改为Automatic(自动启动)。
Get-Service -Name ssh-agent | Set-Service -StartupType Automatic
# 将私钥加载进 ssh-agent
ssh-add C:\Users\xxx\.ssh\your_rsa
# 显示 ssh-agent 中的公钥,验证私钥是否添加成功
ssh-add -L
# 服务重启后,添加的密钥仍在 agent 中ssh代理设置
切换到ssh目录:~/.ssh/config,存在config文件的直接编辑,不存在的新建
复制以下内容到config文件,修改对应配置
Host github.com
User git
Port 22
Hostname github.com
# 注意修改路径为你的路径
IdentityFile "C:\Users\xxx\.ssh\id_rsa"
TCPKeepAlive yes
# host:port修改为你的代理地址
ProxyCommand connect -S host:port -a none %h %p
Host ssh.github.com
User git
Port 443
Hostname ssh.github.com
# 注意修改路径为你的路径
IdentityFile "C:\Users\xxx\.ssh\id_rsa"
TCPKeepAlive yes
# host:port修改为你的代理地址
ProxyCommand connect -S host:port -a none %h %p