ChinaUnix首页 > 精华文章 > Solaris > 正文

[精华] route add 如何增加永久路由


http://www.chinaunix.net 作者:dragonzh  发表于:2008-12-22 12:53:19
发表评论】 【查看原文】 【Solaris讨论区】【关闭

在机器重起后依然维持原来的路由表保持不变



 ipfreak 回复于:2002-09-19 15:51:00

mkae a file start with S and put under rc2.d or rc3.d.

put "route add whatever shit" in this file



 liluo 回复于:2002-09-19 16:49:31

如果是default路由,那么就在/etc/defaultrouter里面写入你的路由器的IP地址
如果是具体到某个网段的路由,偶的方便方法是在/etc/inittab的最后写入加路由的命令,具体规则查/etc/inittab的语法。
不在运行级别里面写是因为太麻烦


 南非蜘蛛 回复于:2002-09-19 17:28:46

引用:下面引用由[u]dragonzh[/u]在 2002/09/19 03:44pm 发表的内容:
在机器重起后依然维持原来的路由表保持不变


route add加不了永久路径,从起就会失效,需要写到文件里


 rimrock 回复于:2002-09-19 17:35:41

at /etc/rc3.d/S90route file add:
route -nv add -net netip routeip

reboot


 南非蜘蛛 回复于:2002-09-19 17:39:14

引用:下面引用由[u]rimrock[/u]在 2002/09/19 05:35pm 发表的内容:
at /etc/rc3.d/S90route file add:
route -nv add -net netip routeip
reboot


什么意思?/etc/rc3.d/S90route有这个文件吗?


 rimrock 回复于:2002-09-19 17:41:37

you must create file:  /etc/rc3.d/S90route   by yourself
in my SUN server, it work well.
note: this file can run only in level 3.


 sunmarmot 回复于:2002-09-19 17:57:02

静态路由

最长用的静态路由是一台主机到一台本地路由器,通常在 /etc/defaultrouter中定义,用于添加网关

#vi /etc/defaultrouter
192.168.8.1
服务器的的静态路由也可以通过使用in.routed在文件/etc/gateways中定义,当使用静态路由时,在内核中的路由表在系统启动时被定义,并

且通常不会改变,除非使用route或ifconfig 命令修改。当本地网络通过单一的网关与Internet的其余部分相连时静态路由是最合适的选择。

 静态路由可用route或ifconfig 手工加入,生成后重起不变,如果使用动态路由则在重启后经常会导致路由表的改变,应为守护进程负责处理网

络配置和可用路由的变化。

内核路由表

有三种路由
1.主机路由从一台主机映射一条到本地网络上的的其他主机上
command format:
route add -host destination_ip local_ip -interface interface
eg:
我们想为本地主机接口hme0(204.12.17.1)和另一台在相邻才C类网络上的主机(204.12.16.100)之间增加一条路由
#route add -host 204.12.16.100 204.12.17.1 -interface hme0
2.网络路由允许数据包从本地主机传输到在本地网络的其他主机上
增加一个到另一个网络的的直接路由
command format:
route add -net  destination_network_ip local_ip -netmask mask
eg:
如果我们要想为C类网掩码在本地主机(204.12.17.1)和我们上面指出的网络之间增加一条路由(204.12.16.0网络)我们可以使用如下的命令:
#route add -net 204.12.16.0 204.12.17.1 -netmask 255.255.255.0
3.缺省路由将寻找一条路由的任务传送到一台路由器。RIP 和RDISC守护进程都可以使用缺省路由。
command format:
route add default hostname -interface interface
eg:增加一条到本地路由器(204.54.56.1)的缺省路由,通常用在添加网关并马上生效
#route add default 204.54.56.1 -interface hme1

动态路由

     Solaris 支持路由信息协议(RIP),路由发现协议(RDISC)两种TCP/IP网络标准路由协议 ,RIP是通过in.routed守护进程来实现的,通常

在多用户启动时配置启动。路由守护进程总是建立一个可到达每个网络的路由表。
     主机使用RDISC守护进程(in.rdisc)来搜集来自路由器的可用路由信息。in.rdisc通常为响应请求的每一个路由器建立一个缺省路由。
注意:如果要使动态路由启用,前提必须是文件/etc/defaultrouter为空。
    
    in.routed进程通过启动/etc/rc2.d/S69inet中的一行,在使用如下命令的多用户启动时启动。
    #/usr/sbin/in.routed -q  初始化后,路由守护进程检查文件/etc/gateways中指定的网关.
    #/usr/sbin/in.routed -S
    也可以使用-S参数处始化in.routed来实现节省模式,这种方式可以节省并更新的活动路由的数目。但这种方式可能受到网络中路由器故障的困绕。
    
如果你要想每次重启动后要用,你就把route add.....写到里面去

# cat /etc/rc2.d/S69inet
#!/sbin/sh
#
# Copyright (c) 1995, 1997-1999 by Sun Microsystems, Inc.
# All rights reserved.
#
#pragma ident   "@(#)inetinit   1.44    99/10/04 SMI"

#
# This is the second phase of TCP/IP configuration.  The first part,
# run in the "/etc/rcS.d/S30rootusr.sh" script, does all configuration
# necessary to mount the "/usr" filesystem via NFS.  This includes configuring
# the interfaces and setting the machine's hostname.  The second part,
# run in this script, does all configuration that can be done before
# NIS or NIS+ is started.  This includes configuring IP routing,
# setting the NIS domainname and setting any tunable parameters.  The
# third part, run in a subsequent startup script, does all
# configuration that may be dependent on NIS/NIS+ maps.  This includes
# a final re-configuration of the interfaces and starting all internet
# services.
#

case "$1" in
'start')
        &#59;&#59; # Fall through -- rest of script is the initialization code

'stop')
        #
        # If we were routing dynamically, we will note this with
        # the .dynamic_routing file, so that we can leave the routes
        # in place without thinking they're static route entries
        # when we come back into states 2 or 3.
        #
        if /usr/bin/pgrep -x -u 0 'in.routed|in.rdisc' >;/dev/null 2>;&1&#59; then
                /usr/bin/pkill -x -u 0 'in.routed|in.rdisc'
                >; /etc/.dynamic_routing
        fi
        exit 0
        &#59;&#59;

*)
        echo "Usage: $0 { start | stop }"
        exit 1
        &#59;&#59;
esac

#
# Initialize IPsec only if ipsecinit.conf exists.  Otherwise, save the
# kernel memory that'll be chomped if IPsec is loaded.
#
if [ -f /etc/inet/ipsecinit.conf ] &#59; then
        /usr/sbin/ipsecconf -qa /etc/inet/ipsecinit.conf
fi

#
# Set the RFC 1948 entropy, regardless of if I'm using it or not.  If present,
# use the encrypted root password as a source of entropy.  Otherwise,
# just use the pre-set (and hopefully difficult to guess) entropy that
# tcp used when it loaded.
#
encr=`/usr/bin/awk -F: '/^root:/ {print $2}' /etc/shadow`
[ -z "$encr" ] || /usr/sbin/ndd -set /dev/tcp tcp_1948_phrase $encr
unset encr

#
# Set TCP ISS generation.  By default the ISS generation is
# time + random()-delta.  This might not be strong enough for some users.
# See /etc/default/inetinit for settings and further info on TCP_STRONG_ISS.
# If not set, use TCP's internal default setting.
#

# Get value of TCP_STRONG_ISS
[ -f /etc/default/inetinit ] && . /etc/default/inetinit
if [ $TCP_STRONG_ISS ]&#59; then
        /usr/sbin/ndd -set /dev/tcp tcp_strong_iss $TCP_STRONG_ISS
fi

