Linux ip 命令教程
ip 命令是 Linux 网络配置的核心工具,替代了传统的 ifconfig、route、arp 等命令,功能更强大、覆盖场景更全面。本文将通过「命令 + 实际执行结果 + 解析」的方式,讲解 ip 命令核心用法,
一、基础语法与核心选项
1. 语法格式
bash
ip [选项] 对象 { 命令 | help }
ip -force -batch 文件名 # 批量执行配置(从文件读取命令)
2. 高频选项说明(带执行效果)
| 选项 | 功能说明 | 执行示例 + 结果演示 |
|---|---|---|
-V/--version |
查看命令版本 | bash root@debian:/tmp# ip -V ip utility, iproute2-ss190123 |
-br/--brief |
简洁输出(仅关键信息) | 后续所有对象操作都会演示,核心是「去冗余、留核心」 |
-s/--statistics |
显示统计信息(收发字节/包) | 见 link 和 addr 对象示例 |
-4/-6 |
仅操作 IPv4/IPv6 | bash root@debian:/tmp# ip -4 addr show lo # 只显示 IPv4 地址 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever |
-c/--color |
彩色输出(区分状态) | 执行后接口状态(UP/DOWN)、IP 类型会用不同颜色标注,便于视觉识别 |
-o/--oneline |
单行输出(脚本解析用) | bash root@debian:/tmp# ip -o link show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 |
3. 核心操作对象(对应功能场景)
| 对象 | 核心功能 | 替代的传统命令 |
|---|---|---|
link |
管理网卡(启用/禁用、重命名、MAC 配置) | ifconfig、ifup、ifdown |
address/addr |
配置网卡 IP 地址(添加/删除/查看) | ifconfig |
route |
管理路由表(静态路由、默认网关) | route |
neighbor/neighbour |
管理 ARP 缓存(IP-MAC 映射) | arp |
netns |
网络命名空间(隔离网络环境) | 无 |
tunnel |
创建 IP 隧道(如 GRE、IPIP) | iptunnel |
maddress |
组播地址管理 | netstat -g |
二、核心对象实战(命令 + 结果 + 解析)
1. link 对象:网卡管理(最常用)
用于查看网卡状态、启用禁用、修改 MAC、重命名等,是网络配置的基础。
(1)查看所有网卡信息
-
简洁查看(日常排查首选):
bashroot@debian:/tmp# ip -br link show执行结果:
lo UP 00:00:00:00:00:00 eth0 UP 00:1e:06:3a:8b:1c wlan0 DOWN 7c:1d:d9:5e:f2:a3 docker0 DOWN 02:42:7d:9b:6e:00解析:列含义依次为「网卡名 → 状态(UP 启用/DOWN 禁用)→ MAC 地址」
-
详细查看(含统计信息):
bashroot@debian:/tmp# ip -s link show eth0执行结果:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 link/ether 00:1e:06:3a:8b:1c brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast 12568903 15230 0 0 0 456 TX: bytes packets errors dropped carrier collsns 8976231 11890 0 0 0 0解析:显示网卡状态(UP)、MTU(1500)、收发字节数(RX/TX bytes)、错误包/丢包数(均为 0,正常)
(2)启用/禁用网卡
-
禁用 eth0 网卡:
bashroot@debian:/tmp# ip link set eth0 down执行结果:无输出(成功),验证状态:
bashroot@debian:/tmp# ip -br link show eth0 eth0 DOWN 00:1e:06:3a:8b:1c -
启用 eth0 网卡:
bashroot@debian:/tmp# ip link set eth0 up验证结果:
bashroot@debian:/tmp# ip -br link show eth0 eth0 UP 00:1e:06:3a:8b:1c
(3)重命名网卡(eth0 → lan0)
bash
# 步骤1:先禁用网卡
root@debian:/tmp# ip link set eth0 down
# 步骤2:重命名
root@debian:/tmp# ip link set eth0 name lan0
# 步骤3:重新启用
root@debian:/tmp# ip link set lan0 up
验证结果:
bash
root@debian:/tmp# ip -br link show lan0
lan0 UP 00:1e:06:3a:8b:1c
(4)修改网卡 MAC 地址(伪装)
bash
# 步骤1:禁用网卡
root@debian:/tmp# ip link set lan0 down
# 步骤2:修改 MAC(自定义为 00:11:22:33:44:55)
root@debian:/tmp# ip link set lan0 address 00:11:22:33:44:55
# 步骤3:启用网卡
root@debian:/tmp# ip link set lan0 up
验证结果:
bash
root@debian:/tmp# ip -br link show lan0
lan0 UP 00:11:22:33:44:55
(5)设置网卡 MTU 值(如 VPN 场景)
bash
root@debian:/tmp# ip link set lan0 mtu 1400
验证结果:
bash
root@debian:/tmp# ip link show lan0 | grep mtu
2: lan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1400 qdisc mq state UP mode DEFAULT group default qlen 1000
2. address/addr 对象:IP 地址管理
用于查看、添加、删除网卡的 IPv4/IPv6 地址,支持单网卡多 IP。
(1)查看所有 IP 地址
-
简洁查看(日常首选):
bashroot@debian:/tmp# ip -br addr show执行结果:
lo UP 127.0.0.1/8 ::1/128 lan0 UP 192.168.1.105/24 fe80::21e:6ff:fe3a:8b1c/64 wlan0 DOWN docker0 DOWN 172.17.0.1/16解析:列含义「网卡名 → 状态 → IP 地址(IPv4/IPv6 并列)」
-
仅查看 IPv4 地址:
bashroot@debian:/tmp# ip -4 -br addr show执行结果:
lo UP 127.0.0.1/8 lan0 UP 192.168.1.105/24 docker0 DOWN 172.17.0.1/16
(2)添加临时 IP 地址(重启失效)
-
给 lan0 添加第二个 IPv4(别名 IP):
bashroot@debian:/tmp# ip addr add 192.168.1.200/24 dev lan0 label lan0:1验证结果:
bashroot@debian:/tmp# ip -4 -br addr show lan0 lan0 UP 192.168.1.105/24 192.168.1.200/24 -
添加 IPv6 地址:
bashroot@debian:/tmp# ip addr add 2001:db8::1/64 dev lan0验证结果:
bashroot@debian:/tmp# ip -6 -br addr show lan0 lan0 UP fe80::21e:6ff:fe3a:8b1c/64 2001:db8::1/64
(3)删除 IP 地址
bash
# 删除 192.168.1.200/24
root@debian:/tmp# ip addr del 192.168.1.200/24 dev lan0
验证结果:
bash
root@debian:/tmp# ip -4 -br addr show lan0
lan0 UP 192.168.1.105/24
(4)清空网卡所有 IP(谨慎使用)
bash
root@debian:/tmp# ip addr flush dev lan0
验证结果:
bash
root@debian:/tmp# ip -br addr show lan0
lan0 UP
3. route 对象:路由表与网关管理
路由表决定数据包的转发路径,route 对象用于配置静态路由和默认网关。
(1)查看路由表
-
简洁查看(日常排查):
bashroot@debian:/tmp# ip -br route show执行结果:
default via 192.168.1.1 dev lan0 proto dhcp src 192.168.1.105 metric 100 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown 192.168.1.0/24 dev lan0 proto kernel scope link src 192.168.1.105 metric 100解析:
default:默认路由(所有未匹配的网络请求走这里)via 192.168.1.1:网关 IP(路由器地址)dev lan0:出口网卡192.168.1.0/24:直连网段(无需网关,直接通信)
-
查看 IPv6 路由表:
bashroot@debian:/tmp# ip -6 -br route show执行结果:
::1 dev lo proto kernel scope host src ::1 2001:db8::/64 dev lan0 proto kernel scope link src 2001:db8::1 fe80::/64 dev lan0 proto kernel scope link src fe80::21e:6ff:fe3a:8b1c metric 100
(2)添加静态路由(跨网段访问)
场景:访问 192.168.2.0/24 网段,需通过网关 192.168.1.254 转发
bash
root@debian:/tmp# ip route add 192.168.2.0/24 via 192.168.1.254 dev lan0
验证结果:
bash
root@debian:/tmp# ip -br route show
default via 192.168.1.1 dev lan0 proto dhcp src 192.168.1.105 metric 100
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
192.168.1.0/24 dev lan0 proto kernel scope link src 192.168.1.105 metric 100
192.168.2.0/24 via 192.168.1.254 dev lan0 # 新增静态路由
(3)设置默认网关(临时)
bash
# 先删除原有默认网关(可选)
root@debian:/tmp# ip route del default
# 添加新默认网关
root@debian:/tmp# ip route add default via 192.168.1.2 dev lan0
验证结果:
bash
root@debian:/tmp# ip -br route show
default via 192.168.1.2 dev lan0 # 新网关生效
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
192.168.1.0/24 dev lan0 proto kernel scope link src 192.168.1.105 metric 100
(4)删除静态路由
bash
root@debian:/tmp# ip route del 192.168.2.0/24
验证结果:该路由条目消失
4. neighbor/neighbour 对象:ARP 缓存管理
ARP 缓存是 IP 地址与 MAC 地址的映射表,用于局域网通信。
(1)查看 ARP 缓存
bash
root@debian:/tmp# ip neigh show
执行结果:
192.168.1.1 dev lan0 lladdr 00:1c:42:a8:6d:3f REACHABLE
192.168.1.108 dev lan0 lladdr 78:2b:cb:4d:9e:71 STALE
192.168.1.254 dev lan0 lladdr 00:0c:29:8f:3e:1a FAILED
状态解析:
REACHABLE:可达(最近10秒内有通信)STALE:过期(超过10秒无通信,缓存未清除)FAILED:通信失败(ARP 请求无响应)
(2)添加静态 ARP 条目(固定 IP-MAC 映射)
bash
root@debian:/tmp# ip neigh add 192.168.1.20 lladdr 00:11:22:33:44:55 dev lan0
验证结果:
bash
root@debian:/tmp# ip neigh show
192.168.1.20 dev lan0 lladdr 00:11:22:33:44:55 PERMANENT # PERMANENT 表示静态条目
192.168.1.1 dev lan0 lladdr 00:1c:42:a8:6d:3f REACHABLE
...
(3)删除 ARP 条目
bash
root@debian:/tmp# ip neigh del 192.168.1.20 dev lan0
验证结果:该 ARP 条目消失
5. netns 对象:网络命名空间(隔离网络环境)
网络命名空间可实现"一台主机多个独立网络环境"(如容器、虚拟机),每个命名空间有独立的网卡、IP、路由。
(1)创建网络命名空间
bash
root@debian:/tmp# ip netns add ns1
验证结果:
bash
root@debian:/tmp# ip netns list
ns1
(2)在命名空间内执行命令
格式:ip netns exec 命名空间 命令
-
查看 ns1 内的网卡:
bashroot@debian:/tmp# ip netns exec ns1 ip -br link show执行结果:
lo DOWN 00:00:00:00:00:00 -
启用 ns1 内的回环网卡并添加 IP:
bashroot@debian:/tmp# ip netns exec ns1 ip link set lo up root@debian:/tmp# ip netns exec ns1 ip addr add 127.0.0.1/8 dev lo验证结果:
bashroot@debian:/tmp# ip netns exec ns1 ip -br addr show lo UP 127.0.0.1/8 -
在 ns1 内测试 ping:
bashroot@debian:/tmp# ip netns exec ns1 ping -c 2 127.0.0.1执行结果:
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.032 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.028 ms --- 127.0.0.1 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 0.028/0.030/0.032/0.002 ms
(3)命名空间间通信(虚拟网卡对)
实现主机(默认命名空间)与 ns1 之间的通信:
bash
# 步骤1:创建虚拟网卡对(veth0 和 veth1,成对存在)
root@debian:/tmp# ip link add veth0 type veth peer name veth1
# 步骤2:将 veth1 移动到 ns1 命名空间
root@debian:/tmp# ip link set veth1 netns ns1
# 步骤3:给两块网卡配置 IP 并启用
root@debian:/tmp# ip addr add 192.168.100.1/24 dev veth0
root@debian:/tmp# ip link set veth0 up
root@debian:/tmp# ip netns exec ns1 ip addr add 192.168.100.2/24 dev veth1
root@debian:/tmp# ip netns exec ns1 ip link set veth1 up
测试通信:
-
主机 ping ns1 内的 veth1:
bashroot@debian:/tmp# ping -c 2 192.168.100.2执行结果:
PING 192.168.100.2 (192.168.100.2) 56(84) bytes of data. 64 bytes from 192.168.100.2: icmp_seq=1 ttl=64 time=0.054 ms 64 bytes from 192.168.100.2: icmp_seq=2 ttl=64 time=0.048 ms --- 192.168.100.2 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 0.048/0.051/0.054/0.003 ms -
ns1 内 ping 主机的 veth0:
bashroot@debian:/tmp# ip netns exec ns1 ping -c 2 192.168.100.1执行结果:通信成功(与上面类似)
(4)删除网络命名空间
bash
root@debian:/tmp# ip netns delete ns1
验证结果:
bash
root@debian:/tmp# ip netns list
# 无输出(ns1 已删除)
6. 其他常用对象(实用场景)
(1)tunnel:创建 GRE 隧道(跨网段通信)
场景:本地 IP 192.168.1.105,远端 IP 192.168.3.200,通过 GRE 隧道互通 10.0.0.0/24 网段
bash
# 本地创建 GRE 隧道
root@debian:/tmp# ip tunnel add gre1 mode gre remote 192.168.3.200 local 192.168.1.105 ttl 255
root@debian:/tmp# ip link set gre1 up
root@debian:/tmp# ip addr add 10.0.0.1/24 dev gre1
验证隧道状态:
bash
root@debian:/tmp# ip -br link show gre1
gre1 UP 00:00:00:00:00:00
远端配置(需在 192.168.3.200 执行):
bash
ip tunnel add gre1 mode gre remote 192.168.1.105 local 192.168.3.200 ttl 255
ip link set gre1 up
ip addr add 10.0.0.2/24 dev gre1
测试隧道通信:
bash
root@debian:/tmp# ping -c 2 10.0.0.2
# 通信成功表示隧道正常
(2)maddress:查看组播地址
bash
root@debian:/tmp# ip maddress show dev lan0
执行结果:
2: lan0
link 33:33:00:00:00:01
link 33:33:ff:3a:8b:1c
link ff:ff:ff:ff:ff:ff
解析 :33:33 开头为 IPv6 组播 MAC,ff:ff:ff:ff:ff:ff 为 IPv4 广播 MAC
三、实战组合命令(排查/配置场景)
1. 快速排查网络故障
bash
# 步骤1:查看网卡状态和 IP(确认网卡启用、IP 正确)
root@debian:/tmp# ip -br link show && ip -br addr show
# 步骤2:查看路由表(确认默认网关正确)
root@debian:/tmp# ip -br route show
# 步骤3:查看 ARP 缓存(确认网关 MAC 可达)
root@debian:/tmp# ip neigh show | grep REACHABLE
# 步骤4:测试网关连通性
root@debian:/tmp# ping -c 2 $(ip route show default | awk '{print $3}')
2. 临时配置网卡(测试环境)
bash
# 禁用网卡 → 配置 IP → 启用 → 设置网关 → 配置 DNS
root@debian:/tmp# ip link set lan0 down
root@debian:/tmp# ip addr add 192.168.1.150/24 dev lan0
root@debian:/tmp# ip link set lan0 up
root@debian:/tmp# ip route add default via 192.168.1.1 dev lan0
root@debian:/tmp# echo "nameserver 8.8.8.8" > /etc/resolv.conf # 临时 DNS
3. 批量执行配置(-batch 选项)
bash
# 步骤1:创建配置文件 ip-config.txt
root@debian:/tmp# cat > ip-config.txt << EOF
link set lan0 up
addr add 192.168.1.200/24 dev lan0
route add default via 192.168.1.1 dev lan0
neigh flush dev lan0
EOF
# 步骤2:批量执行
root@debian:/tmp# ip -batch ip-config.txt
验证结果:所有命令批量执行成功,无需逐一输入
四、关键注意事项
-
临时生效 vs 永久生效:
ip命令直接执行的配置(IP、路由、MAC 等)均为临时生效,重启网络(systemctl restart networking)或主机后丢失。- 永久生效需修改配置文件:
- Debian/Ubuntu:
/etc/network/interfaces - CentOS/RHEL:
/etc/sysconfig/network-scripts/ifcfg-网卡名
- Debian/Ubuntu:
-
权限要求 :所有
ip命令需root权限(或前缀加sudo),普通用户执行会报错「Permission denied」。 -
安装说明 :
ip命令属于iproute2工具集,主流 Linux 发行版(CentOS 7+、Ubuntu 14.04+、Debian 8+)默认安装,若未安装:bash# Debian/Ubuntu apt install iproute2 # CentOS/RHEL yum install iproute2 -
传统命令替代表(快速迁移):
传统命令 对应的 ip 命令 ifconfigip -br addr show/ip -br link showifup eth0ip link set eth0 upifdown eth0ip link set eth0 downroute -nip -br route showarp -aip neigh showiptunnelip tunnel
五、获取帮助
若需查看某个对象的详细用法,直接执行 ip 对象 help:
bash
root@debian:/tmp# ip route help # 查看 route 对象的所有命令
root@debian:/tmp# ip link help # 查看 link 对象的所有命令
通过本教程的「命令 + 结果 + 解析」模式,你可以直观掌握 ip 命令的所有核心用法,覆盖日常网络配置、故障排查、环境隔离等场景。实际使用中,可根据需求组合选项和对象,灵活适配不同场景。