LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   How to add permanent static route (saved between reboot) (http://www.linuxquestions.org/questions/slackware-14/how-to-add-permanent-static-route-saved-between-reboot-4175455507/)

StreamThreader 03-25-2013 10:28 AM

How to add permanent static route (saved between reboot)
 
Hello people!
Please help, how add route, save it between reboots?
Only through rc.local?

BroX 03-25-2013 11:17 AM

No, modify /etc/rc.d/rc.inet1.conf instead.

try
Code:

man rc.inet1.conf

StreamThreader 03-25-2013 11:56 AM

Quote:

Originally Posted by BroX (Post 4918411)
No, modify /etc/rc.d/rc.inet1.conf instead.

try
Code:

man rc.inet1.conf

In this file I could set Getway (for default network in this interface)
in case with few routes, i need add this comman in rc.local file:
route add -net 192.168.1.0/24 gw 192.168.1.200
route add -net 192.168.2.0/24 gw 192.168.1.200
route add -net 192.168.5.0/24 gw 192.168.1.254
route add -net 192.168.50.0/24 gw 192.168.1.1
How to write this routes permanent in system, saved it between reboots?

GazL 03-25-2013 01:03 PM

Slackware doesn't have any official way of doing this. I don't like the idea of putting them in rc.local as it isn't really tied to the stop/start of rc.inet in any way.

I'd be inclined to make yourself a custom rc.inet1 script and include the route add/route del commainds within it at the appropriate place.

Here's a quick and dirty example I was using not long back
Code:

#! /bin/sh
#    /etc/rc.d/rc.inet1
#      Simpler replacement for Slackware's stock rc.inet1.

########################################################################

##  STOP  ##

if [ "$1" = "stop" -o "$1" = "restart" ]; then
  echo "Stopping network interfaces..."
  # /sbin/ifconfig lo down
  /sbin/ifconfig eth0 down

  /sbin/dhclient -x wlan0
  /usr/sbin/wpa_cli -i wlan0 terminate
fi

##  (RE)START  ##

if [ "$1" != "stop" ]; then
  echo "Starting network interfaces..."
  /sbin/ifconfig lo 127.0.0.1
  /sbin/ifconfig eth0 192.168.1.2

  /usr/sbin/wpa_supplicant -B -D wext -i wlan0 \
                            -c /etc/wpa_supplicant.conf \
    && /sbin/dhclient wlan0
fi

########################################################################

Obviously, if you do something like this you're pretty much going your own way, and netconfig and inet1.conf will no longer work,
but to my mind something like the above is far easier to understand and work with than the 400 lines of rc.inet1 and rc.inet1.conf.

StreamThreader 03-25-2013 07:09 PM

Ok, Thanks!

gnashley 03-26-2013 04:17 AM

"far easier to understand" - GazL, I have to agree there. I just recently started using wireless and never got the normal slack configuration to work. Network connections are really pretty easy, but because there is such a large variety of ways to connect it starts looking complicated when all the various options are presented in the same place. Other distros don't do much better in this regard. I finally 'went my own way' similar to what you have done.

GazL 03-26-2013 04:27 PM

I've always quite liked the OpenBSD approach of having /etc/hostname.$interface (though I'm not sure 'hostname' is the best filename prefix for it.
Anyway, just for fun I've knocked together a rc.inet1 that uses the same approach as OpenBSD.

/etc/rc.d/rc.inet1:
Code:

#!/bin/sh
#
#  Replacement /etc/rc.d/rc.inet1 inspired by the OpenBSD approach
#  to interface configuration using /etc/hostname.$interface files.

PATH="/usr/bin:/usr/sbin:/bin:/sbin"
unset IFS

########################################################################
list_interfaces()
{
    local line
    local sep=""

    tail +3 /proc/net/dev \
      | while IFS=' ' read line
        do 
            echo -n "${sep}${line%%:*}"
            sep=" "
        done
}

########################################################################
stop_interface()
{
    local interface="$1"

    ps ax | grep -q "[d]hclient $interface" \
      && dhclient -x "$interface" && return 0
   
    ifconfig "$interface" down
}

########################################################################
start_interface()
{
    local interface="$1"
    local config="/etc/hostname.$interface"
    local line

    ifconfig | grep -q "^$interface" \
        && echo "  $interface: skipped (already active)." \
        && return 1
       
    if [ -r "$config" ]; then
        while read line
        do
            case "$line" in
                dhcp) dhclient $interface
                      ;;
                inet*) ifconfig $interface $line
                      ;;
                !*)  ${line#!}
                      ;;
            esac
        done < "$config"
    else
        echo "  $interface: skipped (no config)." >&2
        return 1
    fi

}

########################################################################

[ "$(id -u)" -ne 0 ] \
  && { echo "$0 must be run by superuser." >&2 ; exit 1 ; }

unset STOP START

case "$1" in
    stop)      STOP=1
              ;;
    restart)  STOP=1
              START=1
              ;;
    ''|start)  START=1
              ;;
    *)        echo "Invalid option $1" >&2
              exit 1
              ;;
esac


if [ -z "$2" ]; then
    interfaces=$( list_interfaces )
else
    shift
    interfaces="$*"
fi

echo "Interfaces: $interfaces"

for interface in $interfaces
do
  ( [ $STOP ]  && stop_interface $interface \
              && echo "  $interface stopped."
    [ $START ] && { start_interface $interface \
              && echo "  $interface started." ; }
  )&
done

wait

########################################################################

Then a couple of /etc/hostname.* files along the lines of
Code:

root@ws1:~# cat /etc/hostname.lo
inet 127.0.0.1
root@ws1:~# cat /etc/hostname.eth0
dhcp

Static routes can also be appended to the files using
Code:

!route add -net blah blah blah

It doesn't do Wifi yet, but I might get around to that when I have a few minutes.

at0 08-28-2013 03:30 PM

Modifications of rc.inet1 and rc.inet1.conf to add static routes support
 
I have modified rc.inet1 and rc.inet1.conf files to be able to add new static routes through rc.inet1.conf (maybe it will be useful). Any suggestions/modifications/comments would be highly appreciated (this is my first "serious" work in this field)

rc.inet1
Code:

# IMMEDIATELY AFTER GATEWAY FUNCTIONS

###########################
# STATIC ROUTES FUNCTIONS #
###########################

# set MAXROUTES variable - the maximum number of routes to search for
MAXROUTES=${MAXROUTES:-20}

# Function to create route.
route_up() {
# Determine position 'i' of this routee in the ROUTE array:
  i=0
  while [ $i -lt $MAXROUTES ]; do
    [ "${ROUTE[$i]}" = "${1}" ] && break
    i=$(($i+1))
  done
 
  case ${ROUTETYPE[$i]} in
'net')
# try to determine if mask was provided as simple number (e.g. 24) or as IP address (255.255.255.0)
# and set maskpart variable accordingly (to "/mask" or " netmask mask"
    maskpart="/${ROUTEMASK[$i]}"
    point=`echo "$maskpart" | grep "\."`
    if [ "$point" == "$maskpart" ]; then
      maskpart=" netmask ${ROUTEMASK[$i]}";
    fi
    if /sbin/route -n | grep "^${1}" 1> /dev/null ; then
      echo "/etc/rc.d/rc.inet1: route $1 already exists." | $LOGGER ;
    else
      `/sbin/route add -net $1$maskpart gw ${ROUTEGATEWAY[$i]}`
    fi
    ;;
 'host')
    if /sbin/route -n | grep "^${1}" 1> /dev/null ; then
      echo "/etc/rc.d/rc.inet1: route $1 already exists." | $LOGGER ;
    else
      `/sbin/route add -host $1 gw ${ROUTEGATEWAY[$i]}`
    fi
    ;;
 *)
    echo "/etc/rc.d/rc.inet1: Invalid route type (${ROUTETYPE[$i]}) for route $1; route not set" | $LOGGER
    ;;
  esac
}

# Function to delete route.
route_down() {
# Determine position 'i' of this routee in the ROUTE array:
  i=0
  while [ $i -lt $MAXROUTES ]; do
    [ "${ROUTE[$i]}" = "${1}" ] && break
    i=$(($i+1))
  done
 
  case ${ROUTETYPE[$i]} in
'net')
    maskpart="/${ROUTEMASK[$i]}"
    point=`echo "$maskpart" | grep "\."`
    if [ "$point" == "$maskpart" ]; then
      maskpart=" netmask ${ROUTEMASK[$i]}";
    fi
    if /sbin/route -n | grep "^${1}" 1> /dev/null ; then
      `/sbin/route del -net $1$maskpart gw ${ROUTEGATEWAY[$i]}`
    else
      echo "/etc/rc.d/rc.inet1: route $1 does not exist." | $LOGGER ;
    fi
    ;;
 'host')
    if /sbin/route -n | grep "^${1}" 1> /dev/null ; then
      `/sbin/route del -host $1 gw ${ROUTEGATEWAY[$i]}`;
    else
      echo "/etc/rc.d/rc.inet1: route $1 does not exist." | $LOGGER ;
    fi
    ;;
 *)
    echo "/etc/rc.d/rc.inet1: Invalid route type (${ROUTETYPE[$i]}) for route $1; route not deleted" | $LOGGER
    ;;
  esac
}

rc.inet1 - modifications to start(), stop() functions and main case switch
Code:

# Function to start the network:
start() {
  lo_up
  for i in ${IFNAME[@]} ; do
    if_up $i
  done
  gateway_up
# create routes
  for i in ${ROUTE[@]} ; do
    route_up $i
  done
}

# Function to stop the network:
stop() {
#delete routes
  for i in ${ROUTE[@]} ; do
    route_down $i
  done
  gateway_down
  for i in ${IFNAME[@]} ; do
    if_down $i
  done
  lo_down
}


############
### MAIN ###
############

case "$1" in
'start') # "start" brings up all configured interfaces:
  start
  ;;
'stop') # "stop" takes down all configured interfaces:
  stop
  ;;
'restart') # "restart" restarts the network:
  stop
  start
  ;;
'routes_start')
  for i in ${ROUTE[@]} ; do
    route_up $i
  done
  ;;
'routes_stop')
  for i in ${ROUTE[@]} ; do
    route_down $i
  done
  ;;
# ...

to create a 'static' route, it is necessary to add next code to rc.inet1.conf:

rc.inet1.conf
Code:

# Static routes
# first network
ROUTE[0]="10.10.1.16"
ROUTETYPE[0]="net"
ROUTEMASK[0]="255.255.255.240"
ROUTEGATEWAY[0]="192.168.0.254"

# another network
ROUTE[1]="10.10.3.16"
ROUTETYPE[1]="net"
ROUTEMASK[1]="28"
ROUTEGATEWAY[1]="192.168.0.200"

# a host (without mask)
ROUTE[2]="10.10.4.25"
ROUTETYPE[2]="host"
ROUTEGATEWAY[2]="192.168.0.200"

Values in ROUTE[] should be unique. It is possible to extend script to support other route command options. And it can be supplied as a patch for rc.inet1, however I haven't tried to create it.

Another problem that I cannot solve for the moment is "disappearing" of the routes after waking up from the sleep state... Maybe anyone has suggestions for this?


All times are GMT -5. The time now is 05:00 AM.