Raspberry Pi: Share Wifi Connection to Ethernet

We want to share our wifi with the lan through the ethernet connection.

1. Configure wireless:

Insert wifi dongle, boot RPi and check which networks you can see:

iwlist wlan0 scan

Let's say the output shows a network called "FloonNet":

wlan0     Scan completed :
          Cell 01 - Address: 24:13:22:b2:87:33
                    Channel:11
                    Frequency:2.462 GHz (Channel 11)
                    Quality=39/70  Signal level=-71 dBm  
                    Encryption key:on
                    ESSID:"Floon-Net"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
                              11 Mb/s; 12 Mb/s; 18 Mb/s
                    Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                    Mode:Master

Generate a psk using wpa_passphrase <network_name> <password>:

wpa_passphrase Floon-Net beetlezRcool

Output looks like this:

network={
    ssid="Floon-Net"
    #psk="beetlezRcool"
    psk=a0874df9f52de3383d9caced8d4683d1b9e5820d014df65fd8ec41a35669c0e5
}

Copy it and paste it into wpa_supplicant.conf:

sudo vi /etc/wpa_supplicant/wpa_supplicant.conf

...and restart networking...

sudo /etc/init.d/networking restart

Easy. Perhaps too easy? But cheer up, challenges await!

2. Install dhcp software

sudo apt-get install isc-dhcp-server

3. Configure dhcp for ethernet

Edit dhcpd.conf thusly:

sudo vi /etc/dhcp/dhcpd.conf

...and comment out lines so they look like this...

#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;

...uncomment line so you have this...

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;

...and append these lines at the bottom...

subnet 192.168.41.0 netmask 255.255.255.0 {
range 192.168.41.10 192.168.41.50;
option broadcast-address 192.168.41.255;
option routers 192.168.41.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local";
option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Configure the dhcp server for ethernet adapter thiswayishly:

sudo vi /etc/default/isc-dhcp-server

...inserting eth0 in the INTERFACES line so it looks like this:

INTERFACES="eth0"

Now make sure ethernet adapter is down...

sudo ifdown eth0

...and edit the interfaces file...

sudo vi /etc/network/interfaces

Take care here. Insert these lines in the eth0 bit:

iface eth0 inet static 
address 192.168.41.1
netmask 255.255.255.0

If there's a line like this, ensure it's commented out:

#iface default inet dhcp

Also assign statis IP to ehternet adapter at this time:

sudo ifconfig eth0 192.168.41.1

4. Configure NAT

Edit sysctl configuration...

sudo vi /etc/sysctl.conf

...to allow port forwarding at boot time by appending this line the bottom...

net.ipv4.ip_forward=1

Also activate port forwarding now:

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

Run the following commands to set up 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

...and take a butcher's at iptables with these:

sudo iptables -t nat -S
sudo iptables -S

Make the iptable setup active at boot time by running...

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

...and editing the interface file

sudo vi /etc/network/interfaces

...appending this line at the bootom:

up iptables-restore < /etc/iptables.ipv4.nat

5. Configure startup services

First check all is well with:

sudo service isc-dhcp-server start

...and then ensure the service daemon is activated at boot time:

sudo update-rc.d isc-dhcp-server enable

6. Reboot and test

sudo shutdown -h now

view syslog to see what happens when a client connects to the access point

tail -f /var/log/syslog

*** The End ***