bypassing route config problem in raspi

For some reason when I try to configure the routing of my Ethernet connection by changing the /etc/network/interfaces file, the changes do not apply properly and it keeps giving errors such as “SIOCADDRT: file exists” “SIOCADDRT: network unreachable”. After many attempts to get this right, I gave up and instead used another method to achieve my goal.

The reason why I so wanted to add a route to my route table at startup is because somehow the statements in /etc/network/interfaces were not properly implemented at system startup. Here’s the very first config file that I used to set my raspi to use 110.76.82.1.

pi@kch-rsp ~ $ cat /etc/network/interfaces 
auto lo
iface lo inet loopback
auto eth0
allow-hotplug eth0
iface eth0 inet static
 address 110.76.82.21
 netmask 255.255.255.0
 gateway 110.76.82.1

however, even after this result of ‘route’ cmd after restart did not show what I intended. The result is as below:

pi@kch-rsp ~ $ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
110.76.82.0 * 255.255.255.0 U 0 0 0 eth0

you can see that the gateway is ‘*’ which makes my raspi unable to connect to the internet.

Even after hours of googling, I tried to use other statements but it still did not give the result that I wanted. During these attempts, I kept seeing “SIOCADDRT: file exists” and “SIOCADDRT: network unreachable” error messages at reboot. FYI, SIOCADDRT: file exists error seems to be cause by an attempt trying to override an existing route. Finally, I just decided to give up trying to do what I wanted by changing /etc/network/interfaces.

Instead I thought why not just add a shell script that will be executed at system startup? It basically does the same thing I wanted to do.

1) create a shell script in /etc/init.d directory

$ cd /etc/init.d
$ vi route_startup.sh

2) write a simple script doing the ‘route’ cmd that I desire

#!/bin/bash
route add default gw 110.76.82.1

3) change the script file into executable

chmod +x route_startup.sh

4) make sure this script gets executed when system is started (reason and meaning of this cmd: check here)

$ sudo update-rc.d route_startup.sh defaults 100

now, whenever I start my system the original /etc/network/interfaces configuration will be modified with the cmds written in the custom shell script. Nicely done indeed.

Leave a comment