Manuals, Timing, Ham Radio, Test Equipment

Help keep this site free:
(More Info)
        

Configure the Raspberry Pi as an Access Point

adapted from : http://www.rpiblog.com/2012/12/turn-raspberry-pi-into-wireless-access.html

Prerequisite:

This assumes that you already have a working WiFi dongle on your Pi

Install "hostapd" and "udhcpd" :

sudo apt-get install hostapd udhcpd

Configure the DHCP server

1. We need to configure DHCP so that the Raspberry Pi can distribute IP to the clients. Edit the file /etc/udhcpd.conf with following information:-

start 192.168.42.2 # This is the range of IPs that the hostspot will give to client devices.
end 192.168.42.20
interface wlan0 # The device uDHCP listens on.
remaining yes
opt dns 8.8.8.8 4.2.2.2 # The DNS servers client devices will use.
opt subnet 255.255.255.0
opt router 192.168.42.1 # The Pi's IP address on wlan0 which we will set up shortly.
opt lease 864000 # 10 day DHCP lease time in seconds.

2. Edit the file /etc/default/udchpd and change the line DHCPD_ENABLED="no" to DHCPD_ENABLED="yes"

3. Assign a static IP by entering the following command:

sudo ifconfig wlan0 192.168.42.1

To set this up automatically you must edit the file /etc/network/interfaces and replace the line "iface wlan0 inet dhcp" to:

iface wlan0 inet static
address 192.168.42.1
netmask 255.255.255.0

If the line "iface wlan0 inet dhcp" does not exist, include the below lines at thebottom part of the file. You will additionally need to update the lines-

allow-hotplug wlan0
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet manual

to

allow-hotplug wlan0
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet manual

Configure hostapd

In order to create network we need to configure hostapd. It can be either an open network or a WPA- secured network for authorized access only.You need to create or edit some files accordingly.

1. The first file that needs to be edited/created is "/etc/hostapd/hostapd.conf". For a WPA-secured network make it look like this:

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

To create an open network enter the following text in the file instead:-

interface=wlan0
ssid=My_AP
hw_mode=g
channel=6
auth_algs=1
wmm_enabled=0

2. Next  step is to change a variable inside the file /etc/default/hostapd so that it points to the file that has been just created/edited. Change this line:

DAEMON_CONF=""

to

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

3. The next thing to do is to configure NAT (Network Address Translation) which is a technique that allows several devices to use a single connection to the internet. Edit the file /etc/sysctl.conf and add the following line to the end of the file:

net.ipv4.ip_forward=1
It will enable NAT in the kernel. Now, run the following commands:
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 this concludes the NAT configuration. To make it permanent so you don't have to run the commands after each reboot, run the command

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

and edit the file /etc/network/interfaces and add the following lines to the end of the file:

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

9. Almost done! Run the following commands to start the Access Point:-

sudo service hostapd start
sudo service udhcpd start

And your Pi should finally be hosting a wireless hotspot!  To get it to start on boot everytime, run these two last commands:

sudo update-rc.d hostapd enable
sudo update-rc.d udhcpd enable

And you're done! If you followed every step correctly everything should be working as expected