Difference between revisions of "RPI-Wireless-Hotspot"

From eLinux.org
Jump to: navigation, search
(Configure your Raspberry Pi as a wireless hotspot)
 
m (Removed some stray forum tags)
Line 14: Line 14:
 
   opt subnet 255.255.255.0
 
   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 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[/code]
+
   opt lease 864000 # 10 day DHCP lease time in seconds
  
 
Edit the file /etc/default/udhcpd.conf and change the line:
 
Edit the file /etc/default/udhcpd.conf and change the line:
   DHCPD_ENABLED="no"[/code]
+
   DHCPD_ENABLED="no"
 
to
 
to
   #DHCPD_ENABLED="no"[/code]
+
   #DHCPD_ENABLED="no"
  
 
You will need to give the Pi a static IP address with the following command:
 
You will need to give the Pi a static IP address with the following command:
Line 29: Line 29:
 
   iface wlan0 inet static
 
   iface wlan0 inet static
 
     address 192.168.2.1
 
     address 192.168.2.1
     netmask 255.255.255.0[/code]
+
     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.
 
If the line "iface wlan0 inet dhcp" is not present, add the above lines to the bottom of the file.
Line 35: Line 35:
 
Change the lines:
 
Change the lines:
 
   wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
 
   wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
   iface default inet dhcp[/code]
+
   iface default inet dhcp
 
to:
 
to:
 
   #wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
 
   #wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
   #iface default inet dhcp[/code]
+
   #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:
 
3. Configure HostAPD. Edit the file /etc/hostapd/hostapd.conf (create it if it doesn't exist) and add the following lines:
Line 52: Line 52:
  
 
Edit the file /etc/default/hostapd and change the line:
 
Edit the file /etc/default/hostapd and change the line:
   #DAEMON_CONF=""[/code]
+
   #DAEMON_CONF=""
 
to:
 
to:
   DAEMON_CONF="/etc/hostapd/hostapd.conf[/code]
+
   DAEMON_CONF="/etc/hostapd/hostapd.conf
  
 
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:
 
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:
  
   net.ipv4.ip_forward=1[/code]
+
   net.ipv4.ip_forward=1
  
 
This will enable NAT in the kernel. Second, run the following commands:
 
This will enable NAT in the kernel. Second, run the following commands:
Line 64: Line 64:
 
   sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
 
   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 eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
   sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT[/code]
+
   sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
  
 
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:
 
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:
  
   sudo iptables-save > /etc/iptables.ipv4.nat[/code]
+
   sudo iptables-save > /etc/iptables.ipv4.nat
  
 
Now edit the file /etc/network/interfaces and add the following line to the bottom of the file:
 
Now edit the file /etc/network/interfaces and add the following line to the bottom of the file:
  
   up iptables-restore < /etc/iptables.ipv4.nat[/code]
+
   up iptables-restore < /etc/iptables.ipv4.nat
  
 
5. Fire it up! Run the following commands to start the access point:
 
5. Fire it up! Run the following commands to start the access point:
  
 
   sudo service hostapd start
 
   sudo service hostapd start
   sudo service udhcpd start[/code]
+
   sudo service udhcpd start
  
 
Your Pi should now be hosting a wireless hotspot. To get the hotspot to start on boot, run these additional commands:
 
Your Pi should now be hosting a wireless hotspot. To get the hotspot to start on boot, run these additional commands:
  
 
   sudo update-rc.d hostapd enable
 
   sudo update-rc.d hostapd enable
   sudo update-rc.d udhcpd enable[/code]
+
   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.
 
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.
  
 
This tutorial originally was a post on the Raspberry Pi forum [http://www.raspberrypi.org/phpBB3/viewtopic.php?f=36&t=19120 here], you can reply to that topic if you have issues.
 
This tutorial originally was a post on the Raspberry Pi forum [http://www.raspberrypi.org/phpBB3/viewtopic.php?f=36&t=19120 here], you can reply to that topic if you have issues.

Revision as of 11:32, 8 November 2012

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.

 sudo apt-get install hostapd udhcpd

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

 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:

 DHCPD_ENABLED="no"

to

 #DHCPD_ENABLED="no"

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

 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:

 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:

 wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
 iface default inet dhcp

to:

 #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:

 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:

 #DAEMON_CONF=""

to:

 DAEMON_CONF="/etc/hostapd/hostapd.conf

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:

 net.ipv4.ip_forward=1

This will enable NAT in the kernel. Second, 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

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:

 sudo iptables-save > /etc/iptables.ipv4.nat

Now edit the file /etc/network/interfaces and add the following line to the bottom of the file:

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

5. Fire it up! Run the following commands to start the access point:

 sudo service hostapd start
 sudo service udhcpd start

Your Pi should now be hosting a wireless hotspot. To get the hotspot to start on boot, run these additional commands:

 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.

This tutorial originally was a post on the Raspberry Pi forum here, you can reply to that topic if you have issues.