WiFi –LAN Router with Raspberry Pi2

WiFi –LAN Router with Raspberry Pi2


Introduction

When I used to work with Raspberry pi, the main issue is a router with LAN port. The alternate method is to use a static IP address. I personally do not like static IP address as when I need to connect my laptop in office network need to change it to DHCP. Also there is a risk of forgetting the static IP address. So I decided to go for wired and Wireless Router configuration for my Raspberry pi.

What you need?

A Raspberry Pi2
A boot microSD card for the Raspberry Pi.
A USB WiFi device that supports “Access Point” mode.(I used WiPi from elemet14)
An Ethernet cable to connect to the local network.

How does it work?

The Raspberry Pi is configured as a WiFi and Ethernet managed Switch with DHCP.

The basic steps are

Step 1:

1. Install the necessary software.
sudo aptitude install hostapd hostap-utils iw bridge-utils
2.Edit the file Hostapd:
sudo nano /etc/init.d/hostapd
and add to (DAEMON_CONF=) the following line:
DAEMON_CONF=/etc/hostapd/hostapd.conf
Save nano(CTRL+O)and Exit(CTRL+X)
3.Configure Hostapd.conf

You can create an open network, or a WPA-secured network, or a WPA-secured network. A secure network is recommended to prevent unauthorized use and tampering, but you can also create an open network. To create a WPA-secured network, edit the file /etc/hostapd/hostapd.conf (create it if it doesn’t exist) and add the following lines:
sudo nano /etc/hostapd/hostapd.conf
ctrl_interface=/var/run/hostapd
###############################
# Basic Config
###############################
macaddr_acl=0 auth_algs=1
# Most modern wireless drivers in the kernel need driver=nl80211
driver=nl80211
##########################
# Local configuration…
##########################
interface=wlan0
hw_mode=g
ieee80211n=1
channel=1
ssid=RaspberryPI_AP
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=3
wpa_passphrase=MY_PASSPHRASE
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
Save nano(CTRL+O)and Exit(CTRL+X)
Important:
!! Change ssid=, channel=, and wpa_passphrase= 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,
!! wpa_passphrase is the password for the wireless network
(If you would like to create an open network like I used, put the following text into /etc/hostapd/hostapd.conf:)
sudo nano /etc/hostapd/hostapd.conf

ctrl_interface=/var/run/hostapd
###############################
# Basic Config
###############################
macaddr_acl=0 auth_algs=1
# Most modern wireless drivers in the kernel need driver=nl80211
driver=nl80211
##########################
# Local configuration…
##########################
interface=wlan0
ssid=RaspberryPI_AP
hw_mode=g
ieee80211n=1
channel=1
auth_algs=1
wmm_enabled=0
Save nano(CTRL+O)and Exit(CTRL+X)

Step 2:

Configure network interface
1.Change /etc/network/interfaces and add the following lines:
sudo nano /etc/network/interfaces

auto wlan0

iface wlan0 inet static

address 192.168.1.11

netmask 255.255.255.0

network 192.168.1.0

allow-hotplug wlan0

auto eth0

iface eth0 inet static

address 192.168.1.12

netmask 255.255.255.0

network 192.168.1.0

Save nano(CTRL+O)and Exit(CTRL+X)
Finish:
Restart Raspberry ore try to run this commands:

sudo /etc/init.d/networking restart
sudo service hostapd restart

Step 3:

Next we need to install the DHCP server package on our Pi so we can allocate addresses to clients.

sudo apt-get install isc-dhcp-server

Now let’s edit the DHCP server configuration file.

sudo nano /etc/dhcp/dhcpd.conf

# A slightly different configuration for an internal subnet.

subnet 192.168.1.0 netmask 255.255.255.0{

range 192.168.1.30 192.168.1.50; #//Hands out an address in this range to clients

option domain-name-servers 8.8.8.1; #//Router handles the DNS requests so send them to it

option domain-name “MakPi”; #//Same as set previously (Not sure it matters)

option routers 192.168.1.1; #//The router in the network/Gateway the clients should use

option broadcast-address 192.168.1.255;

default-lease-time 600;

max-lease-time 7200;

}

sudo /etc/init.d/isc-dhcp-server restart

You should receive two ok messages.

[ ok ] Stopping ISC DHCP server: dhcpd.

[ ok ] Starting ISC DHCP server: dhcpd

Now we need to tell the DHCP service the interface to hand out addresses on. Edit the following file:

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

Find this section, it should be at the bottom of the file.

# Path to dhcpd’s config file (default: /etc/dhcp/dhcpd.conf).

DHCPD_CONF=/etc/dhcp/dhcpd.conf

# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?

#Separate multiple interfaces with spaces, e.g. “eth0 eth1”.

INTERFACES=”wlan0 eth0

Change the INTERFACES=”eth0″ to the interface you want the DHCP service to send requests, the Raspberry Pi only has one interface so this is Eth0.

Now we need to edit the actual DHCP configuration file. Edit the following:

Step4

Start the DHCP service upon boot

Run this command to make the service start on boot otherwise when the machine is rebooted then any clients in the local network won’t be given an address.

sudo update-rc.d isc-dhcp-server start

Now you can either start the service by running the following command or reboot the server.

sudo service isc-dhcp-server start

Checking the currently leased addresses

Run this command to check the currently assigned addresses. Please note this does not include the clients given a fixed address. You should see something like this:

cat /var/lib/dhcp/dhcpd.leases

Run this command to check the currently leased addresses to the fixed clients. You should see something like this:

cat /var/lib/dhcp/dhclient.eth0.leases

Technorati Tags: Rasppberry Pi,Wireless Router,DHCP,WiFi Hotspot,Linux,Hostapd,DHCP server

Raspberry-Pi 2

Hello Guys !! Raspberry Pi is not something for profit. It is dedicated to society. In my upcoming posts will bring some interesting projects that can be used to make betterment of life… to give smile 🙂

Technorati Tags: ,,,