Raspberry Pi: Share Ethernet Connection to Wifi Access Point

Raspberry Pi: Share Ethernet Connection to Wifi AP
This configuration assumes you will be connecting to the headless RPi via ssh over an ethernet connection. Let's say that the RPi's ethernet IP is 10.0.0.99, and the access point DHCP server will operate on 192.168.42.1

This procedure is based on an adafruit document and mixed with my own experience.

1. Preparation

  • Install Raspian
  • Expand filesystem
  • Enable ssh
  • Have wifi USB dongle at hand (Realtek RTL8192UC in this case)

2. Set up networking

Connect to the RPi over ssh:

ssh pi@10.0.0.99

Check ethernet internet connection:

ping 8.8.8.8 -c 3

Shutdown and insert wifi dongle:

sudo shutdown -h now

Start RPi and check wifi module is up:

ifconfig

3. Install software

Install access point and dhcp server:

sudo apt-get update
sudo apt-get install hostapd isc-dhcp-server

4. Configure wifi module

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.42.0 netmask 255.255.255.0 {
range 192.168.42.10 192.168.42.50;
option broadcast-address 192.168.42.255;
option routers 192.168.42.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 wifi module suchlikely:

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

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

INTERFACES="wlan0"

Now make sure wifi module is down...

sudo ifdown wlan0

...and edit the interfaces file...

sudo vi /etc/network/interfaces

Take care here. Replace everything with these lines:

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0

iface wlan0 inet static
address 192.168.42.1
netmask 255.255.255.0

Also assign statis IP to wifi module now:

sudo ifconfig wlan0 192.168.42.1

5. Configure access point

Edit the hostapd configuration...

sudo vi /etc/hostapd/hostapd.conf

...making it look exactly like this (ie. no trailing whitespace etc)...

interface=wlan0
driver=rtl871xdrv
ssid=pi
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=Arap1uns
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

...and tell hostapd where to find it...

sudo vi /etc/default/hostapd

...by changing the DAEMON_CONF line to this:

DAEMON_CONF="/etc/hostapd/hostapd.conf"

6. 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 eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

...and take a look 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

7. Build hostapd

Identify your wifi dongle with

lsusb

...which will output a line with device info like the one below. Here the device is an RTL8192UC

Bus 001 Device 004: ID 0bda:8178 Realtek Semiconductor Corp. RTL8192CU 802.11n WLAN Adapter

Select the correct driver source for download from vendor (Realtek in this scenario), and download it.

Unpack the zip file somewhere appropriate, and move the files to your RPi with e.g. sftp or filezilla. Let's say we moved the whole lot to ~/Sources on the RPi.

Move to the driver folder below:

cd ~/Sources/rtl/wpa_supplicant_hostapd

More unzipping:

tar -xzvf wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz

Move to hostapd source:

cd wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostapd/

...and finally we're ready to compile source:

make

After an epoch the new binary hostapd will be lying in this folder. Now make a backup copy of the original hostapd...

sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.ORIG

...and move our newly built version into its place...

sudo mv hostapd /usr/sbin

...and make it executable:

sudo chmod 755 /usr/sbin/hostapd

Check that hostapd is working by starting the access point manually...

sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf

...and connecting to our new pi_AP access point from another wireless device.

8. Configure startup services

First check all is well with:

sudo service hostapd start
sudo service isc-dhcp-server start

...and then ensure the service daemons are activated at boot time:

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

Get this file out of the way (e.g. to your home directory):

sudo mv /usr/share/dbus-1/system-services/fi.epitest.hostap.WPASupplicant.service ~/

9. 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 ***