LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices



Reply
 
Search this Thread
Old 03-25-2013, 10:28 AM   #1
StreamThreader
Member
 
Registered: Mar 2012
Location: Ukraine/Odesa
Distribution: Slackware
Posts: 96

Rep: Reputation: 9
How to add permanent static route (saved between reboot)


Hello people!
Please help, how add route, save it between reboots?
Only through rc.local?
 
Old 03-25-2013, 11:17 AM   #2
BroX
Member
 
Registered: Oct 2003
Location: Sweden
Distribution: Slackware64-current
Posts: 759

Rep: Reputation: 65
No, modify /etc/rc.d/rc.inet1.conf instead.

try
Code:
man rc.inet1.conf
 
Old 03-25-2013, 11:56 AM   #3
StreamThreader
Member
 
Registered: Mar 2012
Location: Ukraine/Odesa
Distribution: Slackware
Posts: 96

Original Poster
Rep: Reputation: 9
Quote:
Originally Posted by BroX View Post
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?
 
Old 03-25-2013, 01:03 PM   #4
GazL
Senior Member
 
Registered: May 2008
Posts: 3,503

Rep: Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026
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.
 
1 members found this post helpful.
Old 03-25-2013, 07:09 PM   #5
StreamThreader
Member
 
Registered: Mar 2012
Location: Ukraine/Odesa
Distribution: Slackware
Posts: 96

Original Poster
Rep: Reputation: 9
Ok, Thanks!
 
Old 03-26-2013, 04:17 AM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,775

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
"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.
 
Old 03-26-2013, 04:27 PM   #7
GazL
Senior Member
 
Registered: May 2008
Posts: 3,503

Rep: Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026
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.

Last edited by GazL; 03-26-2013 at 04:59 PM.
 
Old 08-28-2013, 03:30 PM   #8
at0
LQ Newbie
 
Registered: May 2012
Posts: 2

Rep: Reputation: Disabled
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
permanently add static route glock19 Linux - Networking 26 04-08-2013 05:07 AM
add static route with ip command mattholm Linux - Newbie 2 01-24-2013 06:11 PM
How to add the permanent route in linux fedora core 6 cmx08 Linux - Networking 7 09-01-2010 01:00 PM
how to add static route for loopback interface. UltraSoul Linux - Software 4 04-12-2009 02:02 AM
Permanent static Route on fedora cora 5 niurkin69 Linux - Networking 2 09-22-2006 04:56 PM


All times are GMT -5. The time now is 09:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration