[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"menus-public":3,"blog-10053":83,"blog-10053-comments":92},[4,15,43,66],{"id":5,"title":6,"link":7,"icon":8,"target":9,"sort_order":10,"is_active":11,"parent_id":12,"created_at":13,"children":14},10001,"首页","\u002F","","_blank",0,true,null,"2026-04-04T12:03:45",[],{"id":16,"title":17,"link":18,"icon":8,"target":19,"sort_order":20,"is_active":11,"parent_id":12,"created_at":21,"children":22},10038,"墨言博客","\u002Fblog","_self",1,"2026-10-30T23:08:14",[23,29,36],{"id":24,"title":25,"link":26,"icon":8,"target":19,"sort_order":20,"is_active":11,"parent_id":16,"created_at":27,"children":28},10039,"技术前沿","\u002Fblog?category=10001","2026-10-30T23:10:46",[],{"id":30,"title":31,"link":32,"icon":8,"target":19,"sort_order":33,"is_active":11,"parent_id":16,"created_at":34,"children":35},10042,"生活随笔","\u002Fblog?category=20007",2,"2026-10-30T23:55:19",[],{"id":37,"title":38,"link":39,"icon":8,"target":19,"sort_order":40,"is_active":11,"parent_id":16,"created_at":41,"children":42},10043,"旅游日记","\u002Fblog?category=20006",3,"2026-10-31T00:00:11",[],{"id":33,"title":44,"link":45,"icon":8,"target":19,"sort_order":33,"is_active":11,"parent_id":12,"created_at":46,"children":47},"新闻中心","\u002Fnews","2026-10-24T19:09:40",[48,54,60],{"id":49,"title":50,"link":51,"icon":8,"target":19,"sort_order":20,"is_active":11,"parent_id":33,"created_at":52,"children":53},10046,"产品动态","\u002Fnews?category=20010","2026-10-31T00:17:38",[],{"id":55,"title":56,"link":57,"icon":8,"target":19,"sort_order":33,"is_active":11,"parent_id":33,"created_at":58,"children":59},10047,"新闻热点","\u002Fnews?category=20009","2026-10-31T00:17:55",[],{"id":61,"title":62,"link":63,"icon":8,"target":19,"sort_order":40,"is_active":11,"parent_id":33,"created_at":64,"children":65},10045,"公司动态","\u002Fnews?category=20008","2026-10-31T00:17:11",[],{"id":67,"title":68,"link":69,"icon":8,"target":19,"sort_order":67,"is_active":11,"parent_id":12,"created_at":46,"children":70},4,"精彩视频","\u002Fvideo",[71,77],{"id":72,"title":73,"link":74,"icon":8,"target":19,"sort_order":10,"is_active":11,"parent_id":67,"created_at":75,"children":76},13,"活动记录","\u002Fvideo?category=6","2026-10-25T21:04:39",[],{"id":78,"title":79,"link":80,"icon":8,"target":19,"sort_order":20,"is_active":11,"parent_id":67,"created_at":81,"children":82},11,"产品展示","\u002Fvideo?category=5","2026-10-25T21:02:59",[],{"id":84,"category_id":5,"title":85,"author":8,"summary":86,"cover":87,"content":88,"tags":8,"seo_title":8,"seo_description":8,"seo_keywords":8,"og_image":8,"views":89,"comment_count":10,"is_top":90,"is_published":11,"created_at":91},10053,"网络IP的使用","ip 命令来自 iproute2 套件，是现代 Linux 网络管理的标准工具。在 Ubuntu 18.04 及之后的版本中，ifconfig 所属的 net-tools 默认已不再安装，ip 命令不仅覆盖了旧工具的全部功能，还支持策略路由、网络命名空间、隧道、VLAN 等现代特性。","\u002Fstatic\u002Fuploads\u002Fblogs\u002F1792651544352-416407629.jpg","`ip` 命令来自 `iproute2` 套件，是现代 Linux 网络管理的标准工具。在 Ubuntu 18.04 及之后的版本中，`ifconfig` 所属的 `net-tools` 默认已不再安装，`ip` 命令不仅覆盖了旧工具的全部功能，还支持策略路由、网络命名空间、隧道、VLAN 等现代特性。\n\n---\n\n## 一、基本语法与常用选项\n\n```bash\nip [ OPTIONS ] OBJECT { COMMAND | help }\n```\n\n`OBJECT` 是操作对象（如 `addr`、`link`、`route`），`COMMAND` 是具体动作（如 `show`、`add`、`del`、`set`）。直接运行 `ip OBJECT help` 可以查看该对象下的完整子命令。\n\n常用选项：\n\n| 选项 | 作用 |\n|---|---|\n| `-br` | 简洁表格输出，适合快速查看和脚本处理 |\n| `-s` \u002F `-s -s` | 显示统计信息（`-s -s` 包含错误计数） |\n| `-4` \u002F `-6` | 仅显示 IPv4 \u002F IPv6 |\n| `-j` | JSON 格式输出，便于 `jq` 解析 |\n| `-d` | 显示详细信息（如 VLAN 的 id、协议、父接口） |\n| `-o` | 单行输出（oneline） |\n\n---\n\n## 二、`ip addr`：IP 地址管理\n\n### 查看地址\n\n```bash\nip addr show              # 查看所有接口的地址\nip addr show eth0         # 查看指定接口\nip -br addr show          # 简洁表格：接口名、状态、IP\nip -4 addr show           # 仅 IPv4\nip -6 addr show           # 仅 IPv6\n```\n\n`ip -br addr` 的输出形如：\n\n```text\nlo               UNKNOWN        127.0.0.1\u002F8 ::1\u002F128\neth0             UP             192.168.1.100\u002F24 fe80::5054:ff:feab:cdef\u002F64\n```\n\n### 添加 \u002F 删除 IP 地址（临时）\n\n```bash\nsudo ip addr add 192.168.1.100\u002F24 dev eth0      # 添加\nsudo ip addr del 192.168.1.100\u002F24 dev eth0      # 删除\n```\n\n`ip` 原生支持在同一接口上添加多个 IP 地址，直接再次执行 `add` 并指定不同地址即可，无需像 `ifconfig` 那样借助 `eth0:0` 之类的别名。\n\n### 与 `ifconfig` 的对应关系\n\n| `ifconfig` | `ip` 等价命令 |\n|---|---|\n| `ifconfig -a` | `ip addr show` |\n| `ifconfig eth0` | `ip addr show eth0` |\n| `ifconfig eth0 192.168.1.50 netmask 255.255.255.0` | `ip addr add 192.168.1.50\u002F24 dev eth0` |\n| `ifconfig eth0 0.0.0.0` | `ip addr del 192.168.1.50\u002F24 dev eth0` |\n\n---\n\n## 三、`ip link`：链路层与接口管理\n\n### 启用 \u002F 禁用接口\n\n```bash\nsudo ip link set eth0 up\nsudo ip link set eth0 down\n```\n\n### 修改 MTU\n\n```bash\nip link show eth0                  # 查看当前 MTU\nsudo ip link set eth0 mtu 9000\n```\n\n### 修改 MAC 地址\n\n需要先 `down`，改完再 `up`：\n\n```bash\nsudo ip link set eth0 down\nsudo ip link set eth0 address 00:11:22:33:44:55\nsudo ip link set eth0 up\n```\n\n### 查看统计信息\n\n```bash\nip -s link show eth0\nip -s -s link show eth0      # 额外显示错误计数\n```\n\n如果 `RX errors` 或 `TX dropped` 持续增长，通常指向物理链路或驱动问题。\n\n### 混杂模式\n\n```bash\nsudo ip link set eth0 promisc on\nsudo ip link set eth0 promisc off\n```\n\n---\n\n## 四、`ip route`：路由管理\n\n### 查看路由表\n\n```bash\nip route show                  # 主路由表\nip route show table main       # 指定表\nip route show 192.168.0.0\u002F16   # 指定目标网段\n```\n\n### 添加路由\n\n```bash\n# 静态路由：到达 192.168.2.0\u002F24 走网关 10.0.0.1\nsudo ip route add 192.168.2.0\u002F24 via 10.0.0.1\n\n# 指定出口接口\nsudo ip route add 192.168.2.0\u002F24 dev eth0\n\n# 同时指定网关和接口\nsudo ip route add 192.168.2.0\u002F24 via 10.0.0.1 dev eth0\n\n# 默认路由\nsudo ip route add default via 192.168.1.1\nsudo ip route add default via 192.168.1.1 dev eth0\n\n# 单主机路由（\u002F32）\nsudo ip route add 10.10.10.1\u002F32 via 192.168.1.1\n```\n\n### 删除与替换\n\n```bash\nsudo ip route del 192.168.2.0\u002F24\nsudo ip route del default via 192.168.1.1\nsudo ip route replace 192.168.2.0\u002F24 via 10.0.0.2   # 不先删除，直接替换\n```\n\n### `ip route get`：排障利器\n\n`ip route get` 不修改任何配置，而是**模拟内核的路由决策**，告诉你访问某个目标时实际会使用哪个出口、哪个网关、哪个源 IP：\n\n```bash\nip route get 8.8.8.8\nip route get 192.168.2.100\n```\n\n在多网卡、多路由表的场景中，这是定位“为什么走了错误的线路”最直接的方法。\n\n---\n\n## 五、`ip neigh`：邻居表 \u002F ARP 管理\n\n`ip neigh` 管理的是协议地址与链路层地址之间的绑定关系（IPv4 场景即 ARP 表）。\n\n### 查看与添加\n\n```bash\nip neigh show                                    # 查看邻居表\nsudo ip neigh add 192.168.1.1 lladdr 00:11:22:33:44:55 dev eth0\n```\n\n### 删除与清空\n\n```bash\nsudo ip neigh del 192.168.1.1 dev eth0\nsudo ip neigh flush dev eth0                    # 清空 eth0 的邻居缓存\n```\n\n邻居条目的 `nud` 状态包括 `permanent`、`reachable`、`stale`、`failed`、`incomplete` 等，`ip neigh show` 输出中的状态字段可以直接用于判断 ARP 解析是否正常。\n\n---\n\n## 六、`ip rule` 与自定义路由表：策略路由\n\n默认路由只依据**目标地址**做决策。策略路由可以在此基础上加入**源地址**等条件，实现多 ISP、多网卡场景下的分流。\n\n### 创建自定义路由表\n\n在 `\u002Fetc\u002Fiproute2\u002Frt_tables` 中添加名称映射，便于记忆：\n\n```bash\nsudo tee -a \u002Fetc\u002Fiproute2\u002Frt_tables \u003C\u003C 'EOF'\n100 isp-a\n200 isp-b\nEOF\n```\n\n路由表编号可用范围为 1–252，253（default）、254（main）、255（local）为系统保留。\n\n### 填充表路由与添加规则\n\n以双 ISP 场景为例：\n\n```bash\n# 为 isp-a 表设置默认路由，走 ISP-A 网关\nsudo ip route add default via 203.0.113.1 dev eth0 table isp-a\nsudo ip route add 203.0.113.0\u002F24 dev eth0 table isp-a\n\n# 为 isp-b 表设置默认路由，走 ISP-B 网关\nsudo ip route add default via 198.51.100.1 dev eth1 table isp-b\nsudo ip route add 198.51.100.0\u002F24 dev eth1 table isp-b\n\n# 添加规则：来自 ISP-A 地址的流量查 isp-a 表，来自 ISP-B 的查 isp-b 表\nsudo ip rule add from 203.0.113.10 table isp-a priority 100\nsudo ip rule add from 198.51.100.10 table isp-b priority 200\n```\n\n`priority` 数值越小越先匹配。验证规则和实际路由走向：\n\n```bash\nip rule show\nip route get 8.8.8.8 from 203.0.113.10\n```\n\n输出会明确显示使用了哪张表、哪个网关、哪个出口接口。\n\n---\n\n## 七、`ip tunnel` 与 `ip link`（虚拟接口）：隧道、VLAN、网桥\n\n### GRE \u002F VXLAN 隧道\n\n```bash\n# GRE\nsudo ip tunnel add gre1 mode gre remote 203.0.113.1 local 192.168.1.100 ttl 255\nsudo ip link set gre1 up\nsudo ip addr add 10.0.0.1\u002F30 dev gre1\n\n# VXLAN\nsudo ip link add vxlan0 type vxlan id 42 dstport 4789 remote 203.0.113.2\nsudo ip link set vxlan0 up\n```\n\n### VLAN 子接口\n\n```bash\nsudo ip link add link eth0 name eth0.100 type vlan id 100\nsudo ip link set eth0.100 up\nsudo ip addr add 192.168.100.1\u002F24 dev eth0.100\n```\n\n### 网桥\n\n```bash\nsudo ip link add name br0 type bridge\nsudo ip link set eth0 master br0\nsudo ip link set br0 up\n```\n\n创建后可以用 `ip -d link show eth0.100` 确认 VLAN 的 `id`、`protocol` 和父接口是否正确。\n\n---\n\n## 八、`ip netns`：网络命名空间\n\n网络命名空间是 `ip` 超越 `ifconfig` 的核心能力之一，也是理解 Docker \u002F Kubernetes 网络模型的基础。\n\n```bash\nsudo ip netns add testns              # 创建\nip netns list                          # 列出\nsudo ip netns exec testns ip addr     # 在命名空间内执行命令\nsudo ip netns exec testns ip link set lo up\nsudo ip netns del testns              # 删除\n```\n\n创建命名空间后，回环接口默认是 `DOWN` 状态，通常需要先 `ip netns exec \u003Cns> ip link set lo up` 才能进行本机连通性测试。\n\n---\n\n## 九、实用技巧\n\n**实时监控**：`ip monitor link` 监控链路状态变化，`ip monitor route` 监控路由变化。\n\n**批量执行**：将多条 `ip` 子命令写入文件，用 `ip -batch` 一次性执行：\n\n```bash\nip -batch -force \u003C config.txt\n```\n\n**JSON 输出配合 `jq`**：适合脚本自动化，例如列出所有 UP 状态接口的名称：\n\n```bash\nip -j link show | jq '.[] | select(.operstate == \"UP\") | .ifname'\n```\n\n---\n\n## 十、关键注意事项：持久化\n\n**所有通过 `ip` 命令做出的修改都是临时的，重启后丢失。** 持久化需要借助 Ubuntu 的配置管理层：\n\n- **Netplan（Ubuntu 默认）**：编辑 `\u002Fetc\u002Fnetplan\u002F*.yaml`，用 `netplan apply` 生效。\n- **NetworkManager**：使用 `nmcli connection modify` 配置网关和路由。\n- **systemd-networkd**：在 `.network` 文件中使用 `[Route]` 段。\n\n不要将 `ip` 命令直接写入启动脚本作为“持久化方案”的唯一依赖，优先使用发行版推荐的配置工具。\n\n---\n\n## 附：网络故障快速排查顺序\n\n一条实用的排查链：\n\n```bash\nip -br addr            # 1. IP 配了吗？接口是 UP 吗？\nip route show          # 2. 默认网关和必要路由存在吗？\nip route get 8.8.8.8   # 3. 实际路由决策是否符合预期？\nip neigh show          # 4. ARP \u002F 邻居解析是否正常？\n```",14,false,"2026-10-22T14:47:08",{"list":93,"total":10,"page":20,"size":94},[],30]