Files

465 lines
9.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# IP 系列命令管理 Linux 网络完全指南
## 一、IP 命令概述
`ip` 命令是 `iproute2` 工具包的一部分,取代了传统的 `ifconfig`、`route`、`arp` 等命令,提供更强大和统一的网络管理功能。
### 基本语法结构
```bash
ip [OPTIONS] OBJECT { COMMAND | help }
```
**常用 OBJECT(对象):**
- `link` - 网络接口管理
- `address` - IP地址管理
- `route` - 路由表管理
- `neigh` - ARP/邻居表管理
- `netns` - 网络命名空间管理
- `maddr` - 多播地址管理
---
## 二、网络接口管理(link)
### 1. 查看接口信息
```bash
# 查看所有接口状态
ip link show
ip -s link show # 带统计信息
ip -s -s link show # 更详细的统计信息
# 查看特定接口
ip link show eth0
ip -s link show eth0
```
### 2. 接口启停控制
```bash
# 启用接口
ip link set eth0 up
# 禁用接口
ip link set eth0 down
# 修改接口MTU
ip link set eth0 mtu 9000
# 修改MAC地址
ip link set eth0 address 00:11:22:33:44:55
```
### 3. 接口重命名
```bash
# 临时重命名
ip link set eth0 name eth-new
# 永久重命名(需要配合系统配置)
# 1. 先禁用接口
ip link set eth0 down
# 2. 重命名
ip link set eth0 name eth-new
# 3. 启用
ip link set eth-new up
```
### 4. 查看接口详情
```bash
ip -d link show # 显示接口详细信息
ip -d link show eth0
```
**输出示例:**
```
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:xx:xx:xx brd ff:ff:ff:ff:ff:ff promiscuity 0
addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
```
---
## 三、IP地址管理(address/addr)
### 1. 查看IP地址
```bash
# 查看所有接口IP地址
ip address show
ip addr show
ip a # 简写
# 查看特定接口
ip addr show eth0
ip a show dev eth0
# 查看IPv4或IPv6地址
ip -4 addr show # 只显示IPv4
ip -6 addr show # 只显示IPv6
```
### 2. 添加IP地址
```bash
# 添加IPv4地址
ip addr add 192.168.1.100/24 dev eth0
ip addr add 192.168.1.101/24 dev eth0 label eth0:1
# 添加IPv6地址
ip addr add 2001:db8::1/64 dev eth0
```
### 3. 删除IP地址
```bash
# 删除指定IP
ip addr del 192.168.1.100/24 dev eth0
# 删除所有IP(慎用)
ip addr flush dev eth0
ip addr flush dev eth0 scope global # 只删除全局地址
```
### 4. 清理和刷新地址
```bash
# 清理特定类型的地址
ip addr flush dynamic # 清理动态获取的地址
ip addr flush permanent # 清理永久地址
ip addr flush temporary # 清理临时地址
# 清理IPv6特定地址
ip -6 addr flush dev eth0
```
---
## 四、路由表管理(route)
### 1. 查看路由表
```bash
# 查看所有路由
ip route show
ip route list
ip r # 简写
# 查看特定网络路由
ip route show 192.168.1.0/24
# 查看特定接口路由
ip route show dev eth0
# 查看缓存路由
ip route show cache
# 查看详细路由信息
ip -d route show
```
### 2. 添加路由
```bash
# 添加默认网关
ip route add default via 192.168.1.1 dev eth0
# 添加网络路由
ip route add 10.0.0.0/8 via 192.168.1.254 dev eth0
# 添加直连路由
ip route add 192.168.2.0/24 dev eth1
# 添加多路径路由
ip route add default nexthop via 192.168.1.1 weight 1 \
nexthop via 192.168.2.1 weight 2
```
### 3. 删除路由
```bash
# 删除默认路由
ip route del default
# 删除特定路由
ip route del 10.0.0.0/8 via 192.168.1.254
ip route del 192.168.2.0/24 dev eth1
```
### 4. 修改和替换路由
```bash
# 替换路由(如果存在则替换,不存在则添加)
ip route replace 192.168.1.0/24 via 10.0.0.1 dev eth0
# 修改路由属性
ip route change 192.168.1.0/24 via 10.0.0.2 dev eth0 metric 100
```
### 5. 特殊路由操作
```bash
# 清空路由表
ip route flush table main
ip route flush cache # 清空路由缓存
# 添加策略路由
ip route add table 100 default via 10.0.0.1
# 查看特定路由表
ip route show table local
ip route show table main
```
---
## 五、邻居表管理(ARP/NDP)
### 1. 查看邻居表
```bash
# 查看ARP表(IPv4)
ip neigh show
ip neighbor show
# 查看IPv6邻居发现表
ip -6 neigh show
# 查看特定接口
ip neigh show dev eth0
```
### 2. 添加/删除ARP条目
```bash
# 添加静态ARP条目
ip neigh add 192.168.1.100 lladdr 00:11:22:33:44:55 dev eth0 nud permanent
# 删除ARP条目
ip neigh del 192.168.1.100 dev eth0
# 刷新ARP表
ip neigh flush dev eth0
```
### 3. 邻居状态说明
- `permanent` - 永久条目,手动添加
- `noarp` - 不需ARP验证
- `reachable` - 可达状态
- `stale` - 陈旧状态
- `delay` - 延迟验证
- `failed` - 验证失败
---
## 六、网络命名空间管理(netns)
### 1. 命名空间操作
```bash
# 创建命名空间
ip netns add ns1
ip netns add ns2
# 列出所有命名空间
ip netns list
# 在命名空间中执行命令
ip netns exec ns1 ip addr show
ip netns exec ns1 bash # 进入命名空间的shell
# 删除命名空间
ip netns del ns1
```
### 2. 命名空间网络配置
```bash
# 创建虚拟以太网对
ip link add veth0 type veth peer name veth1
# 将一端移到命名空间
ip link set veth1 netns ns1
# 在命名空间内配置
ip netns exec ns1 ip addr add 10.0.0.1/24 dev veth1
ip netns exec ns1 ip link set veth1 up
```
---
## 七、网络统计和监控
### 1. 查看网络统计
```bash
# 查看所有接口统计
ip -s link show
# 查看特定接口详细统计
ip -s -s link show eth0
# 持续监控网络统计
watch -n 1 'ip -s link show eth0'
```
### 2. 监控网络事件
```bash
# 监控网络事件(需要root)
ip monitor all # 监控所有事件
ip monitor link # 只监控链路事件
ip monitor address # 只监控地址事件
```
---
## 八、高级网络配置
### 1. 多播地址管理
```bash
# 查看多播地址
ip maddr show
ip maddr show dev eth0
# 添加多播地址
ip maddr add 224.0.0.1 dev eth0
# 删除多播地址
ip maddr del 224.0.0.1 dev eth0
```
### 2. 策略路由
```bash
# 查看路由规则
ip rule show
# 添加路由规则
ip rule add from 192.168.1.0/24 table 100
ip rule add fwmark 1 table 100
# 删除路由规则
ip rule del from 192.168.1.0/24
```
### 3. 隧道配置
```bash
# 创建GRE隧道
ip tunnel add gre1 mode gre remote 203.0.113.1 local 192.0.2.1 ttl 255
ip link set gre1 up
ip addr add 10.0.0.1/30 dev gre1
```
---
## 九、实用脚本示例
### 1. 网络配置脚本
```bash
#!/bin/bash
# 配置多IP地址
INTERFACE="eth0"
NETWORK="192.168.1"
# 清除现有配置
ip addr flush dev $INTERFACE
# 添加主IP
ip addr add ${NETWORK}.100/24 dev $INTERFACE
# 添加多个辅助IP
for i in {101..110}; do
ip addr add ${NETWORK}.$i/24 dev $INTERFACE label ${INTERFACE}:$i
done
# 设置默认网关
ip route add default via ${NETWORK}.1
# 启用接口
ip link set $INTERFACE up
```
### 2. 网络诊断脚本
```bash
#!/bin/bash
# 网络状态诊断
echo "=== 接口状态 ==="
ip link show | grep -E "^[0-9]+:"
echo -e "\n=== IP地址 ==="
ip addr show | grep -E "inet|inet6"
echo -e "\n=== 路由表 ==="
ip route show
echo -e "\n=== ARP表 ==="
ip neigh show
echo -e "\n=== 连接统计 ==="
ss -s
```
### 3. 批量配置脚本
```bash
#!/bin/bash
# 批量配置网络命名空间
for i in {1..5}; do
# 创建命名空间
ip netns add ns$i
# 创建虚拟接口对
ip link add veth$i type veth peer name vpeer$i
# 配置主端
ip link set veth$i up
ip addr add 10.0.$i.1/24 dev veth$i
# 配置命名空间端
ip link set vpeer$i netns ns$i
ip netns exec ns$i ip link set vpeer$i up
ip netns exec ns$i ip addr add 10.0.$i.2/24 dev vpeer$i
ip netns exec ns$i ip route add default via 10.0.$i.1
done
```
---
## 十、IP命令与传统命令对照表
| **功能** | **传统命令** | **ip命令** |
|---------|------------|-----------|
| 查看接口 | `ifconfig -a` | `ip link show` |
| 配置IP | `ifconfig eth0 192.168.1.100 netmask 255.255.255.0` | `ip addr add 192.168.1.100/24 dev eth0` |
| 查看路由 | `route -n` | `ip route show` |
| 添加路由 | `route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.1` | `ip route add 10.0.0.0/8 via 192.168.1.1` |
| 查看ARP | `arp -an` | `ip neigh show` |
| 添加ARP | `arp -s 192.168.1.100 00:11:22:33:44:55` | `ip neigh add 192.168.1.100 lladdr 00:11:22:33:44:55 dev eth0` |
---
## 十一、常见问题排查
### 1. 网络不通排查步骤
```bash
# 1. 检查接口状态
ip link show eth0
# 2. 检查IP配置
ip addr show eth0
# 3. 检查路由
ip route show
ip route get 8.8.8.8
# 4. 检查ARP表
ip neigh show dev eth0
# 5. 检查防火墙
iptables -L -n -v
```
### 2. 快速诊断命令
```bash
# 一键诊断
ip a && ip r && ip neigh
# 保存网络配置快照
ip addr show > /tmp/network_snapshot.txt
ip route show >> /tmp/network_snapshot.txt
ip neigh show >> /tmp/network_snapshot.txt
```
---
## 十二、最佳实践
1. **使用完整语法**:虽然简写方便,但脚本中建议使用完整命令
2. **配置持久化**:`ip` 命令配置是临时的,重启后会丢失
3. **参数验证**:重要操作前先验证参数
4. **批量操作**:多个接口配置使用循环
5. **错误处理**:脚本中添加错误检查
6. **权限管理**:大部分 `ip` 命令需要 root 权限
通过掌握 `ip` 系列命令,你可以高效地管理 Linux 网络配置,进行网络故障排查和性能优化。