#
# Configure default IPv4 routers using the local "/etc/defaultrouter"
# configuration file.  The file can contain the hostnames or IP
# addresses of one or more default routers.  If hostnames are used,
# each hostname must also be listed in the local "/etc/hosts" file
# because NIS and NIS+ are not running at the time that this script is
# run.  Each router name or address is listed on a single line by
# itself in the file.  Anything else on that line after the router's
# name or address is ignored.  Lines that begin with "#" are
# considered comments and ignored.
#
# The default routes listed in the "/etc/defaultrouter" file will
# replace those added by the kernel during diskless booting.  An
# empty "/etc/defaultrouter" file will cause the default route
# added by the kernel to be deleted.
#
# Note that the default router file is ignored if we received routes
# from a DHCP server.  Our policy is to always trust DHCP over local
# administration.
#
if [ "$_INIT_NET_STRATEGY" = "dhcp" ] && [ -n "`/sbin/dhcpinfo Router`" ]&#59; then
        defrouters=`/sbin/dhcpinfo Router`
elif [ -f /etc/defaultrouter ]&#59; then
        defrouters=`/usr/bin/grep -v \^\# /etc/defaultrouter | \
            /usr/bin/awk '{print $1}'`
        if [ -n "$defrouters" ]&#59; then
                #
                # We want the default router(s) listed in /etc/defaultrouter
                # to replace the one added from the BOOTPARAMS WHOAMI response
                # but we must avoid flushing the last route between the running
                # system and its /usr file system.
                #

                # First, remember the original route.
                shift $#
                set -- `/usr/bin/netstat -rn -f inet | /usr/bin/grep '^default'`
                route_IP="$2"

                #
                # Next, add those from /etc/defaultrouter.  While doing this,
                # if one of the routes we add is for the route previously
                # added as a result of the BOOTPARAMS response, we will see
                # a message of the form:
                #       "add net default: gateway a.b.c.d: entry exists"
                #
                do_delete=yes
                for router in $defrouters&#59; do
                        set -- `/usr/sbin/route -n add default $router`
                        [ $? -eq 0 -a "x$5" = "x$route_IP:" ] && do_delete=no
                done

                #
                # Finally, delete the original default route unless it was
                # also listed in the defaultrouter file.
                #
                if [ -n "$route_IP" -a $do_delete = yes ]&#59; then
                        /usr/sbin/route -n delete default $route_IP >; /dev/null
                fi
        else
                /usr/sbin/route -fn >; /dev/null
        fi
else
        defrouters=
fi

#
# Set NIS domainname if locally configured.
#
if [ -f /etc/defaultdomain ]&#59; then
        /usr/bin/domainname `cat /etc/defaultdomain`
        echo "NIS domainname is `/usr/bin/domainname`"
fi

#
# Run in.routed/router discovery if we don't already have a default
# route installed or if we had been running them in a previous
# multiuser state.
#
if [ -z "$defrouters" -a ! -f /etc/.dynamic_routing ]&#59; then
        #
        # No default routes were setup by "route" command above - check the
        # kernel routing table for any other default routes.
        #
        /usr/bin/netstat -rn -f inet | \
            /usr/bin/grep default >;/dev/null 2>;&1 && defrouters=yes
fi
[ -f /etc/.dynamic_routing ] && /usr/bin/rm -f /etc/.dynamic_routing

