11

I am running raspbian and I can access anything on my lan through ethO but cannot ping an internet address. route -n says the following:

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

then I run sudo /sbin/route add -net 0.0.0.0 gw 192.168.1.1 eth0 followed by route -n and get

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

And everything works. So, how do I make this permanent so I don't have to run the route command everytime I boot the PI?

3
  • please paste the content of /etc/network/interfaces file. Nov 22, 2013 at 20:19
  • 1
    How can i do that in the file dhcpcd.conf?
    – user40889
    Feb 2, 2016 at 8:09
  • 1
    @winnie you should type sudo nano /etc/dhcpcd.conf and then: interface eth0, static ip_address = "the address", static routers = "the router address", static domain_name_servers = "the DNS address". Put this 4 lines one below the other and skip he commas. Substitute the text between quotes by your real numbers
    – VMMF
    Jun 23, 2016 at 18:41

4 Answers 4

5

Adding to /etc/network/interfaces didn't work for me in Raspbian/Jessie. I was adding a second route to a VPN subnet so already had used VMMF's method to add the default route; but adding

ip route add 192.168.[othersubnet].0/24 via 192.168.1.1

to a new file created at

/lib/dhcpcd/dhcpcd-hooks/40-vpnroute

hooked on to /etc/dhcpcd.conf and added the route persistently.

Ref:https://wiki.archlinux.org/index.php/dhcpcd#DHCP_static_route.28s.29 and thx VMMF for the pointers in your comments.

2
  • Nice. At first I disliked the usage of dhcpcd for everything, but I'm really starting to like it. Thanks for this tidbit.
    – Mr. B
    Mar 23, 2020 at 23:02
  • Since I only needed this for my wlan0, I just added the line to the existing file /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant. It works on Buster (2021-01-11).
    – GChuf
    Jun 15, 2021 at 6:54
3

To make the Static Route persistent or you must add the route entries to /etc/network/interfaces

Add the static routes in the following format:

ip route add [-net|-host] <host/net>/<mask> gw <host/IP> dev <Interface>

Note This is no longer valid for Raspbian Jessie, as of June 2016 you should edit the /etc/dhcpcd.conf file instead.

1
  • 3
    Attention this is no longer valid for raspbian Jessie, you should edit the /etc/dhcpcd.conf file instead (June 2016)
    – VMMF
    Jun 23, 2016 at 18:38
2

It should be noted that the up entry should be put against the iface entry (stanza as it is called in the man pages). So a more complete example for the file /etc/network/interfaces would look like this

iface eth0 inet dhcp
   up route add -net 10.200.196.0 netmask 255.255.252.0 gw 10.200.205.225 metric 1

The indent is apparently not required but usually added for clarity.

0

the problem is, you have a class A ip local address, great you have an ip address but you have the default gateway as your broadcast address you might want to change that in dhcpcd by using

sudo nano /etc/dhcpcd.conf

then you type this next part

interface wlan0
static ip_address=192.168.x.xxx # the x's mean your own ip address
static routers=(your routers ip address)
static domain_name_servers=127.0.0.1

i would recommend you use localhost(127.0.0.1) for you dns (if you have one)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.