在Linux上查看公网IP(curl)

最后更新: 2026-05-31

在无界面的服务器上无法“看网页”,因此要查询一个回显服务,让它返回它所看到的IP。

使用 curl

curl https://show-ip-addr.com/api/myip   # JSON:ip、hostname、country、org
curl ifconfig.me                          # 纯IP
curl -s https://api.ipify.org             # 纯IP

从我们的JSON中只提取IP:

curl -s https://show-ip-addr.com/api/myip | grep -oP '"ip":"\K[^"]+'

使用 dig(基于DNS,无需HTTP)

dig +short myip.opendns.com @resolver1.opendns.com
dig -4 +short txt ch whoami.cloudflare @1.1.1.1

内网与公网

记住 ip addr 显示的是 内网 IP,而非公网——见 Linux IP命令如何查看我的IP

curl -4 / curl -6 强制IPv4或IPv6。当前部署下 /api/myip 返回IPv4(服务器通过IPv4联网);/api/ip?q= 和各查询工具同样接受IPv6。

参考资料