基本步骤
1. 在本地生成公钥-私钥对
ssh-keygen -t rsa
2. 在服务器上添加本地公钥:
echo "xxxx" >> ~/.ssh/authorized_keys
3. 更新vscode-ssh的config文件,如下所示:
Host xxxx.com
HostName xxxx.com
Port 17506
User root
PreferredAuthentications publickey
IdentityFile "C:\Users\xxxx\.ssh\id_rsa"
下一次登录即可免密
遇到的报错
windows有时候会出现下面的报错:
[09:25:56.274] Running script with connection command: "C:\Windows\System32\OpenSSH\ssh.exe" -T -D 63864 "xxxx" bash
[09:25:56.276] Terminal shell path: C:\Windows\System32\cmd.exe
[09:25:57.510] > Load key "C:\\Users\\xxxx\\.ssh\\id_rsa": Permission denied
> root@xxxx: Permission denied (publickey,password).
这是因为本地的.ssh权限设置有问题,按照下面修改即可:
(1)检查并修复私钥文件权限:
(%USERNAME%使用whoami查看替换)
# 在 PowerShell 中运行(以管理员身份)
icacls "C:\Users\xxxx\.ssh\id_rsa" /inheritance:r
icacls "C:\Users\xxxx\.ssh\id_rsa" /grant:r "%USERNAME%:R"
(2)正确设置 .ssh 目录权限:
icacls "C:\Users\xxxx\.ssh" /inheritance:r
icacls "C:\Users\xxxx\.ssh" /grant:r "%USERNAME%:F"
参考博客: