1. 远程调试结构
本地机器与远程机器通过SSH连接。
1.1 本地机器
Windows 10
1.2 远程机器
在Windows 10上安装部署的Ubuntu虚拟机。
2. 远程机器环境配置
2.1 SSH
设置SSH。
2.1.1 检查SSH状态
$ ps -e | grep ssh
root@test:~# ps -e | grep ssh
2511 ? 00:00:00 sshd
2628 ? 00:00:00 sshd
3466 ? 00:00:00 sshd
3467 ? 00:00:00 sshd
3553 ? 00:00:00 sshd
3555 ? 00:00:00 sshd
2.1.2 安装SSH服务
2.1.3 启动SSH服务
$ sudo /etc/init.d/ssh start
或
$ sudo systemctl start ssh
2.1.4 修改SSH配置文件
编辑配置文件
$ sudo vi /etc/ssh/sshd_config
修改配置文件:
修改
PermitRootLogin withou-password
...
PasswordAuthentication no
...
为:
PermitRootLogin yes
...
PasswordAuthentication yes
...
2.1.5 重启SSH服务
$ sudo systemctl restart ssh
2.2 安装Go
2.2.1 下载go
2.2.2 解压至/usr/local下
$ tar -C /usr/local -xzf go1.16.linux-amd64.tar.gz
$ export PATH=$PATH:/usr/local/go/bin
2.3 安装Delve
2.3.1 下载Delve
$ cd /home/ubuntu
$ mkdir software
$ cd /home/ubuntu/software/
$ git clone https://github.com/go-delve/delve
2.3.2 安装Delve
$ cd /home/ubuntu/software/delve/cmd/dlv
$ go build
$ cp dlv /usr/local/bin
$ dlv version
Delve Debugger
Version: 1.6.0
Build: $Id: 8cc9751909843dd55a46e8ea2a561544f70db34d $
3. 本地机器环境配置
3.1 安装GoLand
GoLand,安装在Windows上,作为客户端。
3.2 安装Multipass
安装Multipass虚机,部署Ubuntu,作为远程机器。
下载multipass-1.6.2+win-win64.exe,并安装。
C:\Users\Administrator>multipass find
Image Aliases Version Description
core core16 20200818 Ubuntu Core 16
core18 20200812 Ubuntu Core 18
snapcraft:core18 20201111 Snapcraft builder for Core 18
snapcraft:core20 20201111 Snapcraft builder for Core 20
16.04 xenial 20210224 Ubuntu 16.04 LTS
18.04 bionic 20210224 Ubuntu 18.04 LTS
20.04 focal,lts 20210223 Ubuntu 20.04 LTS
20.10 groovy 20210303 Ubuntu 20.10
appliance:adguard-home 20200812 Ubuntu AdGuard Home Appliance
appliance:mosquitto 20200812 Ubuntu Mosquitto Appliance
appliance:nextcloud 20200812 Ubuntu Nextcloud Appliance
appliance:openhab 20200812 Ubuntu openHAB Home Appliance
appliance:plexmediaserver 20200812 Ubuntu Plex Media Server Appliance
安装Ubuntu 20.04
$ multipass launch 20.04 --name go-gdal
3.3 查看远程机器IP地址
multipass info -all
C:\Users\Administrator>multipass info --all
Name: test
State: Running
IPv4: 172.18.146.95
Release: Ubuntu 20.04.2 LTS
Image hash: bba6582030e5 (Ubuntu 20.04 LTS)
Load: 0.08 0.02 0.01
Disk usage: 1.7G out of 4.7G
Memory usage: 463.0M out of 916.9M
3.4 测试远程机器IP地址(ping)
$ ping 172.18.146.95
C:\Users\Administrator>ping 172.18.146.95
正在 Ping 172.18.146.95 具有 32 字节的数据:
来自 172.18.146.95 的回复: 字节=32 时间<1ms TTL=64
来自 172.18.146.95 的回复: 字节=32 时间=1ms TTL=64
来自 172.18.146.95 的回复: 字节=32 时间<1ms TTL=64
来自 172.18.146.95 的回复: 字节=32 时间<1ms TTL=64
172.18.146.95 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 1ms,平均 = 0ms
3.5. GoLand配置
3.5.1 FTP/SFTP Connectivity
要远程调试,需要本地与远程之间的连接与同步。在GoLand中需要安装FTP/SFTP Connectivity插件。
3.5.2 Deployment
Tool --> Deployment --> Configurations…
配置远程部署,使用SFTP类型。
3.5.3 SSH配置
Host为远程Ubuntu的地址(IP地址,或Multipass的名称),由于每次Multipass启动Ubuntu后,IP地址都会改变,所以最好使用名称。
例如,根据前面Multipass安装Ubuntu时,通过–name参数,给定的名称为go-gdal,则此处的Host可设置为:go-gdal.mshome.net
。
3.5.4 目录映射(Mappings)
建立远程路径与本地路径之间的映射关系。
4. 远程Ubuntu运行Delve
$ cd /root/go/src/img/src
root@go-gdal:~/go/src/img/src# ls
go.mod goroutine1 goroutine1.go m
root@go-gdal:~/go/src/img/src# dlv debug --headless --listen=:2345 --api-version=2 --accept-multiclient goroutine1.go
API server listening at: [::]:2345
5. 本地Windows运行GoLand
5.1 配置远程调试(Remote Debugging)
5.2 运行本地远程调试
在本地(Windows 10)上运行GoLand的远程调试。
运行本地远程调试后,会在断点处挂起。
参考
[1] 解决ubuntu下SSH无法连接的问题
[2] goland远程调试
[3] goland 调试docker容器中的go代码
[4] goland连接linux主机进行dlv远程调试
[5] Overview of Remote Debugging
[6] 如何在Ubuntu 20.04上安装Go
[7] ubuntu安装go语言调试器dlv
[8] win10下goland&dlv&docker打造远程断点调试环境
[9] 在GoLand中使用Docker插件生成镜像与创建容器
[10] docker安装ubuntu镜像