从MacOS goland无法debug到dns无法解析localhost

背景

MacOS 13.0.1

GoLand2022.2

GoLand开发golang项目,可以run,但是无法debug...

复制代码
​
/Applications/GoLand.app/Contents/plugins/go/lib/dlv/mac/dlv --listen=10.xx.xxx.129:63466 --headless=true --api-version=2 --check-go-version=false --only-same-user=false exec /private/var/folders/7p/dsbvg0k54139kxmr4fyd864w0000gn/T/GoLand/___go_build_codeup_aliyun_com_607fcd17c06031c0ebd327fc_canopia_be_business_cmd_rpc 

couldn't start listener: listen tcp 10.xx.xxx.129:63466: bind: can't assign requested address
Debugger finished with the exit code 1

​

解决思路

  1. debug无法找到正确的本地IP地址,尝试修改 /etc/hosts 强行指定

  2. goland dlv 强行指定debug进程启动时候的listen地址为127.0.0.1 (无法配置)

分析

1.日志中出现的IP,可以发现是远程dns的服务器VIP(多个ip轮巡)

2.测试 `ping localhost` ,发现解析出来的地址不是 127.0.0.1 ,还是远程dns的ip

3.尝试通过修改 /etc/hosts 和 /etc/resolv.conf 文件

  • /etc/hosts 理论上是优先级最高的配置,但是可能被一些信息安全工具修改

  • /etc/resolv.conf 一般自动修改,不需要手动,但是也可能被工具修改

尝试手动修改2个文件,发现还是不生效

4.尝试清理dns缓存,保证 /etc/hosts 生效

复制代码
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

清理后并没有效果...

5.尝试在 /etc/hosts 文件中配置其他,比如

复制代码
10.xx.xx.104 github.com

ping github.com 的时候无法解析到 10.xx.xx.104

6.问题逐渐变成 /etc/hosts 为什么不生效?

7.Google & chatgpt 查询,得到了一些可能的问题

  • 需要保证 /etc/hosts 是属于 root 用户 wheel 组,并且权限是644

    sudo ls -l /etc/hosts

    -rw-r--r--@ 1 root wheel 2110 8 7 22:02 /etc/hosts

  • 保证mac的网络配置是关闭自动ipv6的,使用仅本地连接

  • 某些操作系统版本,严格限制 /etc/hosts 内容的格式(或者说是bug),才会生效

要求key value之间只能有一个空格...或者tab(一个或者多个),无法混用

复制代码
# 每行的key value只能有一个空格,或者一个/多个 tab
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1	localhost

解决

上面分析中需要保证的部分全部修改,之后 /etc/hosts 立刻生效;

结论:

  1. goland dlv启动本地debug进程默认是localhost,并且无法修改

2./etc/hosts 文件修改是立刻生效的,如果不生效,说明文件本身存在问题

相关推荐
春波petal1 小时前
MacOS 13.7.8版本-前端环境一键搭建指南
前端·macos
桂花很香,旭很美14 小时前
[7天实战入门Go语言后端] Day 2:用 Go 写一个 HTTP 服务——net/http 入门
http·golang·xcode
eppen18 小时前
macOS 快捷键修改
macos
我不是8神19 小时前
go-zero微服务框架总结
开发语言·微服务·golang
郁大锤21 小时前
macOS 终端 SSH 到 Linux 后快捷键失效解决方法
linux·macos·ssh
阿捏利1 天前
详解Mach-O(十三)Mach-O __TEXT
macos·ios·c/c++·mach-o
遇见你的雩风1 天前
【Golang】--- Goroutine
开发语言·golang
Laughtin1 天前
macos的python安装选择以及homebrew python的安装方法
开发语言·python·macos
YGGP1 天前
【Golang】LeetCode 189. 轮转数组
开发语言·leetcode·golang
猫头虎1 天前
OpenClaw相关的开源AI项目汇总大全:本文涵盖近期所有OpenClaw相关的GitHub高星star热门项目
运维·人工智能·macos·docker·容器·开源·github