前提条件:

  1. 已经安装了 Git for Windows,这会自动安装 SSH 客户端和服务器组件
  2. 远程 Linux 服务器已安装并配置了SSH服务。
  3. 您有足够的权限在远程服务器上进行操作。

1. 生成 SSH 密钥对

  1. 找到C:\Users\你的用户名\下的.ssh文件夹,右键通过Git Bash打开。
  2. 运行以下命令来生成 SSH 密钥对
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

按提示操作,可以选择保存密钥的位置(默认为C:\Users\你的用户名\下的.ssh\id_rsa),连续点三次回车,不要输入密码

2. 将公钥发送到远程服务器

  1. 在 Git Bash 中运行以下命令将公钥发送到远程服务器
ssh-copy-id -i id_rsa.pub root@192.168.1.100

alientek@192.168.1.100
用户名:alientek, ip:你的服务器ip地址

3. 测试 SSH 连接

  1. 在 Git Bash 中运行以下命令测试 SSH 连接:
ssh root@40.101.59.21

# 加-p是端口号
ssh -i ~/.ssh/id_rsa -p 2222 root@192.168.31.26

4. 使用 VSCode 连接到远程服务器

# 配置文件
Host alientek
  HostName 192.168.1.100
  User alientek
  IdentityFile C:\Users\你的用户名\.ssh/id_rsa

其中 alias 是您定义的主机别名,HostName 是远程服务器的 IP 地址或域名,User 是要登录的用户名,IdentityFile 是您的私钥路径。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