[Tutorial] Using the RasPi as a WiFi hostspot (8 Nov 12)


256 posts   Page 1 of 11   1, 2, 3, 4, 5 ... 11
by hunternet93 » Wed Oct 03, 2012 2:06 pm

Update 9 Nov: The tutorial has now been moved to: http://elinux.org/RPI-Wireless-Hotspot


Update 11/8/12: Tutorial updated to work with 10/28/12 Raspbian image.


Recently I needed a WiFi hotspot (AKA access point) for work. I had my Raspberry Pi and a WiFi USB dongle, so I set to work configuring it as a hotspot. My Pi is now set up where on boot, it connects to the Internet through ethernet, and shares that connection over WiFi. The following steps were performed on Raspbian but should be much the same on any Debian-based distro. Here's what I did:

1. Install the necessary software.

Code: Select all
sudo apt-get install hostapd udhcpd


2. Configure DHCP. Edit the file /etc/udhcpd.conf and configure it like this:

Code: Select all
start 192.168.2.2 # This is the range of IPs that the hostspot will give to client devices.
    end 192.168.2.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.2.1 # The Pi's IP address on wlan0 which we will set up shortly.
    opt lease 864000 # 10 day DHCP lease time in seconds


Edit the file /etc/default/udhcpd.conf and change the line:
Code: Select all
DHCPD_ENABLED="no"

to
Code: Select all
#DHCPD_ENABLED="no"


You will need to give the Pi a static IP address with the following command:

Code: Select all
sudo ifconfig wlan0 192.168.2.1


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

Code: Select all
iface wlan0 inet static
      address 192.168.2.1
      netmask 255.255.255.0


If the line "iface wlan0 inet dhcp" is not present, add the above lines to the bottom of the file.

Change the lines:
Code: Select all
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
    iface default inet dhcp

to:
Code: Select all
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
    #iface default inet dhcp


3. Configure HostAPD. Edit the file /etc/hostapd/hostapd.conf (create it if it doesn't exist) and add the following lines:

Code: Select all
interface=wlan0
    ssid=Your Network Name Here
    hw_mode=g
    channel=6 # Change this channel to one of your choosing, between 1 and 12. If you have connection issues, try changing the channel.
    auth_algs=1 # This will create an open, unencrypted WiFi network. Use at your own risk.
    wmm_enabled=0 # Some wireless adapters/drivers have issues with 802.11n, this disables it (basically).
     
    Change ssid= and channel= to values of your choice. SSID is the hotspot's name which is broadcast to other devices, channel is what frequency the hotspot will run on. For many, many more options see the file /usr/share/doc/hostapd/examples/hostapd.conf.gz
     
    Edit the file /etc/default/hostapd and change the line:
    [code]#DAEMON_CONF=""[/code]
    to:
    [code]DAEMON_CONF="/etc/hostapd/hostapd.conf[/code]
     
    4. Configure NAT. NAT (Network Address Translation) is a technique that allows several devices to use a single connection to the internet. Linux supports NAT using Netfilter (also known as iptables), and if fairly easy to set up. First, edit the file /etc/sysctl.conf and add the following line to the bottom of the file:
     
    [code]net.ipv4.ip_forward=1[/code]
     
    This will enable NAT in the kernel. Second, run the following commands:
     
    [code]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[/code]
     
    Your Pi is now NAT-ing. To make this permanent so you don't have to run the commands after each reboot, run the following command:
     
    [code]sudo iptables-save > /etc/iptables.ipv4.nat[/code]
     
    Now edit the file /etc/network/interfaces and add the following line to the bottom of the file:
     
    [code]up iptables-restore < /etc/iptables.ipv4.nat[/code]
     
    5. Fire it up! Run the following commands to start the access point:
     
    [code]sudo service hostapd start
    sudo service udhcpd start[/code]
     
    Your Pi should now be hosting a wireless hotspot. To get the hotspot to start on boot, run these additional commands:
     
    [code] sudo update-rc.d hostapd enable
    sudo update-rc.d udhcpd enable


At the completion of these instructions, your Pi should be providing a wireless network and allowing other devices to connect to the Internet. From my experience, the Pi makes a decent access point, although with cheaper WiFi dongles range will be fairly limited. I haven't stress tested this setup, but it seems to work fairly well and is handy when a "real" access point isn't available. I wrote most of the instructions from memory, if you find any errors/typos I'll correct them.

Note: A few errors have been fixed since the original post, if you have had issues please try again with the new instructions
Posts: 188
Joined: Mon Dec 12, 2011 4:34 pm
by malakai » Wed Oct 03, 2012 2:38 pm
Looks great can't wait to get home and try it out.

What wireless adapter are you using?

Also want to try out with http://www.raspberrypi.org/phpBB3/viewtopic.php?f=41&t=3059 A DIY PoE.
http://www.raspians.com - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)
User avatar
Posts: 1279
Joined: Sat Sep 15, 2012 10:35 am
by bredman » Wed Oct 03, 2012 2:53 pm
A warning for all readers...

Most WiFi adaptors cannot support Access Point mode, so be careful if you buy something just for this purpose.

You need a WiFi device which supports "AP mode" or "Master mode".
Posts: 1413
Joined: Tue Jan 17, 2012 2:38 pm
by hunternet93 » Wed Oct 03, 2012 3:56 pm
malakai wrote:Looks great can't wait to get home and try it out.

What wireless adapter are you using?

Also want to try out with http://www.raspberrypi.org/phpBB3/viewtopic.php?f=41&t=3059 A DIY PoE.

I'm currently using a cheap dongle from Amazon using a Ralink chipset, I'm not sure of the exact model. PoE would be cool combined with the Pi, I may try that out.
Posts: 188
Joined: Mon Dec 12, 2011 4:34 pm
by Linus » Wed Oct 03, 2012 4:30 pm
The guy in this http://sirlagz.net/2012/08/09/how-to-use-the-raspberry-pi-as-a-wireless-access-pointrouter-part-1/ tutorial uses
Code: Select all
iw list
to determine if the dongle has Acces Point capabilities.
Posts: 19
Joined: Sat Mar 03, 2012 9:49 am
by hunternet93 » Wed Oct 03, 2012 7:31 pm
Unless I am very mistaken, the wireless dongle does not need to support master mode. HostAPD (host access point daemon) uses low-level WiFi magic to turn any supported wireless card into an access point, hardware master mode not required. I just got a really cheap dongle off Amazon for this, it doesn't support master mode but hostapd works great.
Posts: 188
Joined: Mon Dec 12, 2011 4:34 pm
by malakai » Wed Oct 03, 2012 7:47 pm
When I get home or time I will test on cards I know that have neither supported I believe the software should handle all the issues that would require such adapters.
http://www.raspians.com - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)
User avatar
Posts: 1279
Joined: Sat Sep 15, 2012 10:35 am
by poing » Thu Oct 04, 2012 9:08 pm
This does sound interesting and is exactly what I need for my embedded project, so, after a double backup of my precious installation ;) , I tried it, but it doesn't work :( .

