2017 January

Setting a default gateway in Linux

As you’ve probably figured out by now, I’m a networking noob and I’m always trying new things to gain a better understanding of how networking works. Personally, I find it fascinating, but the deeper I dig, the the deeper the whole seems to go.

One issue is that most of my test devices (mostly Raspberry Pies) need to have 2 network adapters. 1 to communicate on my lab setup and another for me to access it through SSH, which is usually through the WiFi connection. This scenario actualy works quite well until I need to access something outside of my lab environment, but must also go THROUGH my lab environment. Since the eth0 adapter is statically set and the WiFi adapter is DHCP, the Pi sets the default route through WiFi, completely bypassing my lab environment. The only way to get around this, is to set the default route through the eth0 connection manually.

To start, see where your current route is going.

route -n

*-n says to show the IP of the gateway. If you leave it off, you’ll instead see the hostname of the device instead of the ip address.

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 wlan0
192.168.33.0    *               255.255.255.0   U     0      0        0 eth0

In the above example, it shows the default gateway going through 192.168.1.1 (my primary router), but instead, I need it to go through my internal network, 192.168.33.0.

Add the IP for the desired gateway

sudo route add default gw IP.ADD.RE.SS

*Replace ‘IP.ADD.RE.SS’ with the IP address of the router you want to use. In my case, the actual command is:

sudo route add default gw 192.168.33.1

Check the new route

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.33.1    0.0.0.0         UG    0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 wlan0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0
192.168.33.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

Now you have 2 default gateways! This will likely cause problems (well, it did for me anyway) so next we need to remove the unwanted route.

sudo route del default gw 192.168.1.1

That’s it! Check the route one last time to verify the original route is gone.

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.33.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 wlan0
192.168.33.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

These values should only remain in place as long as the device is on. Rebooting the device should set everything back, or you can simply reverse the procedure by re-adding the old route and removing the new!

By admin on January 20, 2017 | Linux, Networking | A comment?

How to configure a Raspberry Pi with a Static IP

sudo nano /etc/dhcpcd.conf

Scroll to the bottom of the file and add the following lines:

# A hook script is provided to lookup the hostname if not set by the DHCP
# server, but it should not be run by default.
nohook lookup-hostname

#Remove these and reboot to return to dynamic IP
interface eth0
static ip_address=192.168.11.101/24
static routers=192.168.11.1
static domain_name_servers=192.168.1.1

Save the file: Ctrl+O, then Enter
Exit the nano editor: Ctrl+X
Reboot the computer

sudo reboot

Notes:
The subnet mask is written in CIDR notation at the end of the static IP address.
‘static routers=’ is the default gateway
‘static domain_name_servers=’ can be followed by multiple IP addresses, with each address separated from the previous by a single space ‘ ‘.

By admin on January 19, 2017 | Raspberry Pi | A comment?

Raspberry Pi dropping WiFi when ethernet plugged in

I do love my Raspberry Pi! That’s Pi, as in very small computer, not pie as in desert. (I’m diabetic, so please don’t tempt me.) I use these little devices for all kinds of projects.

I’ve recently started using them to test an internal network with a series of routers, with each Pi on a different network. (192.168.11.0/24, 192.168.22.0/24… you get the picture.) To test these, I set static IP addressed on the ethernet connections and then access them with ssh via their dynamic WiFi IPs.

To configure the devices, I would first log in using ssh to the wlan0 interface, configure /etc/network/interfaces with the desired static IP, then plug in the ethernet cable. The problem with this was that as soon as I plugged in the cable, my WiFi connection would drop, causing me to lose all contact with the Pi.

#sudo ifplugd eth0 --kill
By admin on January 13, 2017 | Raspberry Pi | A comment?