背景
很多办公场景的编译环境在云端服务器,并且云端服务器只有 terminal 没有 GUI,对于使用 IDE 工具很不方便,这种场景除了用 vim 等编辑器在远端 terminal 编辑,还可以使用 VScode + sftp 插件来使用本地 IDE 编辑远端,实际使用VScode 的 remote ssh 等方法进行操作。
本文重点介绍 sftp 的场景。
区别
VScode + sftp 是修改本地代码 然后push到远端
VScode + remote ssh 是直接登录远端服务器,然后直接编辑远端代码(本地有缓存,但是有文件同步协议等避免 sftp 等一些坑)
步骤
- 安装sftp插件
- 配置在代码根目录 添加 .vscode 目录,然后创建 sftp.json 配置文件,添加服务器 Hostname passwd hop(如果需要跳板)以及在本地的目录和远程目录。
- 启动全局命令 (MAC 上是 cmd + shift + p),输入sftp,选择对应配置(此时可以查看 sftp 查看登录日志)
- 初次可以在代码编辑框 点击右键 选择sftp upload 同步本地代码到远程,初次远端服务器需要安装 sftp 的server端
- 其他默认情况会自动同步
典型配置:
{
"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
坑:
- sftp 修改后是 push 到远程,如果本地删除很可能远程文件还在。
- 如果是某个源文件从目录 A 移植到目录 B ,A 的很可能还在,编译的时候很可能还是读取的 A 的代码,造成一直在本地修改 B,云端一直不生效
- 如果在云端使用 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/