记录了 Linux、macOS 和 Windows 三个平台查看、添加、修改和删除静态路由的常用命令。
查看
Linux
CentOS 7 起 route 命令已被替代为 ip route,如果想用默认的 route 命令,需提前安装 net-tools。
# 使用 netstat 命令查看netstat -rn# ip route 命令查看ip route showip route list # 效果相同# 格式化结果ip route show | column -t# 查看指定 IP 或网址路由ip route get www.google.commacOS
# 查看路由表netstat -nr# 查看当前默认网关route -n get default# 查看指定 IP 或网址路由route -n get www.google.comWindows
# 查看路由表route print# 仅查看 ipv4route print -4# 查看指定 IP 段route print 10*添加
Linux
ip route add 10.1.1.42/32 via 10.1.1.1# 指定网卡ip route add 10.1.1.42/32 via 10.1.1.1 dev eth0macOS
route add 10.1.1.42/32 10.1.1.1# 指定网卡(不能同时指定网关)route add 10.1.1.42/32 -interface en0Windows
# 加参数 -p 表示永久生效route add 10.1.1.42/32 10.1.1.1# 指定网卡,网卡接口名在 route print 结果中查看route add 10.1.1.42/32 10.1.1.1 if 12修改
假设当前一条路由表为 10.1.1.42/32 10.1.1.1,现需修改网关为 10.1.1.24。
Linux
# change 可使用简称 chgip route change 10.1.1.42/32 via 10.1.1.24macOS
route change 10.1.11.42/32 10.1.1.24Windows
route change 10.1.11.42/32 10.1.1.24删除
删除时一般提供具体的 IP 或掩码即可,网关非必须。
Linux
ip route del 10.1.1.42/32macOS
route delete 10.1.1.42/32Windows
route delete 10.1.1.42/32