Monday, December 28, 2020

Raspberry Pi as WiFi to Ethernet bridge

I recently tried to play around with my Raspberry Pi 4 to see if I can use it as a Wifi to Ethernet bridge for my Blu-ray player (which is old, does not have WiFi, but has an Ethernet port). After some searching, I found this on the Raspberry Pi forum which seems to work. While this is for a Raspberry Pi, I think it should work on any generic device running Linux.

First, update and then install the required packages.
sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get install rpi-update dnsmasq -y
sudo rpi-update

Set up WiFi, if not done already.
sudo nano  /etc/wpa_supplicant/wpa_supplicant.conf
then add the following to the file.
network={
        ssid="mynetwork"
        psk="secret"
        key_mgmt=WPA-PSK
}

Next is to set the RPi with a static IP address on its Ethernet port.
sudo nano /etc/network/interfaces
Comment out the existing etho0 line (if any) and add the following.
#iface eth0 inet manual
allow-hotplug eth0  
iface eth0 inet static  
    address 172.24.1.1
    netmask 255.255.255.0
    network 172.24.1.0
    broadcast 172.24.1.255

Save the old dnsmasq.conf file and create a new one.
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig  
sudo nano /etc/dnsmasq.conf
Add the following:
interface=eth0      # Use interface eth0  
listen-address=172.24.1.1 # Explicitly specify the address to listen on  
bind-interfaces      # Bind to the interface to make sure we aren't sending things elsewhere  
server=8.8.8.8       # Forward DNS requests to Google DNS  
domain-needed        # Don't forward short names  
bogus-priv           # Never forward addresses in the non-routed address spaces.  
dhcp-range=172.24.1.50,172.24.1.150,12h # Assign IP addresses between 172.24.1.50 and 172.24.1.150 with a 12 hour lease time  

Enable IPv4 forwarding by
sudo nano /etc/sysctl.conf
and then uncommenting
net.ipv4.ip_forward=1

Set up the IP tables
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE 
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Then
sudo nano /lib/dhcpcd/dhcpcd-hooks/70-ipv4-nat
and add:
iptables-restore < /etc/iptables.ipv4.nat

After rebooting, it should allow devices connected via Ethernet to the RPi to connect to the Internet as long as the RPi is connected to the Internet via WiFi.

Oh, I also needed to edit /etc/dhcp/dhclient.conf because it assigns my hostname to eth0 instead of wlan0.

In it, I replaced the line
send host-name = gethostname();
with
interface "eth0" {
        send host-name = "pi_lan";
}

interface "wlan0" {
        send host-name = gethostname();
}

Replace pi_lan with whatever you want to hostname to be on Ethernet. This makes sure my RPi is available on the main WiFi network as whatever the hostname says it should be.

Then,
sudo dhclient
to make sure it works. Or just reboot.

Update (January 9, 2021): Editing /etc/dhcp/dhclient.conf didn't seem to help, my hostname still gets assigned to eth0 occasionally. Changing avahi seems to work, though. I edited /etc/avahi/avahi-daemon.conf and added, under the [server] section
allow-interfaces=wlan0
to make avahi only use the hostname on wlan0. So far so good.

Update (April 13, 2021): Adding allow-interfaces=wlan0 to /etc/avahi/avahi-daemon.conf didn't seem to help, my hostname still gets assigned to eth0 occasionally. Adding a hostname entry to it seemed to help, though. I edited /etc/avahi/avahi-daemon.conf and added, under the [server] section
host-name=myhostname
to make avahi only use myhostname (replace with whatever your hostname is) on wlan0. So far so good.

No comments: