在线查询本地外网ip地址
下面给你一组 用 curl 在线查询本机外网 IP 的常用命令(含 IPv4/IPv6、JSON、容器/K8s、代理等场景)。直接复制执行即可。
最简用法
# 自动返回公网 IP(常见且稳定)
curl -s https://ipinfo.io/ip
curl -s https://api.ipify.org
curl -s https://icanhazip.com
指定 IPv4 / IPv6
curl -4 -s https://ip.sb # 强制 IPv4
curl -6 -s https://ip.sb # 强制 IPv6
curl -4 -s https://ifconfig.co
curl -6 -s https://ifconfig.co
要 JSON(便于脚本解析)
curl -s https://ipinfo.io/json
curl -s https://ifconfig.co/json
curl -s https://httpbin.org/ip
# 仅取 IP 字段(需装 jq)
curl -s https://ipinfo.io/json | jq -r '.ip'
国内可访问性更好的几个
curl -s https://ip.sb
curl -s https://cip.cc # 会返回IP+归属地(纯文本)
curl -s https://myip.ipip.net # 中文说明(纯文本)
超时、静默与健壮性
# 设置 3 秒超时;失败不阻塞脚本
curl -sS --max-time 3 https://api.ipify.org || echo "查询失败"
一条命令多源兜底(取第一个成功的)
for u in https://ipinfo.io/ip https://api.ipify.org https://ifconfig.co/ip https://ip.sb; do
ip=$(curl -sS --max-time 3 "$u") && [ -n "$ip" ] && echo "$ip" && break
done
走代理/指定出口(排查代理链路)
# 指定 HTTP/HTTPS 代理
curl -s --proxy http://user:pass@proxy-host:port https://api.ipify.org
# 指定本机网卡(仅影响本机选路,不一定改变NAT后的公网IP)
curl -s --interface eth0 https://api.ipify.org
Docker / Kubernetes 内查看 Pod 的“出口公网IP”
# Docker 容器
docker run --rm curlimages/curl -s https://ip.sb
# Kubernetes Pod(需要有权限)
kubectl run tmp-curl --rm -it --image=curlimages/curl -- \
sh -c "curl -s https://ip.sb"
额外:只要 IP(去掉换行)
curl -s https://api.ipify.org | tr -d '\n'