Jump to content
xisto Community
Sign in to follow this  
Ahsaniqbalkmc

Convert Your Raspberry Pi Into A Wifi Router

Recommended Posts

There are literally hundreds of fun and useful things that you can do with your raspberry pi. Recently, I felt the need for an extra wifi router that can give me more control over who uses my network, in what way they use it and know what happens on it. My existing router (TP LINK TL-WR740N is doing a tremendous job and it has a great set of options to control who is allowed on the network and who is not. However, it lacks some advanced controlling options and has literally no monitoring features. (I don't blame TP Link for that as the device is not meant for that)

 

So, as always, I used the service of master Google to find out what possible options I have here. Within a couple of minutes, I got to know that a linux machine can be configured to be used as a router (both wired and wifi). And as soon as I saw the term "A linux Machine", my sweet little PI came into my mind........ At the time I didn't search further to know how to get the control and monitoring features I desire because I know in my stomach that if its a linux machine, you can do a hell lot of stuff with it...

So first thing is first..... The raspberry pi needs to be turned into a wifi router before I can dig further for the controlling and monitoring features... So let’s do it…

 

Requirements:

 

To make a raspberry pi (or any other linux machine) work like a wifi router, the followings things are required:


  • Working Raspberry Pi (using Raspbian Wheezy OS)
  • USB WIFI adapter that supports Access-Point mode
  • Connection to the internet via ethernet or some other network interface (other than the USB WIFI adapter used for Access Point mode)
  • Some time to learn and implement…

 

The theory:

 

The theory behind converting a raspberry pi or some other linux machine into a wifi router consists of three major components.


  • Access Point Mode
  • DHCP Server
  • Forwarding

Access Point Mode:

Access Point Mode means that the wifi interface on the raspberry pi would serve as an Access Point to which other computers could connect via their wifi. This is in contrast to a normal scenario where wifi is used to connect to other access points. People using “CONNECTIFY” on windows and “PORTABLE HOTSPOT” on android phones would be quite familiar with this feature.

For the wifi interface to work as an access point, it is necessary that the WIFI Hardware supports this feature. This point is especially important for the raspberry pi because there is no built in wifi interface. A USB WIFI adapter is needed which comes in different models and with different features. So if you plan to buy a USB WIFI adapter and there is a chance that you would be requiring Access Point mode, make sure that the adapter you are buying supports it.

 

DHCP server:

Every computer that connects to the router needs to be assigned a separate IP address so that many computers can simultaneously use the router without hassle. And this is where the DHCP server comes into action. The process of giving different IP to every connecting computer can also be done manually but it then becomes a tedious job and there is no wisdom in doing it manually.

 

Forwarding:

Forwarding means that the Raspberry pi should be able to Forward all traffic that comes to it but which is not meant for itself i.e. when the raspberry pi would function as a router, other computers would connect to it mostly for the purpose of getting access to the internet. So the pi should be able to forward the request of these computers to the actual router (connected to the broadband line).

 

 

Get your hands dirty:

 

The practical process of converting the raspberry pi into a wifi router is rather easy, but you still need to perform some basic linux tasks (and this is where a basic linux knowledge will help). So let’s get our hands dirty.

Note: For this tutorial, I connected to my raspberry pi via SSH (putty). Someone with direct access to the raspberry pi (i.e. Pi with Monitor, keyboard and Mouse attached) will have to first open a terminal window and then enter the commands there…

 

Setting up the environment:

After turning on the raspberry pi, and connecting to it via SSH (or opening a terminal windows), first thing that needs to be made sure of is that the OS is up-to –date. For this use the following commands:

sudo apt-get update

And then:

sudo apt-get upgrade

Installing the necessary software:

After the Pi is updated, we need to install “Hostapd” and “Isc-dhcp-Server”. Use the following command to install these software:

sudo apt-get install hostapd isc-dhcp-server

After the installation of the above tools is complete, turn off your raspberry pi by issuing the following command:

sudo shutdown –h now

When the Pi is turned off, insert the USB-WIFI adapter and turn on the pi (by plugging the USB cable out and then back in)

One the pi has booted up, issue the following command to see whether the Wlan0 interface is working or not:

iwconfig

The wlan0 interface should be present and displaying information about the wireless configuration of the device (the configurations are not important, its just that they should be visible as a sign that the interface is working correctly)

 

Set up the DHCP server:

We installed the isc-dhcp-server for our DHCP needs, but before it can work as desired, we need to modify the configuration file so that the WIFI connections would automatically be assigned unique IP addresses and DNS etc.

To edit the configuration file, enter the following command:

sudo nano /etc/dhcp/dhcpd.conf

(nano is command-line based text editor and “/etc/dhcp/dhcpd.conf” is the location of the configuration file)

Once the file is opened in nano, find the following lines:

option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

and change them to:

#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;

(i.e. comment them out as they are not needed)

 

Then find the following line:

#authoritative;

and change it to:

authoritative;

(i.e. remove the commenting sing # as this command would be needed)

 

Then scroll down to the bottom and 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;    }

(these will be the settings of our DHCP server)

Save the configuration file by pressing Ctrl+X, then Y and then Enter

Next, we need to make changes to /etc/default/isc-dhcp-server file. So enter the following command:

sudo nano /etc/default/isc-dhcp-server

and change INTERFACES=”” to INTERFACES=”wlan0”

 

Set up static IP for wlan0:

For the DHCP server to function normally, we need to make sure that our wlan0 has a static IP address.

First make sure that wlan0 interface is down:

sudo ifdown wlan0

Next, we need to edit the file /etc/network/interfaces:

sudo nano /etc/ network/interfaces

Find the line auto wlan0 and add a # in front of the line (comment it out), and in front of every line afterwards. If you don't have that line, its fine. Next, find the line allow-hotplug wlan0 and add the following lines below it;

    iface wlan0 inet static    address 192.168.42.1    netmask 255.255.255.0

Comment out the remaining lines below (by adding # in the start) and save the file.

Next assign the static IP to the wifi adapter by entering the following command:

sudo ifconfig wlan0 192.168.42.1

Configure Hostapd:

Next, we need to configure hostapd, so that our wifi access point is just like we want it to be. For this, create a new file /etc/hostapd/hostapd.conf by entering the following command:

sudo nano /etc/hostapd/hostapd.conf

Add the following to this new file:

interface=wlan0driver=nl80211ssid=my-wifihw_mode=gchannel=6macaddr_acl=0auth_algs=1ignore_broadcast_ssid=0wpa=2wpa_passphrase=my-passwordwpa_key_mgmt=WPA-PSKwpa_pairwise=TKIPrsn_pairwise=CCMP

A few things are important to understand here. If you are using Adafruit wifi adapters, the driver would be rtl871xdrv. But for most other adapters, the driver is nl80211. So you need to modify that line according to the wifi adapter you are using.

Second thing is that SSID is the name of your wifi network and wpa_passphrase is its password. So change them as you like. (Make sure that the password is at least 8 characters long

Next thing we need to do is tell hostapd where the configuration file is. So enter the following command:

sudo nano /etc/default/hostapd

and then change the line #DAEMON_CONF=”” to DAEMON_CONF=”/etc/hostapd/hostapd.conf” (the comment sign is also removed)

 

Configure the IP tables:

Here we are going to set the configurations required for proper forwarding:

Enter the following command:

sudo nano /etc/ sysctl.conf

On the bottom of this file, add the following line:

net.ipv4.ip_forward=1

This will make sure that forwarding starts working next time you bootup.

Next, we need to modify the iptables to create network translation between ethernet interface (the connection to the internet) and the wlan0 interface (the connection to other devices).

Enter 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

To make these changes permanent, (i.e. you don’t have to type the above commands every time you reboot), enter the following command:

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Enter the following command to make the changes take effect immediately:

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

And then edit the file /etc/network/interfaces (command: sudo nano /etc/network/interfaces) and add the following line at the bottom:

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

 

Update Hostapd to the newest version:

The version of hostapd that is intalled by default is 1.0 however the newest version is 2.0. The newer version has better support for wifi adapters so we need to upgrade it.

First download the following file:

http://w1.fi/releases/hostapd-2.0.tar.gz (you can download the file directly to the PI using command wget http://w1.fi/releases/hostapd-2.0.tar.gz)

Then extract it using command:

tar xzvf hostapd-2.0.tar.gz

Change the directory to hostapd-2.0/hostapd/ (command: cd hostapd-2.0/hostapd)

Next, copy the defconfig file as .config (command: cp defconfig .config)

And then open the file in nano (command: sudo nano .config) and remove the comment (#) from line #CONFIG_DRIVER_NL80211=y

Next, compile hostapd (command: make)

[NOTE: Before compiling, make sure that you have the following packages installed, otherwise you are going to get a lot of errors

libssl-dev, libnl1, libnl-dev (command: sudo apt-get install libssl-dev libnl1 libnl-dev) ]

After compilation is complete, run the following commands to replace the older version of hostapd:

sudo mv /usr/sbin/hostapd usr/sbin/hostapd.ORIG (renames the old version as hostapd.ORIG)

sudo mv hostapd /usr/sbin/hostapd

sudo chmod 755 /usr/sbin/hostapd (make necessary changes to permissions)

 

 

First Test:

 

Its time we test our WIFI access point. Run the following command:

sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf

Hurrey….. The Raspbery Pi wifi Access Point is up and running (I have written this tutorial simultaneously with setting up a wifi AP on my own PI and I confirm that it is perfectly working. So if you are getting any errors, please make sure that you have performed each step correctly).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.