Set-up the Raspberry Pi as an access point

I’ve had a successful time getting the Pi set-up as a Wifi access point. The end result is an access point running on one wifi dongle, bridged to a second wifi dongle for an internet connection. This means that I can connect a phone to the Pi using an adhoc wireless network connection.

For the access point dongle,  I am using an old AzureWave AW-NU221 dongle. It’s an ugly white plastic thing that’s definitely NOT a ‘nano’ dongle. This dongle is known on my system as wlan3.
For the bridged wifi dongle, I’m using the good old reliable Edimax. This is known as wlan0.

Be aware that all my scripts are stored in /boot/redwing-pi and that I am logged in as root when I run it!

 

Here is a script to install everything you need and to walk you through the set-up.

#!/bin/bash
clear

echo "Redwing Access Point installer"
echo "(c) Michael Horne 2012 / Recantha"
echo "Please note all responses requested are Case Sensitive"
echo ""
echo "SPECIFY WIRELESS DEVICE"
# DEBUGGING MODE - set to something other than hostapd.conf
HOSTAPDFILE=hostapd.conf
# List devices
iwconfig 2>/dev/null | grep wlan
echo "Please type in the name of the wlan device to use as the access point"
read WLAN

echo "Type in the name of the wifi driver. This is normally nl80211 but may vary. You can always change this later by modifying the hostapd.conf file"
read DRIVER

echo "Type in the name of the access point you wish to create"
read SSID

echo "Type in the passphrase for the access point"
read PASSPHRASE

echo "PACKAGE INSTALLATION"
echo "There are a fair few packages that need to be installed. Some of these may require you to confirm their installation"
echo "Press any key"
read -n1 -s IGNORE

apt-get update
apt-get install hostapd
apt-get install dnsmasq
apt-get install dhcpd
apt-get install wireless-tools hostapd bridge-utils

echo ""
echo "Using $WLAN for the access point"
echo ""

# HOSTAPD SECTION
echo "Generating the hostapd.conf file"

# Create the hostapd.conf file
echo "" > $HOSTAPDFILE
echo "interface=$WLAN" >> $HOSTAPDFILE
echo "driver=$DRIVER" >> $HOSTAPDFILE
echo "ctrl_interface=/var/run/hostapd" >> $HOSTAPDFILE
echo "ctrl_interface_group=0" >> $HOSTAPDFILE
echo "ssid=$SSID" >> $HOSTAPDFILE
echo "hw_mode=g" >> $HOSTAPDFILE
echo "channel=10" >> $HOSTAPDFILE
echo "wpa=3" >> $HOSTAPDFILE
echo "wpa_passphrase=$PASSPHRASE" >> $HOSTAPDFILE
echo "wpa_key_mgmt=WPA-PSK" >> $HOSTAPDFILE
echo "wpa_pairwise=TKIP" >> $HOSTAPDFILE
echo "rsn_pairwise=CCMP" >> $HOSTAPDFILE
echo "beacon_int=100" >> $HOSTAPDFILE
echo "auth_algs=3" >> $HOSTAPDFILE
echo "wmm_enabled=1" >> $HOSTAPDFILE
echo "" >> $HOSTAPDFILE

echo ""
echo "Do you want to change the system so it uses the hostpad.conf file just generated? (Do this ONCE and ONCE only)"
read -n1 -s CONFIRM
if [ "$CONFIRM" == "Y" ];then
echo "DAEMON_CONF="/boot/redwing-pi/access_point/hostapd.conf"" >> /etc/default/hostapd
fi

echo "Do you want to route the IP tables for network forwarding? [Y to do it]"
read -n1 -s CONFIRM
if [ "$CONFIRM" == "Y" ];then
iptables -A FORWARD -i $WLAN -o wlan0 -s 10.0.0.0/24 -m state --state NEW -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A POSTROUTING -t nat -j MASQUERADE
sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
fi

echo "Do you want to set /etc/network/interfaces for the access point (wlan3)? (Do this ONCE and only ONCE) [Y to do it]"
read -n1 -s CONFIRM
if [ "$CONFIRM" == "Y" ];then
echo "" >> /etc/network/interfaces
echo "auto $WLAN" >> /etc/network/interfaces
echo "iface $WLAN inet static" >> /etc/network/interfaces
echo " address 10.0.0.1" >> /etc/network/interfaces
echo " netmask 255.255.255.0" >> /etc/network/interfaces
echo " wireless-channel 10" >> /etc/network/interfaces
echo " wireless-ssid "$SSID"" >> /etc/network/interfaces
echo " wireless-mode ad-hoc" >> /etc/network/interfaces
echo "" >> /etc/network/interfaces