if [ -z "$defrouters" ]&#59; then
        #
        # Determine how many active interfaces there are and how many pt-pt
        # interfaces. Act as an IPv4 router if there are more than 2 interfaces
        # (including the loopback interface) or one or more point-point
        # interface. Also act as an IPv4 router if /etc/gateways exists.
        #
        # Do NOT act as an IPv4 router if /etc/notrouter exists.
        # Do NOT act as an IPv4 router if DHCP was used to configure
        # interface(s)
        #
        inetifaddrs="`/usr/sbin/ifconfig -a4u | /usr/bin/grep inet`"
        numifs=`echo "$inetifaddrs" | /usr/bin/wc -l`
        numptptifs=`echo "$inetifaddrs" | /usr/bin/egrep -c -e '-->;'`

        if [ "$_INIT_NET_STRATEGY" = "dhcp" ]&#59; then
                numdhcp=`/usr/sbin/ifconfig -a4 | /usr/bin/grep -c DHCP`
        else
                numdhcp=0
        fi

        if [ ! -f /etc/notrouter -a $numdhcp -eq 0 -a \
            \( $numifs -gt 2 -o $numptptifs -gt 0 -o -f /etc/gateways \) ]&#59; then
                #
                # Machine is an IPv4 router: turn on ip_forwarding, run
                # in.routed, and advertise ourselves as a router using router
                # discovery.
                #
                echo 'Machine is an IPv4 router.'
                /usr/sbin/ndd -set /dev/ip ip_forwarding 1

                [ -f /usr/sbin/in.routed ] && /usr/sbin/in.routed -s
                [ -f /usr/sbin/in.rdisc ] && /usr/sbin/in.rdisc -r

        else
                #
                # Machine is an IPv4 host: if router discovery finds a router
                # then we rely on router discovery. If there are no routers
                # advertising themselves through router discovery
                # run in.routed in quiet mode.  In both cases, turn off
                # ip_forwarding.
                #
                /usr/sbin/ndd -set /dev/ip ip_forwarding 0

                if [ -f /usr/sbin/in.rdisc ] && /usr/sbin/in.rdisc -s&#59; then
                        echo 'Starting IPv4 router discovery.'
                elif [  -f /usr/sbin/in.routed ]&#59; then
                        /usr/sbin/in.routed -q
                        echo 'Starting IPv4 routing daemon.'
                fi
        fi
else
        /usr/sbin/ndd -set /dev/ip ip_forwarding 0
fi

#
# Run IPv6 if more than lo0 plumbed for IPv6.
#
/usr/sbin/ifconfig -a6u >;/tmp/ifconfig.$$
numv6ifs=`/usr/bin/grep -c inet6 /tmp/ifconfig.$$`
if  [ $numv6ifs -gt 1 ]&#59; then
        #
        # Run IPv6 routing only if /etc/inet/ndpd.conf exists, otherwise just
        # run the host portion.
        #
        if [ -f /etc/inet/ndpd.conf ]&#59; then
                #
                # Machine is an IPv6 router: turn on ip6_forwarding,
                # ip6_send_redirects and ip6_ignore_redirect, run in.ripngd, and
                # advertise ourselves as a router using neighbor discovery.
                #
                echo "Machine is an IPv6 router."
                /usr/sbin/ndd -set /dev/ip ip6_forwarding 1
                /usr/sbin/ndd -set /dev/ip ip6_send_redirects 1
                /usr/sbin/ndd -set /dev/ip ip6_ignore_redirect 1
                if [ -f /usr/lib/inet/in.ndpd ]&#59; then
                        /usr/lib/inet/in.ndpd
                fi
                if [ -f /usr/lib/inet/in.ripngd ]&#59; then
                        /usr/lib/inet/in.ripngd -s
                fi
        else
                # Machine is an IPv6 host - just run neighbor discovery.
                /usr/sbin/ndd -set /dev/ip ip6_forwarding 0
                /usr/sbin/ndd -set /dev/ip ip6_send_redirects 0
                /usr/sbin/ndd -set /dev/ip ip6_ignore_redirect 0
                if [ -f /usr/lib/inet/in.ndpd ] &#59; then
                        echo "Starting IPv6 neighbor discovery."
                        /usr/lib/inet/in.ndpd
                fi
        fi
        #
        # Add a static route for multicast packets out of a link-local
        # interface, although would like to specify multicast interface using
        # an interface name!
        #
        set -- `/usr/bin/awk '
                /inet6 fe80:/ {
                        print substr($2, 1, index($2, "/") - 1)
                }' /tmp/ifconfig.$$`

        if [ -n "$1" ]&#59; then
                echo "Setting default IPv6 interface for multicast: \c"
                /usr/sbin/route -n add -interface -inet6 "ff00::/8" "$1"
        fi
else
        /usr/sbin/ndd -set /dev/ip ip6_forwarding 0
        /usr/sbin/ndd -set /dev/ip ip6_send_redirects 0
        /usr/sbin/ndd -set /dev/ip ip6_ignore_redirect 0
fi
/usr/bin/rm -f /tmp/ifconfig.$$

#
# Configure tunnels which was deferred by /etc/rcS.d/S30network.sh since
# it depends on the tunnel endpoints being reachable i.e. routing must be
# running.
#
# WARNING: you may wish to turn OFF forwarding if you haven't already, because
# of various possible security vulnerabilities when configuring tunnels for
# Virtual Private Network (VPN) construction.
#
# Also, if names are used in the /etc/hostname.ip.tun* file, those names
# have to be in either DNS (and DNS is used) or in /etc/hosts, because this
# file is executed before NIS or NIS+ is started.
#

#
# IPv4 tunnels
# The second component of the name must be either "ip" or "ip6".
#
interface_names="`/usr/bin/ls /etc/hostname.ip*.*[0-9] 2>;/dev/null | \
    /usr/bin/grep '/etc/hostname\.ip6\{0,1\}\.'`"
if [ -n "$interface_names" ]&#59; then
        (
                echo "configuring IPv4 tunnels:\c"
                # Extract the part after the first '.'
                set -- `for intr in $interface_names&#59; do \
                    /usr/bin/expr //$intr : '[^.]*\.\(.*\)$'&#59; done`
                while [ $# -ge 1 ]&#59; do
                        # Skip empty files
                        if [ ! -s /etc/hostname\.$1 ]&#59; then
                                shift
                                continue
                        fi
                        /usr/sbin/ifconfig $1 plumb 2>;&1 >;/dev/null
                        while read ifcmds&#59; do
                                if [ -n "$ifcmds" ]&#59; then
                                        /usr/sbin/ifconfig $1 inet $ifcmds \
                                            2>;&1 >;/dev/null
                                fi
                        done </etc/hostname\.$1
                        echo &quot; $1\c&quot;
                        shift
                done
                echo &quot;.&quot;
        )
fi

#
# IPv6 Tunnels
# The second component of the name must be either &quot;ip&quot; or &quot;ip6&quot;.
#
interface_names=&quot;`/usr/bin/ls /etc/hostname6.ip*.*[0-9] 2>;/dev/null | \
    /usr/bin/grep '/etc/hostname6\.ip6\{0,1\}\.'`&quot;
if [ -n &quot;$interface_names&quot; ]&#59; then
        (
                echo &quot;configuring IPv6 tunnels:\c&quot;
                # Extract the part after the first '.'
                set -- `for intr in $interface_names&#59; do \
                    /usr/bin/expr //$intr : '[^.]*\.\(.*\)$'&#59; done`
                while [ $# -ge 1 ]&#59; do
                        # Skip empty files
                        if [ ! -s /etc/hostname6\.$1 ]&#59; then
                                shift
                                continue
                        fi
                        /usr/sbin/ifconfig $1 inet6 plumb 2>;&amp;1 >;/dev/null
                        while read ifcmds&#59; do
                                if [ -n &quot;$ifcmds&quot; ]&#59; then
                                        /usr/sbin/ifconfig $1 inet6 $ifcmds \
                                            2>;&amp;1 >;/dev/null
                                fi
                        done </etc/hostname6\.$1
                        echo &quot; $1\c&quot;
                        shift
                done
                echo &quot;.&quot;
        )
fi


 templine 回复于:2003-01-09 17:00:57

(1)首先你需要创建一个文件,譬如叫/xyz,记得一定要让它可以执行(#chmod +X /xyz)它包含如下内容
route add 目标网段(或者目标地址) 缺省网关
例如
route add 192.168.1.0 192.168.0.100
看你需要添加可以多条类似记录
(2)接着你可以按如下操作就可以了,重新启动机器就成了
# cp /xyz /etc/init.d
# cd /etc/init.d
# ln xyz /etc/rc2.d/S100xyz
# ln xyz /etc/rc0.d/K100xyz
# ls /etc/init.d /etc/rc2.d /etc/rc0.d(检查文件在否)
如果在执行#rm /xyz(删除那个已经临时文件,节省磁盘空间:) )

我在生产环境如是测试过,工作很好,该思路来自管理员手册,看你自己的运气了!:)



 future00 回复于:2003-01-11 01:02:45

  sunmarmot的介绍的比较详细,一般在/etc下vi defaultrouter就可以了 


 laoxia 回复于:2003-01-11 01:14:56

缺省的,放/etc/defaultrouter 里
非缺省的, 放/etc/gateways 里


 苍之狼 回复于:2006-12-06 09:58:57

好棒的帖子!!各位大侠解释的很详细哈~~受教了!!
另外小弟总结一下,不对的地方请指正:
要想在机器重起后依然维持原来的路由表保持不变 
(一)如果是default路由,那么就在/etc/defaultrouter里面写入你的路由器的IP地址
     (注:/etc/defaultrouter里面只能有一条路由!!!)
(二)如果不是default路由,那么可以
(1)cat /etc/rc3.d/S90route 并在该文件中添加route -nv add -net netip routeip这样每次机器
      reboot的时候都会自动执行一次route -nv add -net netip routeip添加路由的操作。
(2)首先/etc/init.d下面编辑文件 routefile(名称可以自己起),然后在vi routefile里面添加上
     route add -net destination next-hop 
       (如:route add 目的地址 -netmask 目的地址的子网掩码 默认网关即下一跳地址)
     然后再#chmod  +X /etc/init.d/routefile使这个文件为可执行文件。
     然后在/etc/init.d/下 
     #ln routefile /etc/rc2.d/S100route 
        #ln routefile /etc/rc0.d/K100route
        #ls /etc/init.d /etc/rc2.d /etc/rc0.d(检查文件在否)
        令系统启动以后也启动该路由。

不知道总结的是否全面??哪位大侠还有好的方法也请赐教哈~~我继续总结!
上述3个我方法我在实验机上都做过测试了。

[ 本帖最后由 苍之狼 于 2006-12-7 09:21 编辑 ]


 yuhuohu 回复于:2006-12-06 12:46:23

gateways里面添加路由就可以了吧


 hail 回复于:2006-12-15 12:17:43

加入倒/etc/init.d/inetinit文件末尾


 svspt 回复于:2006-12-17 20:23:46

怎么没人使用/etc/gateways呢?


 mengniu2000 回复于:2008-05-29 10:56:42

使用 route -p add .....
参数p 表示 永久固定的

Note that the manpage has various synopsis entries and the "route -p"
syntax is the last entry there. This option is available on Solaris 10
systems with patch 122539-06 or Solaris 10 KU patch 118833-36 or above.

route -p [-R root-dir] show

参考 [url=http://www.mydatabasesupport.com/forums/solaris/370606-setting-up-routes-solaris-10-a.html]http://www.mydatabasesupport.com/forums/solaris/370606-setting-up-routes-solaris-10-a.html


 yuhuohu 回复于:2008-05-29 11:00:09

引用:原帖由 mengniu2000 于 2008-5-29 10:56 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8462858&ptid=16587]
使用 route -p add .....
参数p 表示 永久固定的

Note that the manpage has various synopsis entries and the "route -p"
syntax is the last entry there. This option is available on Solaris 10
sy ... 




哈哈 solaris终于也有-p选项了,偶就一直郁闷了,在windows下用-p就可以解决了,怎么unix的世界里居然不采纳这么好的东西


 风之幻想 回复于:2008-05-29 11:01:33

典型的火星贴. 这么长时间的帖子也找出来了.


 bear 回复于:2008-05-29 13:31:31

晕啊,这个坟挖的够深的。
PS:精华属于下面回复的兄弟


 yqx1986 回复于:2008-11-18 11:30:49

-p 这个参数确实好用,我想问的是最低支持到哪个版本?


 sunshiene 回复于:2008-11-18 17:33:08

隐约的记得什么应用下好像gateway是需要禁用的


 Eric__Szm 回复于:2008-12-22 12:53:19

route add -p可以永久加路由信息




原文链接:http://bbs.chinaunix.net/viewthread.php?tid=16587
转载请注明作者名及原文出处