Linux internet sharing (II) - LAN to Wifi (NAT)

Download and install the tools necessary. I used the wifi to download them.

sudo apt-get install hostapd isc-dhcp-server

Edit /etc/dhcp/dhcpd.conf

sudo nano /etc/dhcp/dhcpd.conf

Find the lines that say

option domain-name "";
option domain-name-servers,;

and comment them out by adding a # in the beginning of each line

 #option domain-name "";
 #option domain-name-servers,;

Find the lines that say

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

and remove the # so it says

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

Then add this to the bottom

subnet netmask {
	option broadcast-address;
	option routers;
	default-lease-time 600;
	max-lease-time 7200;
	option domain-name "local";
	option domain-name-servers,;

Save the file by typing in Control-X then Y then return

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

and scroll down to INTERFACES="" and update it to say INTERFACES="wlan0"

Set up wlan0 for static IP

sudo nano /etc/network/interfaces

Insert the follwoing:

allow hotplug wlan0
iface wlan0 inet static

Configure Access Point

( You can detect wifi driver in use: "readlink /sys/class/net/wlan0/device/driver", but it may not be the right driver. Use instead if possible: driver=nl80211)

Create a new file by running sudo nano /etc/hostapd/hostapd.conf


Now we will tell the Pi where to find this configuration file. Run

sudo nano /etc/default/hostapd

Find the line #DAEMON_CONF="" and edit it so it says DAEMON_CONF="/etc/hostapd/hostapd.conf"

Configure Network Address Translation

sudo nano /etc/sysctl.conf



If you want it to be effective immediately and reset after reboot, just run this and skip the above.

echo 1 > /proc/sys/net/ipv4/ip_forward

Run the following commands to create the network translation between the ethernet port eth0 and the wifi port wlan0

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

You can check to see whats in the tables with

sudo iptables -t nat -S
sudo iptables -S

To save the firewall rules and apply them at every boot:

iptables-save > /etc/iptables.ipv4.nat
sudo nano /etc/network/interfaces

add to the end

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

Finally we can test the access point host!

Start the AP service:

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

Or start them as daemon:

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

you can always check the status of the host AP server and the DHCP server with

sudo service hostapd status
sudo service isc-dhcp-server status

Verify that they both start successfully (no 'failure' or 'errors')

To schedule them to autostart at every boot:

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

The command below was not necessary for my default installation!!!

Depending on your distro, you may need to remove WPASupplicant. Do so by running this command:

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

and then rebooting (sudo reboot)