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 "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

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

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

Find the lines that say

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

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.
 authoritative;

Then add this to 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;
}

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
	address 192.168.42.1
	netmask 255.255.255.0

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

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

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

Uncomment:

net.ipv4.ip_forward=1

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)

Source:

http://learn.adafruit.com/setting-up-a-raspberry-pi-as-a-wifi-access-point/install-software