echo ""
echo "NB: If you have run this twice, you will need to remove the duplicate text"
echo "NB: If your wifi dongle for the access point is NOT on wlan3, edit /etc/network/interfaces to suit"
fi

# Sets dhcp range and length of lease
echo "MANUAL PROCESSES REQUIRED"
echo "Edit /etc/dnsmasq.conf"
echo "Find and change interface setting to : interface=$WLAN"
echo "dhcp-range=10.0.0.2,10.0.0.5,255.255.255.0,12h"
echo ""

You then need another script to actually run the access point:

#!/bin/bash
killall hostapd
clear

USAGE="Usage: start_access_point   "
if [ "$1" == "" ];then
	echo $USAGE
	exit 0
fi

if [ "$2" == "" ];then
	echo $USAGE
	exit 0
fi

if [ "$3" == "" ];then
	echo $USAGE
	exit 0
fi

WLAN=$1
ESSID=$2
BRIDGETO=$3

ifconfig $WLAN down
iwconfig $WLAN mode ad-hoc
ifconfig $WLAN up
iwconfig $WLAN essid "$ESSID"
ifconfig $WLAN inet 10.0.0.1

ifconfig $WLAN up 10.0.0.1 netmask 255.255.255.0
udhcpd $WLAN &

iptables --flush
iptables --table nat --flush
iptables --delete-chain
iptables --table nat --delete-chain
iptables --table nat --append POSTROUTING --out-interface $BRIDGETO -j MASQUERADE
iptables --append FORWARD --in-interface $WLAN -j ACCEPT
sysctl -w net.ipv4.ip_forward=1

# Now actually start hostapd
hostapd /boot/redwing-pi/access_point/hostapd.conf
#hostapd /boot/redwing-pi/access_point/hostapd.conf 1> /dev/null &

Now, I can’t guarantee that’ll all work, but it did for me!

 

7 comments for “Set-up the Raspberry Pi as an access point

  1. Shuker
    16 January 2013 at 8:44 am

    Good, i need to adjust this for Arch linux. Any ideas?

    • 16 January 2013 at 10:13 am

      Sorry, no. I’ve not used Arch 🙁 Try asking on the Raspberry Pi Foundation forum (www.raspberrypi.org) – they might be able to help.

  2. Ascot
    23 January 2013 at 10:16 am

    Nice work, I have seen many of your posts and help. I have a similar setup I will use when out and about in my campervan The idea being that I can jump on BtWiFi or the local campsite Hotspot from distance. I have a Sinmax SI-G98000 which I have tested connecting to my BT Router using the BTHub3-XXXX SSID. A PC connected to the RJ45 is then able to browser the Internet etc. This is working fine with the help of some of your other posts (big thanks to you and others)

    I have a problem connecting to BtWiFi though as it requires a browser login, the PC on the RJ45 does not see the Hubs login page. I wonder if this is to do with IP’s or MAC addresses, i.e. the PC is not the same as the USB adapter that made the connection request.

    Have you been able to connect to Hotspots, particularly BTWiFi, using your wireless / wireless setup?

    • 24 January 2013 at 8:13 am

      Glad the posts have helped someone 🙂
      No, I haven’t tried connecting to the BT network yet, but will give it a go today or tomorrow to see what happens. Great idea.

  3. Tamius
    31 January 2013 at 12:24 pm

    Is ESSID=2 a typo? Shouldn’t it be ESSID=$2? (Second script, right after the if sentences.)

    Also, (for clarification) what is that $3 at BRIDGETO supposed to be? Name of ethernet card (e.g. eth0)?

    • 31 January 2013 at 1:02 pm

      Yes, you’re right! Damn. It should be $2.
      The BRIDGETO should be the working internet connection. In other words, you bridge FROM your wifi dongle TO, for instance, the ethernet connection (which could be eth0 if you’re cabled. I actually bridge between wlan1 and wlan0, just to make my life complicated!)

Leave a Reply

Your email address will not be published. Required fields are marked *