Is the line:
"iptables-restore < /etc/iptables.ipv4.nat " correct, or should it be:
"iptables-restore > /etc/iptables.ipv4.nat" ?

Thanks.
Posts: 736
Joined: Thu Mar 08, 2012 3:32 pm
by hunternet93 » Thu Oct 04, 2012 11:36 pm
poing wrote:This does sound interesting and is exactly what I need for my embedded project, so, after a double backup of my precious installation ;) , I tried it, but it doesn't work :( .

Is the line:
"iptables-restore < /etc/iptables.ipv4.nat " correct, or should it be:
"iptables-restore > /etc/iptables.ipv4.nat" ?

Thanks.


The first line is correct. The "<" symbol send the contents of the iptables.ipv4.nat file to the iptables-restore program. Sorry it doesn't work. What is the problem, exactly?
Posts: 188
Joined: Mon Dec 12, 2011 4:34 pm
by poing » Fri Oct 05, 2012 6:54 am
I have the Edimax nano stick, which worked great with the setup through the icon in the latest Raspian distro. I then worked through your tutorial, where there were two problems.

The first was that I was not allowed to complete one step using sudo, I had to do "sudo bash #" before I went through. Sadly I didn't record the exact step (but I can repeat the process after replacing the backup), I think it was "iptables-save > /etc/iptables.ipv4.nat".

the next problem was that I had to change, I think, "/etc/udhcpd.conf" or "/etc/hostapd/hostapd.conf" where a value had to be changed from "no" to "yes" to allow changes.

After 'firing up' nothing happened. I then restarted the Pi but still no go. Before the Edimax was blinking all the time with a small blue led, but now it's just dead. I guess I'll retry and carefully note the steps above as I really would like this to work. Sadly I'm a newbie, so I have no clue myself :D
Posts: 736
Joined: Thu Mar 08, 2012 3:32 pm
by hunternet93 » Fri Oct 05, 2012 11:33 am
poing wrote:I have the Edimax nano stick, which worked great with the setup through the icon in the latest Raspian distro. I then worked through your tutorial, where there were two problems.

The first was that I was not allowed to complete one step using sudo, I had to do "sudo bash #" before I went through. Sadly I didn't record the exact step (but I can repeat the process after replacing the backup), I think it was "iptables-save > /etc/iptables.ipv4.nat".

the next problem was that I had to change, I think, "/etc/udhcpd.conf" or "/etc/hostapd/hostapd.conf" where a value had to be changed from "no" to "yes" to allow changes.

After 'firing up' nothing happened. I then restarted the Pi but still no go. Before the Edimax was blinking all the time with a small blue led, but now it's just dead. I guess I'll retry and carefully note the steps above as I really would like this to work. Sadly I'm a newbie, so I have no clue myself :D


Hmm. Try running the following commands and posting the results:
Code: Select all
sudo service hostapd status
sudo service udhcpd status
sudo ifconfig
sudo iwconfig

The first two commands check if HostAPD and uDHPD are running, the second two give some information on your network interfaces. It's very possible I missed a step when writing the instructions, I wrote them during a break a work and haven't had time to verify them on a Pi yet.
Posts: 188
Joined: Mon Dec 12, 2011 4:34 pm
by hunternet93 » Fri Oct 05, 2012 1:56 pm
I found an error in my tutorial. The line
Code: Select all
 iptables-restore < /etc/iptables.ipv4.nat

should be
Code: Select all
 up iptables-restore < /etc/iptables.ipv4.nat

I'll edit my tutorial to fix this.
I'm also having an issue with getting the thing to work on boot, I'll post when I get that worked out.
Posts: 188
Joined: Mon Dec 12, 2011 4:34 pm
by hunternet93 » Fri Oct 05, 2012 2:03 pm
If you have a network management program running (i.e. NetworkManager, wicd) then you should disable or remove it. I had NetworkManger installed and it tried to take control of wlan0, preventing me from connecting.

EDIT: Also, my iptables rules seem to block traffic of any kind. Oops. I'll fix it ASAP. Also, I can't edit my first post for some reason, I reported it so hopefully an admin will take pity on me. :D
Posts: 188
Joined: Mon Dec 12, 2011 4:34 pm
by poing » Fri Oct 05, 2012 2:57 pm
OK, what I'll do is put the backup back and wait for the update of your tutorial (no reason for haste or anything) and then I'll try again step by step.
Posts: 736
Joined: Thu Mar 08, 2012 3:32 pm
by poing » Sat Oct 06, 2012 7:24 pm
After looking into this a bit further it appears my Edimax is not compatible with hostapd so I decided to order a Ralink RT5370 as is described here:
http://sirlagz.net/2012/08/09/how-to-use-the-raspberry-pi-as-a-wireless-access-pointrouter-part-1/
Posts: 736
Joined: Thu Mar 08, 2012 3:32 pm
by hunternet93 » Sun Oct 07, 2012 3:52 am
I found my issue -- NetworkManager was taking control of wlan0 and eth0. I thought I had disabled it, but it was still running. I'll rewrite my tutorial when I get a chance. According to http://linuxwireless.org/en/users/Documentation/hostapd, the mac80211 drivers now used by most wireless cards moves master mode to userspace, so most cards should work in master mode. I've only tested a couple Ralink-based adapters, but I think most cards should work ok.
Posts: 188
Joined: Mon Dec 12, 2011 4:34 pm
by e21c » Sun Oct 07, 2012 9:16 pm
Hello,
I would like to make web page, which will be working as splash (welcome) screen for clients who connect to my AP.
Now, I have lighttpd server + webpy (fastCGI) working on RPi. I connect wifi dongle (TL-WN722) to hub and run hostap + dhcp.

But I don't know how to configure net traffic redirecting.
Posts: 4
Joined: Sun Oct 07, 2012 6:37 pm
by hunternet93 » Mon Oct 08, 2012 11:56 pm
I've edited the tutorial and sent it to scep, he offered to edit the post for me since I can't edit it for some reason. I believe I've worked the bugs out, I had typed a command backwards which prevented hostapd and udhcpd from starting at boot.
Posts: 188
Joined: Mon Dec 12, 2011 4:34 pm
by hunternet93 » Tue Oct 09, 2012 12:01 am
e21c wrote:Hello,
I would like to make web page, which will be working as splash (welcome) screen for clients who connect to my AP.
Now, I have lighttpd server + webpy (fastCGI) working on RPi. I connect wifi dongle (TL-WN722) to hub and run hostap + dhcp.

But I don't know how to configure net traffic redirecting.

