For Raspbian Jessie
From this document:
We will use
dnsmasq package for this purpose because it is combined
DHCP and DNS server and also easy to configure.
If you want something a little more 'heavyweight', you can use the
bind9 packages for DHCP and DNS respectively,
but for our purposes,
dnsmasq works just fine.
sudo apt-get install dnsmasq
We need to configure interfaces. We will assign a static IP address to
eth0 which will be used as gateway. Open the interfaces file
sudo nano /etc/network/interfaces
eth0 section like this:
iface eth0 inet static
Next, we will configure
dnsmasq. The shipped
dnsmasq config file
contains a lot of information on how to use it. So, I will advise to
move it and create a new one.
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo nano /etc/dnsmasq.conf
Paste the following into the new file
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=18.104.22.168 # 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
/etc/sysctl.conf file to enable packet forwarding
sudo nano /etc/sysctl.conf
# from the beginning of the line containing
net.ipv4.ip_forward=1 This will enable packet forwarding on next
reboot. But if you want to try it right now without reboot then do
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
We also need to share RPi’s internet connection with the devices
connected over Wi-Fi. We will configure a NAT between
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
However, we need these rules to be applied every time we reboot the
Pi, so run
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
to save the rules to the file
/etc/iptables.ipv4.nat. Now we need to
run this after each reboot, so open the
/etc/rc.local file with
sudo nano /etc/rc.local and just above the line
exit 0, add the
iptables-restore < /etc/iptables.ipv4.nat
And that’s all! Now just Reboot your RPi and you will be able to
Updated for Raspbian Stretch
Above configuration won't work in newer version of Raspbian. So, I have created a script for this which make it possible in less pain.
Connect to WiFi network using this guide.
Download the script from here. Place it at
Add the last line :
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
sudo bash /home/pi/wifi-to-eth-route.sh
Make sure you have given full path to the file. And you're done. Now reboot to see the changes