First of all, we have to be sure weather the wifi card supports host mode or master mode. While most of the wifi module support this , there is no guaranty that every card will have this feature.
To be sure, we can install iw package to check the wifi card does have master / host mode or not.
Typing the above command in a terminal will install iw package. Now type the following in the terminal.
This shows the a full technical details of your wifi card. A sample output of the command is as follows.
Now look at the section "Supported interface mode" . If the list contains AP or AP/VLAN , then your wifi card supports master mode or host mode and you can proceed with this guide.
Step 1:
We will use hostapd package to use the wifi card as router /access point.
After installing hostapd, we have to create or edit the hostapd configuration file which is located at /etc/hostapd/hostapd.conf .
Comment out all the existing entries and add the following lines.
Step 2:
We need to install a dhcp server and configure it for wlan0. I have noticed the default dhcp server (dhcp3) has issue with hostapd. So I just uninstalled the default dhcp client and installed dnsmasq . This is really awesome application. dnsmasq works as a server as well as a client. It is also very easy to configure.
Now we will edit the default dnsmasq parameters in /etc/default/dnsmasq .
The file should only contain the following.
Save it . Now we will create a config file /etc/dnsmasq.conf so that it gives the ip address to clients which tries to connect to hosted wifi access point.
Comment out all the existing entries add the following at the end of the file .
Save it.
Step3:
We have now setted up every thing to make our ubuntu box as wifi access point / wifi router.
Now we will create an executable script say /usr/bin/wifi_ap so that we can run it as a command.
#!/bin/bash # broadcasting interface BROADCAST="wlan0" BROADCASTIP="192.168.0.1" # receiving interface broadcast is connected to. # put the correct interface name here. It is the network interface # which is connected to internet and whose connection is going to be #shared over wifi. E.g for internal 3g slot it is "hso0" , for ethernet it is #"eth0", for ppp it is "ppp0" #For me it is an internal 3g slot . RECEIVE="hso0" if [[ $1 == "-0" || $1 == "--start" ]] then ## start hostapd echo "Starting hostapd" echo "You can view the log at /var/log/hostapd.log" sleep 1 # launch hostapd daemon hostapd -d /etc/hostapd/hostapd.conf > /var/log/hostapd.log & echo "hostapd started.." ## start dhcp server; We are making sure that currently dnsmasq is not #running by any other application. So before starting dnsmasq, we are #trying to stop any previous session. echo "Stopping previous dhcp session" /etc/init.d/dnsmasq stop killall -2 dnsmasq echo "Starting dnsmasq DHCP Server on wlan0" # set IP address ifconfig $BROADCAST $BROADCASTIP sleep 2 # launch dnsmasq dhcp daemon #echo "INTERFACES=$BROADCAST" > /etc/default/dhcp dnsmasq --conf-file=/etc/dnsmasq.conf sleep 1 echo "DHCP Server Started..." elif [[ $1 == "-1" || $1 == "--stop" ]] then # send signal 2 to hostapd and dnsmasq echo "Stopping DHCP Server..." sleep 1 /etc/init.d/dnsmasq stop echo "DHCP server stopped..." sleep 1 echo "Killing hostapd, dnsmasq" killall -2 hostapd dnsmasq sleep 1 echo "All process killed.." elif [[ $1 == "-2" || $1 == "--ics" ]] then echo "Starting Internet connection sharing..." # create iptables rules iptables -A FORWARD -i $RECEIVE -o $BROADCAST -s 192.168.0.1/24 -m conntrack --ctstate NEW -j ACCEPT iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT iptables -A POSTROUTING -t nat -j MASQUERADE # set kernel variable(s) echo 1 > /proc/sys/net/ipv4/conf/all/forwarding # edit kernel configuration cp /etc/sysctl.conf /etc/sysctl.conf.ap_ctl echo "net.ipv4.conf.default.forwarding=1" >> /etc/sysctl.conf echo "net.ipv4.conf.all.forwarding=1" >> /etc/sysctl.conf sleep 1 echo "Internet connection is now shared over wifi..." echo "" # To stop internet sharing elif [[ $1 == "-3" || $1 == "--noics" ]] then echo "Stopping Internet connection sharing..." # remove iptables rules iptables -D FORWARD 1 iptables -D FORWARD 1 # set kernel variable(s) echo 0 > /proc/sys/net/ipv4/conf/all/forwarding # revert kernel configuration mv -i /etc/sysctl.conf.ap_ctl /etc/sysctl.conf # restart networking #/etc/init.d/networking restart sleep 1 echo "Internet connection sharing stopped..." else echo $0 echo "A tool to manage hostapd and dnsmasq DHCP server" echo "Usage:" echo " -0 --start Start hostapd and dhcpd3" echo " -1 --stop Stop hostapd and dhcpd3 with signal 2" echo " -2 --ics Activate internet connection sharing" echo " between specified interfaces" echo " -3 --noics Undo internet connection sharing settings" fi exit 0
Save it. Exit the text editor. Now run
Now you can run this script as command. This script has 4 options i.e start , stop , ics and noics .
To start the hostspot , you should run
To stop the hostspot , you can run
To share internet connection over the hostspot
To stop sharing internet connection ,
Hope you enjoy it.