vscode远程连接失败
-
首先确保远程服务器开启了 22 端口
-
在SSH 主机(非本地)上打开
/etc/ssh/sshd_config
或C:\ProgramData\ssh\sshd_config
如果远程是linux就打开
/etc/ssh/sshd_config
是windows就打开:C:\ProgramData\ssh\sshd_config
-
在文件中添加设置
AllowTcpForwarding yes
。 -
重新启动 SSH 服务器。(在 Ubuntu 上,运行
sudo systemctl restart sshd
。在 Windows 上,在管理员 PowerShell 中运行,Restart-Service sshd
)。 -
重试。
快速入门:使用 SSH 密钥
为远程主机设置基于 SSH 密钥的身份验证。首先,我们将创建一个密钥对,然后将公钥复制到主机
检查您的本地计算机上是否已有 SSH 密钥。
- linux/macos :
~/.ssh/id_rsa.pub
Windows 上用户配置文件文件夹中的目录(例如C:\Users\your-user.ssh\id_rsa.pub)。
如果您没有密钥,请在本地终端/PowerShell 中运行以下命令以生成 SSH 密钥对:
ssh-keygen -t rsa -b 4096
运行以下命令之一,在本地终端窗口中根据需要替换用户名和主机名,以将本地公钥复制到 SSH 主机。
- linux/ macos
export USER_AT_HOST="your-user-name-on-host@hostname"
export PUBKEYPATH="$HOME/.ssh/id_rsa.pub"
ssh-copy-id -i "$PUBKEYPATH" "$USER_AT_HOST"
- windwos
export USER_AT_HOST="your-user-name-on-host@hostname"
export PUBKEYPATH="$HOME/.ssh/id_rsa.pub"
ssh $USER_AT_HOST "powershell New-Item -Force -ItemType Directory -Path \"\$HOME\\.ssh\"; Add-Content -Force -Path \"\$HOME\\.ssh\\authorized_keys\" -Value '$(tr -d '\n\r' < "$PUBKEYPATH")'"
免密登录
将自己的公钥追加到到服务器的~/.ssh/authorized_keys
文件后面即可:
cat xx.pub >> ~/.ssh/authorized_keys