最近使用 zed 连接 VPS 修改一堆屎山,发现 AI 改一半代码的时候经常就不动了,原来是 SSH 连接会断连。
于是改用 SSH Key 登录,记录一下操作记录,防止踩坑。

一、在 Windows 生成 SSH Key
1.打开终端,输入:
ssh-keygen -t ed25519
ed25519 是最安全、最快的 SSH 算法
2.生成过程
终端会提示:
Enter file in which to save the key
直接按 Enter
默认位置:C:\Users\你的用户名\.ssh\id_ed25519
继续:
Enter passphrase
直接按两次 Enter(留空)
这样才能 完全免密码登录。
3.生成成功
会得到两个文件:
C:\Users\你的用户名\.ssh\id_ed25519
C:\Users\你的用户名\.ssh\id_ed25519.pub
含义:
| 文件 | 作用 |
|---|---|
| id_ed25519 | 私钥(自己保存) |
| id_ed25519.pub | 公钥(上传服务器 |
二、把公钥上传到服务器
1.连接你的VPS,创建 SSH 目录
mkdir -p ~/.ssh
chmod 700 ~/.ssh
2.打开 authorized_keys
nano ~/.ssh/authorized_keys
3.回到 Windows 复制公钥,在本地打开 powershell,输入
type $env:USERPROFILE\.ssh\id_ed25519.pub
会获得一行类似“ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIxxxxx user@PC”,直接复制整行
4.粘贴到刚刚第 2 步打开的 authorized_keys
当然如果你装了 1panel 的话,可以直接在「SSH 管理」点「授权密钥」,然后粘贴保存
5.设置权限
服务器执行:
chmod 600 ~/.ssh/authorized_keys
三、测试 SSH Key 登录
在 Windows 终端执行:
ssh root@ip
四、配置 SSH KeepAlive(防止断线)
Windows 打开 C:\Users\你的用户名\.ssh\config,如果没有就新建。
写入:
Host myserver
HostName 你的ip
User root
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 60
ServerAliveCountMax 10
TCPKeepAlive yes
解释:
| 参数 | 作用 |
|---|---|
| IdentityFile | 指定 SSH key |
| ServerAliveInterval | 每60秒发送心跳 |
| ServerAliveCountMax | 10次无响应才断开 |
五、服务器端 KeepAlive 配置
ssh 登录服务器输入:
nano /etc/ssh/sshd_config
新增:
ClientAliveInterval 60
ClientAliveCountMax 10
然后重启 SSH,输入:
systemctl restart sshd
六、在 Zed 中配置 SSH Key
打开 Zed settings.json,填写:
{
"ssh_connections": [
{
"host": "你的IP",
"username": "root",
"port": 22,
"args": [
"-i",
"~/.ssh/id_ed25519"
],
"projects": [
{
"paths": [
"/你想要连接的服务器目录"
]
}
]
}
]
}
极客分享原创文章,转载请注明出处:https://geekshare.org/429/