CoovaChilli (http://coova.org/CoovaChilli) looks like it will do what you want. I've never used it but it appears to be what you're looking for.
Posts: 188
Joined: Mon Dec 12, 2011 4:34 pm
by e21c » Thu Oct 11, 2012 6:10 pm
I solved my problem with command :
sudo iptables -t nat -A PREROUTING -d 0/0 -p tcp –dport 80 -j DNAT –to MyServerIP:80
Thanks hunternet93 for your attempt to solve it.
Posts: 4
Joined: Sun Oct 07, 2012 6:37 pm
by hunternet93 » Thu Oct 11, 2012 6:22 pm
e21c wrote:I solved my problem with command :
sudo iptables -t nat -A PREROUTING -d 0/0 -p tcp –dport 80 -j DNAT –to MyServerIP:80
Thanks hunternet93 for your attempt to solve it.


I'm glad you found a solution. That's a great solution, iptables seems to be able to do anything. I may use that in the future.
Posts: 188
Joined: Mon Dec 12, 2011 4:34 pm
by Wolfram23 » Tue Nov 06, 2012 4:09 am
Hey guys. I made a thread and was pointed here to solve my issue. Basically, I am hooking my Pi up in my car and need to access it to see if it is actually working (communicating over OBD 2 with ECU).

I've got an iPhone and/or iPad to ssh to the Pi already but that's over my home network. Following the instructions did not work for me - my guess being that I'm already connected to a network and using ssh to do all this.

Is there anything I need to do differently to get this working? I can connect to a monitor if required for setup, as long as I'm able to remote into it after without a router (car is in a parkade so no wifi access)
Posts: 70
Joined: Thu Jul 19, 2012 6:50 pm
by hunternet93 » Tue Nov 06, 2012 12:15 pm
If you have a laptop available, you can connect via Ethernet. You might be able to connect and complete the steps through WiFi if you skip the commands
Code: Select all
 sudo service hostapd start
sudo service udhcpd start

and then restart the Pi. After you reboot, the Pi should start broadcasting as a hotspot. If it doesn't you'll need to connect with Ethernet or connect to a monitor to see what went wrong.
Posts: 188
Joined: Mon Dec 12, 2011 4:34 pm
by poing » Tue Nov 06, 2012 3:20 pm
hunternet93 wrote:Hmm. Try running the following commands and posting the results:
Code: Select all
sudo service hostapd status
sudo service udhcpd status
sudo ifconfig
sudo iwconfig

The first two commands check if HostAPD and uDHPD are running, the second two give some information on your network interfaces. It's very possible I missed a step when writing the instructions, I wrote them during a break a work and haven't had time to verify them on a Pi yet.


I have two Ralink dongles now. I tried the sirlagz tutorial and had initial success where I could log into my Pi with my phone and see the webpage on the Pi (that's what I need), but then MySQL went corrupt. So I decided to install everything from scratch but now it won't work anymore :( After five tries I'm a bit disparate.

Anyway I tried this tutorial again. What I had to do was change
Code: Select all
DEAMON_CONF=""
into
Code: Select all
DEAMON_CONF="/etc/hostapd/hostapd.conf"
in the file /etc/init.d/hostapd otherwise hostapd wouldn't start.

Next I had to remove the line
Code: Select all
auth_algs=0

from /etc/hostapd/hostapd.conf for it was not allowed to have arguments.

That's where I am, I can see the Access Point but it refuses to give me an IP address, which is the same status I can reach with the sirlagz tutorial (after likewise editing as above, only then I need to change a "group" line in /etc/hostapd/hostapd.conf)

Here's the output from the requested commands:

Code: Select all
pi@raspberrypi ~ $ sudo service hostapd status
[ ok ] hostapd is running.
pi@raspberrypi ~ $ sudo service udhcpd status
Usage: /etc/init.d/udhcpd {start|stop|restart|force-reload}
pi@raspberrypi ~ $ sudo ifconfig
eth0      Link encap:Ethernet  HWaddr b8:27:eb:13:26:2e 
          inet addr:192.168.1.123  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1302 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1086 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1140902 (1.0 MiB)  TX bytes:175176 (171.0 KiB)

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

mon.wlan0 Link encap:UNSPEC  HWaddr 00-0F-54-0A-AC-E2-00-00-00-00-00-00-00-00-00-00 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:130 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:12684 (12.3 KiB)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  HWaddr 00:0f:54:0a:ac:e2 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:18 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6196 (6.0 KiB)  TX bytes:6520 (6.3 KiB)

pi@raspberrypi ~ $ sudo iwconfig
lo        no wireless extensions.

wlan0     IEEE 802.11bgn  Mode:Master  Frequency:2.427 GHz  Tx-Power=20 dBm   
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Power Management:on
         
eth0      no wireless extensions.

mon.wlan0  IEEE 802.11bgn  Mode:Monitor  Tx-Power=20 dBm   
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Power Management:on
         
pi@raspberrypi ~ $


Hope someone can help.
Posts: 736
Joined: Thu Mar 08, 2012 3:32 pm
by Sonny_Jim » Tue Nov 06, 2012 3:43 pm
Code: Select all
pi@raspberrypi ~ $ sudo service udhcpd status
Usage: /etc/init.d/udhcpd {start|stop|restart|force-reload}


It looks to me udhcpd isn't running, hence why no IP addresses are being given out. Try running udhcpd from the command line to make sure the configuration file is correct.

EDIT: Or just use static addresses if you can't get udhcpd to work. Or use dnsmasq as a dhcpd server.
Posts: 10
Joined: Tue Oct 23, 2012 4:15 pm