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 [email protected]

选择 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/[email protected]/cmd/gopls/integration/vscode

/usr/local/go-path/pkg/mod/golang.org/x/[email protected]/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 [email protected]:~/.ssh

.ssh 目录不存时需创建

3、Linux

cd ~/.ssh

cat id_rsa.pub > authorized_keys

4、Windows 验证免密

ssh [email protected]

无需再输入密码

五、配置

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"] // 依据实际参数配置

}

]

}

相关推荐
哈希茶馆13 分钟前
前端工程化利器:Node.js 文件匹配库 fast-glob 完全指南——比传统方案快 350% 的「文件搜索神器」
运维·前端·javascript·npm·node.js·全文检索·运维开发
Brandon汐19 分钟前
Linux中的系统延时任务和定时任务与时间同步服务和构建时间同步服务器
linux·运维
又逢乱世20 分钟前
Ubuntu 安装 MySQL8
linux·运维·mysql·ubuntu
共享家952727 分钟前
冯·诺依曼体系:现代计算机的底层逻辑与百年传承
linux
庐阳寒月29 分钟前
linux多线(进)程编程——(10)信号
linux·c++·嵌入式
孞㐑¥31 分钟前
Linux之基础开发工具(yum,vim,gcc,g++)
linux·c++·经验分享·笔记
Yang三少喜欢撸铁32 分钟前
通过Docker部署Prometheus + Grafana搭建监控平台【超详细版】
linux·服务器·docker·容器·grafana·prometheus
一道秘制的小菜1 小时前
Linux第20节 --- inode和文件系统
linux·运维·服务器·c++·文件
BIN-XYB1 小时前
Ubuntu搭建Conda+Python开发环境
linux·运维·ubuntu
哲讯智能科技1 小时前
无锡哲讯科技:引领企业数字化转型的SAP实施专家
大数据·运维·人工智能