上一篇                     
               
			  Linux查看IP命令是什么
- Linux
- 2025-06-22
- 5013
 使用
 
 
ifconfig(需安装net-tools)或
 ip addr命令查询网络接口IP,
 hostname -I可快速获取本机IP地址。
在Linux系统中查询IP地址是网络管理的基础操作,无论是配置服务器、排查连接问题还是监控网络状态都需要掌握,以下是多种详细方法,涵盖不同场景:
查询本机IP地址
ip 命令(推荐)
 
ip addr show
- 输出解析: 
  - inet后是IPv4地址(如- 168.1.100/24)。
- inet6后是IPv6地址。
- 网卡名称如 eth0(有线)或wlan0(无线)。
 
- 精简用法: ip -br -c addr show # 简洁格式并高亮显示 
ifconfig(传统工具,部分系统需安装)
 
ifconfig
- 若未安装:sudo apt install net-tools(Debian/Ubuntu)或sudo yum install net-tools(CentOS/RHEL)。
- 输出中找 inet字段对应的IP。
hostname 命令
 
hostname -I # 显示所有非回环IP(适用于快速获取)
查询公网IP(外部IP)
当需要获取服务器对外的公网地址时:

curl ifconfig.me # 最常用 curl icanhazip.com # 备用方案 curl ipinfo.io/ip # 返回纯IP无多余信息
注意:需安装
curl(sudo apt install curl或sudo yum install curl)。
查询远程主机IP
域名解析(DNS查询)
nslookup example.com # 交互式查询 dig example.com # 输出更详细(安装:`sudo apt install dnsutils`)
- 输出中找 ANSWER SECTION下的IP地址。
Ping 测试
ping -c 4 example.com # 发送4个包并显示目标IP
- 首行显示 PING example.com (203.0.113.5)即为解析后的IP。
特殊场景处理
仅显示特定网卡的IP
ip addr show eth0 # 只查看eth0网卡 ifconfig wlan0 # 只查看无线网卡
过滤IP地址(脚本自动化)
   ip -o -4 addr show | awk '{print $4}' | cut -d '/' -f1  # 提取所有IPv4
   hostname -I | awk '{print $1}'  # 取第一个IP 
无外网时获取公网IP
- 登录路由器管理界面查看分配的公网IP(适用于本地服务器)。
常见问题解决
-  命令未找到:  - 安装缺失工具: sudo apt install iproute2 net-tools curl dnsutils # Debian/Ubuntu sudo yum install iproute net-tools curl bind-utils # CentOS/RHEL 
 
- 安装缺失工具: 
-  无IP显示: - 检查网卡状态:ip link show(确认UP状态)。
- 重启网络:sudo systemctl restart NetworkManager或sudo systemctl restart networking。
 
- 检查网卡状态:
-  公网IP与预期不符: 可能处于NAT后(如家庭网络),需通过路由器或云平台控制台查看真实公网IP。  
- 基础查询:优先用 ip addr show(现代系统)或hostname -I(快速获取)。
- 公网IP:使用 curl ifconfig.me。
- 远程主机:ping或nslookup。
- 脚本处理:结合 awk/grep过滤输出。
 基于 Linux 内核文档(kernel.org)及iproute2官方手册,命令兼容主流发行版(Ubuntu 20.04+、CentOS 7+、Debian 11+),实际操作前请确保权限正确(普通用户需sudo提权)。
 
  
			