How to Set up a Raspberry Pi as a Wireless Access Point

View previous topic View next topic Go down

How to Set up a Raspberry Pi as a Wireless Access Point

Post by jamied_uk on 30th October 2015, 00:54

How to Set up a Raspberry Pi as a Wireless Access Point

Gary Sims 2nd Feb 2014 Linux 43 Comments


  • Facebook
  • Twitter
  • Google+
  • Stumble






The Raspberry Pi can connect to a Wi-Fi network using a USB dongle but using that same dongle you can also turn your Raspberry Pi into a wireless access point. Once set up correctly, this will allow other wireless devices to connect to your Pi and optionally you can route any traffic out through the Ethernet port and on to the internet (via the router from your ISP).
However, before looking at the steps needed to get this working, a word of warning. The configuration needed can be a little complex and if things don’t work as they should then troubleshooting the problem can be difficult. Also for this to work correctly, you need a WiFi USB dongle that can work as an access point. The best place to find information about your particular dongle and the Raspberry Pi is on the embedded Linux Raspberry Pi Wi-Fi adapters page.
To configure a hotspot requires several steps:

  • Configure the wireless adapter with a static IP address
  • Install and configure a DHCP server
  • Install and configure the access point daemon
  • Configure IP routing between the wireless and Ethernet

In this example, the wireless network will use the address range
Code:
192.168.42.n
and the wired Ethernet will use the address range
Code:
192.168.1.n
.

Configure the wireless adapter with a static IP address

Edit “/etc/network/interfaces” and add the static IP address information for
Code:
wlan0
. You can learn about static IP addresses in our SSH and static IP address tutorial.
sudo nano /etc/network/interfaces
Place a “#” sign in front of all the lines which mention
Code:
wlan0
and
Code:
wpa
, except for “
Code:
allow hotplug wlan0
“. Then add the following lines to the file:
iface wlan0 inet static
address 192.168.42.1
netmask 255.255.255.0
The bottom half of the file will now look something like this:

Now reboot.

Install and configure a DHCP server

Install the DHCP server:
sudo apt-get install isc-dhcp-server
You can safely ignore any errors about not being able to start the DHCP server at this point. Now edit its configuration file:
sudo nano /etc/dhcp/dhcpd.conf
Add a “#” character in front of the “
Code:
option domain-name
” lines like this:
#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;
Remove the “#” sign in front of the “
Code:
authoritative;
” statement like this:
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;
At the bottom of the file add the following lines:
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;
}
Exit from nano with “Ctrl + X”.
Make the wireless adapter the default for the DHCP request:
sudo nano /etc/default/isc-dhcp-server
Change “
Code:
INTERFACES=""
” to “
Code:
INTERFACES="wlan0"

Exit from nano with “Ctrl + X”.
Restart the DHCP server:
sudo service isc-dhcp-server restart

Install and configure the access point daemon

Install hostapd:
sudo apt-get install hostapd
Edit the hostapd configuration file and create a wireless network:
sudo nano /etc/hostapd/hostapd.conf
Add the following lines:
interface=wlan0
driver=nl80211
#driver=rtl871xdrv
ssid=MyPi
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
This will create a password protected network called
Code:
MyPi
on channel 6 with the password
Code:
raspberry
.
Tell hostapd where to find its configuration file by setting the default location:
sudo nano /etc/default/hostapd
Remove the “#” in front of “
Code:
DAEMON_CONF
” and alter the line to read:
DAEMON_CONF="/etc/hostapd/hostapd.conf"

Configure IP routing between the wireless and Ethernet

Edit “
Code:
/etc/sysctl.conf
” to enable IP forwarding:
sudo nano /etc/sysctl.conf
Find the line which reads “
Code:
Uncomment the next line to enable packet forwarding for IPv4
” and uncomment the next line like this:
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1
Run the following command to activate forwarding now:
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
Now turn the Pi into a router with the follow 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 save the routing tables into the file “
Code:
/etc/iptables.ipv4.nat

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Edit “
Code:
/etc/network/interfaces
“:
sudo nano /etc/network/interfaces
And add the following line to the end of the file. This line will restore the routing table whenever the Pi is booted:
pre-up iptables-restore < /etc/iptables.ipv4.nat
You should now reboot your Pi and test the wireless access using a laptop, smartphone, tablet or other Wi-Fi enabled device.

Troubleshooting

Since this configuration is quite complex things can easily go wrong. If you run into trouble, double check all the configuration files. It is also worth power cycling the Pi as it is possible for the USB dongle to get into an undefined state.
You may have noticed that the "hostapd.conf" file had two "
Code:
driver=
" lines and one of them was commented out. If your USB Wi-Fi dongle uses the
Code:
nl80211
driver then the above configuration should work OK. However if your dongle uses the
Code:
rtl871xdrv
which it does for adapters based on the Realtek RTL8188CUS chipset then there are some extra steps needed.
First install the
Code:
iw
package:
sudo apt-get install iw
Now run the following command:
iw list
If the output of the command is "
Code:
nl80211 not found
" then you need to use the
Code:
rtl871xdrv
driver. However you will also need a special version of hostapd.
Adafruit has a pre-compiled version of hostapd for the
Code:
rtl871xdrv
driver. To install it use the following commands:
wget http://www.adafruit.com/downloads/adafruit_hostapd.zip
unzip adafruit_hostapd.zip
sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.ORIG
sudo mv hostapd /usr/sbin
sudo chmod 755 /usr/sbin/hostapd
avatar
jamied_uk
Admin

Posts : 2243
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum