VScode通过 SSH 编辑远程代码的2种方式之sftp(本地修改 push 远端)

本文介绍了如何在没有GUI的云端服务器上利用VScode的sftp插件进行本地IDE编辑远程代码,以及与remotessh的区别。步骤包括安装插件、配置sftp连接、典型配置和多跳板机设置。同时提到了可能遇到的坑和相关链接。
摘要由CSDN通过智能技术生成

背景

很多办公场景的编译环境在云端服务器,并且云端服务器只有 terminal 没有 GUI,对于使用 IDE 工具很不方便,这种场景除了用 vim 等编辑器在远端 terminal 编辑,还可以使用 VScode + sftp 插件来使用本地 IDE 编辑远端,实际使用VScode 的 remote ssh 等方法进行操作。
本文重点介绍 sftp 的场景。

区别

VScode + sftp 是修改本地代码 然后push到远端
VScode + remote ssh 是直接登录远端服务器,然后直接编辑远端代码(本地有缓存,但是有文件同步协议等避免 sftp 等一些坑)

步骤

  1. 安装sftp插件
  2. 配置在代码根目录 添加 .vscode 目录,然后创建 sftp.json 配置文件,添加服务器 Hostname passwd hop(如果需要跳板)以及在本地的目录和远程目录。
  3. 启动全局命令 (MAC 上是 cmd + shift + p),输入sftp,选择对应配置(此时可以查看 sftp 查看登录日志)
  4. 初次可以在代码编辑框 点击右键 选择sftp upload 同步本地代码到远程,初次远端服务器需要安装 sftp 的server端
  5. 其他默认情况会自动同步

典型配置:

{
          "name": "this project", 
          "host": "1.1.1.1",  
          "protocol": "sftp", 
          "port": 22, 
          "username": "[name]", 
          "password": "[passwd]", 
          "remotePath": "/home/xxx/workspace/testpoj", 
          "uploadOnSave": true, 
          "ignore": [ 
              ".*", 
              "*.o", 
              "*.core", 
              "*.vgcore" 
          ] 
        } 

host:服务端的地址
username、passwd:登录host的用户名和密码
protocol、port:sftp用22,ftp用21
remotePath: 配置远端
其他:
指定profiles指定多个配置,包括hop
切换配置:Use SFTP: Set Profile to switch profile.

其他

HOP

如果有跳板机才能访问 ssh的服务器,需要在配置中添加 hop 的配置。

{
  "name": "target",
  "remotePath": "/path/in/target",

  // hop
  "host": "hopHost",
  "username": "hopUsername",
  "privateKeyPath": "/Users/localUser/.ssh/id_rsa", // <-- The key file is assumed on the local.

  "hop": {
    // target
    "host": "targetHost",
    "username": "targetUsername",
    "privateKeyPath": "/Users/hopUser/.ssh/id_rsa", // <-- The key file is assumed on the hop.
  }
}

多hop详细配置参考:
https://github.com/Natizyskunk/vscode-sftp?tab=readme-ov-file#connection-hopping

VScode remote ssh

remote ssh 是VScode 提供的
https://code.visualstudio.com/docs/remote/ssh

插件

插件链接:https://marketplace.visualstudio.com/items?itemName=suntobright.vscode-sftp
插件 Github 链接:https://github.com/suntobright/vscode-sftp

坑:

  1. sftp 修改后是 push 到远程,如果本地删除很可能远程文件还在。
  2. 如果是某个源文件从目录 A 移植到目录 B ,A 的很可能还在,编译的时候很可能还是读取的 A 的代码,造成一直在本地修改 B,云端一直不生效
  3. 如果在云端使用 vim 等修改后,本地很可能无法push修改上去,要时刻查看 sftp 本地的日志

参考链接:
☆详细配置参考:https://github.com/Natizyskunk/vscode-sftp
vs的:https://marketplace.visualstudio.com/items?itemName=Natizyskunk.sftp
pdf: https://www.cs.tufts.edu/comp/15/reference/system_setup/vscode_setup.pdf
https://www.cs.tufts.edu/comp/1/docs/techguide.html
ssh fs: https://marketplace.visualstudio.com/items?itemName=Kelvin.vscode-sshfs
https://marketplace.visualstudio.com/items?itemName=suntobright.vscode-sftp
https://toptechtips.github.io/2023-04-19-vscode-sync-local-to-remote/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值