linux查看本机服务器的外网IP命令

在 Linux 中查看本机服务器的外网 IP(公网 IP)可以通过以下几种方法:

1. 使用 curl 查询外部服务(推荐)

curl ifconfig.me

或:

curl icanhazip.com

或:

curl ipinfo.io/ip

这些服务会返回你的公网 IP 地址。

2. 使用 wget 替代 curl

wget -qO- ifconfig.me

或:

wget -qO- icanhazip.com

3. 使用 dig 查询 DNS 记录(适用于动态 DNS 或特定场景)

dig +short myip.opendns.com @resolver1.opendns.com

4. 使用 hostnslookup

host myip.opendns.com resolver1.opendns.com

或:

nslookup myip.opendns.com resolver1.opendns.com

5. 使用 ipifconfig 查看本地网络接口(仅显示内网 IP,不推荐用于外网 IP)

ip a

或:

ifconfig

(这些命令通常只显示内网 IP,如 192.168.x.x10.x.x.x,不适用于直接获取公网 IP。)


注意事项

  • 如果服务器位于 NAT 或防火墙后(如家用路由器、云服务器内网环境),上述方法返回的是网关或云服务商分配的公网 IP。
  • 某些云服务器(如 AWS、阿里云)的公网 IP 可能需要通过控制台或元数据服务查询,例如:
    curl http://169.254.169.254/latest/meta-data/public-ipv4  # AWS EC2
    

选择最简单的方法(如 curl ifconfig.me)即可快速获取外网 IP。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值