sabato 23 novembre 2013

Turn a Raspberry Pi in a Hotspot Wifi

Some times ago I bought a Raspberry Pi, which is used as a home server. Recently I wanted to have an access point capability without needing to power some extra devices, so I tought why not use my Raspberry Pi?
To achieve this goal I purchase, on Amazon, a cheap Wifi USB dongle and attached it at the RPi. Typing 

$sudo lsusb

command on my Raspberry pi running Raspbian, it was displayed the following message:

Bus 001 Device 004: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter

First step: install and configure dhostapd

Confident to have a good chipset I installed hostapd, typing

$sudo apt-get install hostapd

Now it was time to do some googling in order to configure the service, but after have spent some time I discovered the only way to use hostapd was repacle it with this file. So typing

$sudo unzip -d hostapd.zip
$sudo mv /usr/sbin/hostap /usr/sbin/hostapd.good
$sudo cp hostapd /usr/sbin/
$sudo chmod 744 /usr/sbin/hostapd

Now verify the hostapd config file is read from the inizialization scrpit. Type

$sudo vi /etc/hostapd/ifupdown.sh

check the following line (add it if not exists):

DEAMON_CONF="/etc/hostapd/hostapd.conf"

Save the changes and edit /etc/hostapd/hostapd.conf (or create it if not exists)
Below I posted a sample:

interface=wlan0
ctrl_interface=/var/run/hostapd
driver=rtl871xdrv
country_code=US
ssid=Mobile-Wifi
hw_mode=g
channel=6
wpa=2
ieee80211n=1
wpa_passphrase=Your_Password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
beacon_int=100
wme_enabled=1
wpa_group_rekey=86400

Save the config file. In order check if hostapd is well configured type:

$sudo hostapd -dd /etc/hostapd/hostapd.conf

where -dd option shows debugging output and even more.

Second step: assign a static ip to the wlan network
In order to setup automatically a static ip it is necessary edit the file /etc/network/interfaces changing
the line

iface wlan0 inet dhcp

to:

iface wlan0 inet static
address 192.168.10.1
netmask 255.255.255.0

where address and netmask can be changed in order to achieve your network settings desired.
If the following lines are presents, comment they, as shown:

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

Obviously the wlan card must be inserted before switch on your Raspberry Pi.

Third step: install and configure dhcp server

First of all it is necessary install a dhcp server and for this tutorial I will use isc-dhcp-server:

$sudo apt-get install isc-dhcp-server

Edit /etc/dhcp/dhcpd.conf

subnet 192.168.10.0 netmask 255.255.255.0 {
range 192.168.10.10 192.168.10.20;
option broadcast-address 192.168.10.255;
option routers 192.168.10.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; }

In order to enale the DHCP server it is necessary edit /etc/default/isc-dhcp-server and change the line

 INTERFACES=""

to

INTERFACES="wlan0"

Finally do a test if all is set correctly typing:

sudo isc-dhcp-server start

if all is set correctly if read the following message:

[ ok ] Starting ISC DHCP server: dhcpd.

Forth step: configure NAT

In order to permit to share the same connection between several devices it is necessary confgure both kernel and Netfilter. Thus enable IP forwarding in the kernel:

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

Edit the file /etc/sysctl.conf adding the following line to the bottom:

net.ipv4.ip_forward=1

in order to set up automatically the IP forwarding during the boot.
Now enable NAT in the kernel, running 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

Instead of running this commands after each reboot it could wise save they in a permant way, typing this command:

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

Now edit the file /etc/network/interfaces scroll down and add the following line:

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

do a test if all is set correctly typing:
sudo hostapd start
sudo isc-dhcp-server start

if all is set correctly if read the following messages:

[ ok ] Starting advanced IEEE 802.11 management: hostapd.
[ ok ] Starting ISC DHCP server: dhcpd.

To make permanent this changes and start the services automatically at every reboot type this commands:

sudo update-rc.d hostapd enable
sudo update-rc.d isc-dhcp-server enable

If no error are displayed, now Raspberry will work as Access Point.
That's all.
Posta un commento