linux查询外网ip命令
linux查询外网ip命令详细介绍
在 Linux 系统中,查询外网 IP(公网 IP)通常需要通过访问外部服务来获取,以下是几种常用方法:
方法 1:使用
curl 命令(推荐)
curl 是常用的网络请求工具,可通过访问提供公网 IP 查询的服务获取结果。
示例命令:
bash
icanhazip.com
icanhazip.com 是一个专门返回纯文本公网 IP 的服务,输出结果仅包含 IP 地址(无额外信息)。
方法 2:使用
wget 命令(无
curl 时备用)
若系统未安装 curl(部分轻量系统可能默认不安装),可使用 wget 替代:
示例命令:
bash
-qO- icanhazip.com
-q 表示静默模式(不输出下载过程),-O- 表示将结果输出到标准输出。
其他可选服务(备用)
若某个服务不可用,可尝试其他提供类似功能的站点,例如:
bash
ifconfig.meident.me
ipinfo.io/ip
注意事项
- 需确保设备能访问公网(如能正常访问
icanhazip.com),否则命令会超时或失败。 - 若提示
curl: command not found或wget: command not found,需先安装对应工具(如sudo apt install curl或sudo apt install wget)。