VS Code + Linux 远程开发 go

一、Linux (192.168.12.13)

安装、配置 go 环境

开启ssh服务

二、Windows(192.168.12.12)

不需要 go 环境!!!

1、安装 ssh客户端(ssh.exe)

git 安装包中有 ssh.exe,可通过安装 git 获得 ssh 客户端

添加 ssh.exe目录至 path环境变量

2、安装 VS Code

(1)安装插件 "Remote - SSH"

注意,此时插件安装在 Windows 本机

(2)侧边栏,出现新图标 " 远程资源管理器"(Remote Explorer)

点击 SSH 右侧的 New Remote

输入 ssh root@192.168.12.13

选择 C:\Users\XXX\.ssh\config

文件内容:

Host 192.168.12.13

HostName 192.168.12.13

User root

Port 登录远程主机的端口号(SSH 默认为 22)

IdentityFile 本地的私钥文件 id_rsa 路径

若需要连接多个远程服务器,上述内容复制多份修改即可。

在 Remote Explorer 窗口中点击 Refresh,SSH下显示远程机器列表

点击列表项后的 Connect in Current Window...

选择 Linux

continue

输入远程机器密码

第一次连接,OUTPUT 窗口显示在远程 Linux 机器安装和启动 vscode-server 的过程

连接成功后,TERMINAL 窗口显示远程 Linux 机器的终端命令行界面,可执行各种命令

像操作本地环境一样,在 VS Code 中操作远程 Linux 主机上的文件

(3)打开 VS Code TERMINAL 窗口,在 Linux上新建测试目录 test,编写 go 代码文件

go mod init

vscode自动提示安装所需的插件

注意,此时插件安装在远程 Linux 主机。

( VS Code 扩展页会显示 本机 和 远程主机安装的插件)

三、Linux 会被自动安装 vscode-server 相关文件

bash 复制代码
[root@localhost go-test]# find / -name "*vscode*"

/run/user/0/vscode-ipc-dd8d3090-7e55-4b88-bbac-2e010e8d2c1d.sock

/run/user/0/vscode-git-02c6fc5579.sock

/run/user/0/vscode-ipc-74213b7d-cbee-4614-952c-d65a5f14e7e0.sock

/run/user/0/vscode-git-2d394bc00e.sock

/run/user/0/vscode-ipc-f70b9abb-acaf-4a04-beff-ee0e96e07ba6.sock

/root/.vscode-server

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/vscode-remote-lock.root.e4503b30fc78200f846c62cf8091b76ff5547662

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/extensions/ms-vscode.js-debug-companion

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/extensions/ms-vscode.js-debug

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/extensions/configuration-editing/schemas/devContainer.vscode.schema.json

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/extensions/debug-auto-launch/.vscode

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/extensions/ms-vscode.vscode-js-profile-table

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/node_modules/vscode-textmate

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/node_modules/vscode-regexpp

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/node_modules/vscode-regexp-languagedetection

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/node_modules/@vscode

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/node_modules/@vscode/vscode-languagedetection

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/node_modules/vscode-encrypt

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/node_modules/vscode-encrypt/build/Release/vscode-encrypt-native.node

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/node_modules/vscode-proxy-agent

/root/.vscode-server/bin/e4503b30fc78200f846c62cf8091b76ff5547662/node_modules/vscode-oniguruma

/root/.vscode-server/extensions/golang.go-0.35.2/media/vscode.css

/root/.vscode-server/data/User/globalStorage/vscode.json-language-features

/root/.vscode-server/data/User/workspaceStorage/cb9f3b3db6a18c2b166ede9a924b38e5/vscode.lock

/tmp/vscode-typescript0

/tmp/vscode-goWiRjgh

/usr/local/go-path/pkg/mod/golang.org/x/tools@v0.0.0-20191109212701-97ad0ed33101/cmd/gopls/integration/vscode

/usr/local/go-path/pkg/mod/golang.org/x/tools@v0.0.0-20191109212701-97ad0ed33101/cmd/gopls/integration/vscode/.vscode

[root@localhost go-test]#

四、VS Code 免密连接远程 Linux

1、Windows 生成公私钥

C:\Program Files\Git\usr\bin\ssh-keygen.exe -t rsa

C:\Users\XXX\.ssh 目录下生成两个文件:id_rsa,id_rsa.pub

2、将公钥复制到远程 Linux

scp C:\Users\XXX\.ssh\id_rsa.pub root@192.168.12.13:~/.ssh

.ssh 目录不存时需创建

3、Linux

cd ~/.ssh

cat id_rsa.pub > authorized_keys

4、Windows 验证免密

ssh root@192.168.12.13

无需再输入密码

五、配置

1、.vscode/lauch.json

{

// Use IntelliSense to learn about possible attributes.

// Hover to view descriptions of existing attributes.

// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387

"version": "0.2.0",

"configurations": [

{

"name": "Launch",

"type": "go",

"request": "launch",

"mode": "auto",

"program": "${workspaceFolder}",

"cwd": "${workspaceFolder}",

"env": {},

"args": ["-x"] // 依据实际参数配置

}

]

}

相关推荐
chlk1237 小时前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑7 小时前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件8 小时前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
碳基沙盒8 小时前
OpenClaw 多 Agent 配置实战指南
运维
深紫色的三北六号17 小时前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash21 小时前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI1 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行2 天前
Linux和window共享文件夹
linux
木心月转码ing2 天前
WSL+Cpp开发环境配置
linux
蝎子莱莱爱打怪3 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes