Solaris使用命令行添加静态路由

要添加非持久路由(临时,重启后失效),我们只需简单地使用不带 -p 选项的 route add 命令。
请注意,如果我们重新启动系统,这些路由将被刷新。
以下是为网络 10.10.10.0/24 添加路由 (192.168.1.1) 的 2 个示例

# route add 10.10.10.0 -netmask 255.255.255.0 192.168.1.1
# route add 10.10.10.0/24 192.168.1.1

要添加持久路由,我们需要在 route 命令中使用 -p 参数。
在以下示例中,网络 10.10.10.0/24 网络使用网关 192.168.1.1.

# route -p add 10.10.10.0 -netmask 255.255.255.0 192.168.1.1
# route -p add 10.10.10.0/24 192.168.1.1

添加持久默认路由 (192.168.1.1):

# route -p add default 192.168.1.1

要检索有关特定路线的信息:

# route get default
   route to: default
destination: default
       mask: default
    gateway: 192.168.1.1
  interface: e1000g0
      flags: [UP,GATEWAY,DONE,STATIC]
 recvpipe  sendpipe  ssthresh    rtt,ms rttvar,ms  hopcount      mtu     expire
       0         0         0         0         0         0      1500         0

要显示完整的路由表:

# netstat -nr
Routing Table: IPv4
  Destination           Gateway           Flags  Ref     Use     Interface
-------------------- -------------------- ----- ----- ---------- --------
192.168.1.0          192.168.1.30         U         1         23 e1000g0
224.0.0.0            192.168.1.30         U         1          0 e1000g0
224.0.0.0            192.168.1.30         UG        1          0
127.0.0.1            127.0.0.1            UH        4        121 lo0

各种标志(在标志列中):

U - The interface is up.
H - Host route. The destination is a system, not a network.
G - The delivery system is another system (an indirect route).
D - The entry was added dynamically by an ICMP redirect.

要查看系统中添加的持久路由:

# route -p show
persistent: route add 10.10.10.0/24 192.168.1.1

删除持久路由(持久):

# route -p delete 10.10.10.0/24 192.168.1.1

静态与动态路由

静态路由是通过脚本或者命令行使用 route 命令添加的。
动态路由是由一些路由守护进程添加的。
负责添加当前与 Solaris 捆绑/支持的动态路由的守护进程是 /usr/sbin/in.routed(路由信息协议 (RIP))和 /usr/sbin/in.rdisc(路由器网络发现协议)。

欢迎来到之路教程(on itroad-com)

使用 rc 脚本设置路由

上述命令行方法在solaris 8 和9 中不起作用,在solaris 10 的一些旧补丁版本中也不起作用。
为了克服这个问题,我们有另一种方法。
我们可以在 /etc/rc2.d 中创建一个 rc 脚本,命名为 S91routes。
在此脚本中添加 route add 命令:

# /usr/sbin/route add 10.10.10.0 -netmask 255.255.255.0 192.168.1.1

现在,下次系统启动时,此脚本将运行并添加脚本中指定的路由。

在 Solaris 中如何添加静态路由

其他例子

要更改路由,我们可以使用 route change 命令(将默认路由从 192.168.1.1 更改为 10.10.10.1):

# route change default 10.10.10.1

要持续监控路由表的任何更改和路由查找未命中,我们可以使用 route monitor 命令:

# route monitor
got message of size 124
RTM_DELETE: Delete Route: len 124, pid: 633, seq 1, errno 0, flags:[UP,GATEWAY,DONE,STATIC]
locks: inits:
sockaddrs: [DST,GATEWAY,NETMASK]
192.168.3.0 sys11ext 255.255.255.0

要刷新(删除)所有网关条目的路由表,请使用 route flush 命令。

# route flush
default              192.168.1.1          done
10.10.10.0           10.10.10.1           done

要在评估其余选项之前刷新路由表,请在使用其他选项之前使用刷新选项:

# route -f add 10.10.10.0/24 192.168.1.1

手动添加路由到多播地址范围 224-239 :

# route add 224.0/4 `uname -n`

要使用 /etc/defaultrouter 文件添加默认静态路由,请将默认路由器 IP 地址添加到文件 /etc/defaultrouter.conf 中。
配置了 /etc/defaultrouter 文件的系统不会执行 in.routed 守护进程。

# echo "192.168.1.1"  >> /etc/defaultrouter

我们还可以使用 /etc/gateways 文件添加静态路由。
如果 /etc/gateways 文件存在,则 in.routed 守护进程会在启动时读取该文件。
现在为网络 192.168.1.0 添加静态路由 (192.168.1.1),编辑 /etc/gateways 文件并添加以下条目

# cat /etc/gateways
net 192.168.1.0 gateway 192.168.1.1
日期:2020-09-17 00:15:03 来源:oir作者:oir