Configure Raspberry Pi as WLAN to Ethernet Bridge
At home I have a WiFi network from my router through the appartment. But my working place is at the other side of the house and I do not have the possibility to pull an ethernet cable through the walls. But I for the NAS, some raspberry projects and so on I also wanted to have an wired network right at my desk. So I build myself an raspberry pi as Wifi to Ethernet bridge. Here is how to do this:
I used the dnsmasq package for this because it is combined DHCP and DNS server is simple in configuration.
Run this in your raspberry pi console:
sudo apt-get install dnsmasq
We need to configure interfaces. We will assign a static IP address to the default ethernet port which will be used as gateway. Open the network interfaces file with your favorite editor. I use nano:
sudo nano /etc/network/interfaces
Edit the eth0 to this:
iface eth0 inet static
The standard dnsmasq config gives a lot of configuration. Just create a backup like this before we write our own:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo nano /etc/dnsmasq.conf
Into the new file enter the following configuration:
interface=eth0 # Use interface eth0
listen-address=192.168.2.1 # listen on
# Bind to the interface to make sure we aren't sending things
server=188.8.131.52 # Forward DNS requests to Google DNS
domain-needed # Don't forward short names
# Never forward addresses in the non-routed address spaces.
# Assign IP addresses between 192.168.2.2 and 192.168.2.100 with a
# 12 hour lease time
Now edit the /etc/sysctl.conf file to enable packet forwarding
sudo nano /etc/sysctl.conf
Remove the hash (#) in front of the line containing net.ipv4.ip_forward=1.
This will enable packet forwarding after reboot. Reboot your raspberry:
To share the internet connection with WiFi-connected devices we configure a NAT between eth0 and wlan0:
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
sudo iptables -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT
These rules also need to be applied after a reboot. So run the following lines:
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
The rules are now saved under: /etc/iptables.ipv4.nat
This needs to run on each startup, so open /etc/rc.local with
sudo nano /etc/rc.local and add the following line ABOVE exit 0
iptables-restore < /etc/iptables.ipv4.nat
Now reboot and everything should work.