Server Rack Pet-Project: Switch

After having done a little tweak to the rack, I started tackling the networking side.

I bought a network switch, I have already 2 Raspberry Pi, so finally, I can connect them to the network, then let them do something (not yet sure what though).

Because those Pis without connection they’ll be useless.

I have opted for an unmanaged switch, I know it is less cool than a managed one, but they are cheaper ?

I stumbled upon a 16-Port 10/100BASE-TX Fast Ethernet Switch (FNSW-1601). Even though I have only 2 Raspberry and an 8-ports would be more than enough, this one gives me a few extra ports in case I need them, better to have some free slots than to buy a bigger one later.

my little new friend just arrived
16 ports would be enough, right?
there’s still plenty of room in there
wow, I managed to mangle everything even with just a handful of cables
look at those nice green lights turned on!
road test: ssh connection works!

At the moment there is a “nice” networking setup. The Raspberry A has a WiFi dongle + ethernet connected to the switch, so I can also share its own connection to the Raspberry B. This is great because if I don’t connect the main ethernet line, they can still access the Internet via WiFi shared connectivity.

Finally, those Raspberries have found a new place and they can be used for something else than collecting dust.

Bonus: Implementation Details

Here is the trick to enable forwarding the packets across 2 network interfaces:

# sysctl -w net.ipv4.ip_forward=1

More details here: Share the WiFi connection to the Wired interface in CLI

Enable the WiFi connection, by editing /etc/wpa_supplicant/wpa_supplicant.conf:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
ap_scan=1

update_config=1

# main router
network={
	ssid="OPERATOR-SSID"
	psk="PASSWORD"
	key_mgmt=WPA-PSK
	id_str="wl0"
}

# extender (optional)
network={
	ssid="OPERATOR-SSID2"
	psk="PASSWORD"
	key_mgmt=WPA-PSK
	id_str="wl1"
}

Enable the WiFi connection, by editing /etc/network/interfaces.d/wlan:

allow-hotplug wlan0
auto wlan0
iface wlan0 inet manual
  wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

# main router
iface wl0 inet static
  address 192.168.1.100
  netmask 255.255.255.0
  gateway 192.168.1.254
  post-up iptables -A FORWARD -i w0 -o eth0 -j ACCEPT
  post-up iptables -A FORWARD -i eth0 -o w0 -j ACCEPT
  post-up iptables -t nat -A POSTROUTING -o w0 -j MASQUERADE

# extender (optional)
iface wl1 inet static
  address 192.168.2.100
  netmask 255.255.255.0
  gateway 192.168.1.254
  post-up iptables -A FORWARD -i wl1 -o eth0 -j ACCEPT
  post-up iptables -A FORWARD -i eth0 -o wl1 -j ACCEPT
  post-up iptables -t nat -A POSTROUTING -o wl1 -j MASQUERADE

It connects just fine to the Internet:

$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=117 time=19.3 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=117 time=23.2 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=117 time=23.5 ms
^C
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 19.316/22.015/23.542/1.914 ms

Then, the other Pi B (10.0.1.102) needs to set up its network gateway to Pi A (10.0.1.101):

$ cat /etc/network/interfaces.d/eth 
auto eth0
iface eth0 inet static
  address 10.0.1.102
  netmask 255.255.255.0
  gateway 10.0.1.101
  post-up ip route add default via 10.0.1.101 dev eth0

And now it can connect to the Internet!

$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=116 time=28.9 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=116 time=22.9 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=116 time=23.5 ms
^C
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 22.874/25.088/28.917/2.718 ms

And if I set this on my laptop after plugging in the main ethernet line sudo ifconfig eth0:1 10.0.1.105 netmask 255.255.55.0 up,ย I’m in the same network as the 2 Pis ๐Ÿ™‚