Turn Raspberry Pi into a wireless access point

convert raspberry pi into a wireless access point
In this post I will demonstrate how to transform the Raspberry Pi into a Wireless Access Point. The majority of this post accompanies what is portrayed here yet I have included a couple more steps.


The process may look a little bit lengthy but it can be finished under 10 minutes. Each time you need to alter or make new record, case in point -[ /etc/udhcpd.conf ] you can utilize "sudo nano /etc/udhcpd.conf".

1. The very first thing to do is to check if the dongle is being detected.Fire up the terminal and type :-
lsusb
If any adapter is detected you should get a message like this:-
Bus 001 Device 005: ID 07d1:3c09 D-Link System DWA-140 RangeBooster N Adapter(rev.B1) [Ralink RT2870]

2.Now you'll need to install the "iw" tool to check which modes are supported by your wireless dongle. Open the terminal and type:-
sudo apt-get install iw
After installation enter iw list. At this point you will see a bunch of specs being printed and some place in the middle you might find something similar to this (the modes beneath are the ones of my dongle that I am utilizing):-

Supported interface modes:
   * IBSS
   * managed
   * AP
   * AP/VLAN
   * WDS
   * monitor
   * mesh point
Depending on the support of AP option you can continue going.

3. Next step is to install "hostapd" and "udhcpd" :-
sudo apt-get install hostapd udhcpd

4. Next 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.

5. Next step is to edit the file /etc/default/udchpd and change the line DHCPD_ENABLED="no" to DHCPD_ENABLED="yes"

6. 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

7. 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. 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

8. 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"

9. 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

10. 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 :-)

7 comments

  1. Rahul, this was exactly what I was looking for!! I will try it on my Pi and let you know the result.

    ReplyDelete
  2. Great job! But after rebooting my pi there is a problem with the /etc/network/interfaces file. It couldn't be opened or something like that. In chapter 6 of your tutorial we have to update the three lines in the interface file but they are in both cases exactly the same, so i think the problem must be at this point. by commenting the line "wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf" out it seems to work as it should but i am not sure if this is the right way to solve the problem. Maybe you could take a look at it again. Thank you!

    ReplyDelete
  3. In the descprition the internet connection comes trough the ethernet port and this is shared trough wifi.
    However I want to build the system in wich the internet connection comes trough a wifi stick, and is shared another wifi trough another one.
    I followed the descripiton, with the difference that: where I found eth0, I changed it to wlan0 , and wlan0 to wlan1.
    I connect to internet with wlan0 and I want to share it trough wlan1. I went trough the description without errors, I can see my network,but I can't connect to the internet.
    The Pi doesn't answer to the sent packages. What can be the problem? Is it even possible, to connect to the internet trough one wifi stick and share this trough another one?

    The wifi sticks are TPLINK TL-WN722N.When I stop the network, I can surf the internet with the Pi.
    (I work with USB Hub with external power supply).

    Thank you for your help in advance.

    ReplyDelete
    Replies
    1. You should check out : apt-get install network-manager network-manager-gnome

      On the raspberry startx(as root sudo su). In the right-corner you see network-manager-applet. Right-click --> Edit connections. By IPv4 Settings you can change from Automatic (DHCP) to Shared to other computers.

      Can you show me some output from commands like lsusb, ifconfig, iwlist, iwconfig, dmesg |grep ath1.

      I hope this will help you!

      Delete
  4. I've tried this, it appears wifi but I can't access .... what's the solution so that the raspberry pi can be used as an access point? please help me. thank you

    ReplyDelete
  5. "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"

    I'm using ISC-dhcp-server and....

    sudo /usr/sbin/hostapd /etc/hosapd/hostapd.conf to test, but several times now I've tried to get it to launch on boot, but it never comes back, unless I manually trigger the hostapd.con and hostapd


    is there a way to confirm I indeed have it setup so load on boot as an AP?

    ReplyDelete
  6. Great job buddy. I am using leased line that's why your post is very effective for me.
    point 2 point

    ReplyDelete