Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to setup a Wifi Access Point on Ubuntu 12.04 (or its derivatives).
#!/bin/bash
### Setup a wifi Access Point on Ubuntu 12.04 (or its derivatives).
### make sure that this script is executed from root
if [ $(whoami) != 'root' ]
then
echo "
This script should be executed as root or with sudo:
sudo $0
"
exit 1
fi
##############################################################
## Check whether the wireless card supports Access Point mode
##############################################################
### make sure that iw is installed
apt-get -y install iw
### check that AP is supported
supports_access_point=$(iw list | sed -n -e '/* AP$/p')
if [ "$supports_access_point" = '' ]
then
echo "AP is not supported by the driver of the wireless card."
echo "This script does not work for this driver."
exit 1
fi
##############################################################
## Setup and host a network
##############################################################
### install hostapd
apt-get -y install hostapd
### it should not start automatically on boot
update-rc.d hostapd disable
### get ssid and password
ssid=$(hostname --short)
read -p "The name of your hosted network (SSID) [$ssid]: " input
ssid=${input:-$ssid}
password='1234567890'
read -p "The password of your hosted network [$password]: " input
password=${input:-$password}
### get wifi interface
rfkill unblock wifi # enable wifi in case it is somehow disabled (thanks to Darrin Wolf for this tip)
wifi_interface=$(lshw -quiet -c network | sed -n -e '/Wireless interface/,+12 p' | sed -n -e '/logical name:/p' | cut -d: -f2 | sed -e 's/ //g')
### create /etc/hostapd/hostapd.conf
cat <<EOF > /etc/hostapd/hostapd.conf
interface=$wifi_interface
driver=nl80211
ssid=$ssid
hw_mode=g
channel=1
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=3
wpa_passphrase=$password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
EOF
### modify /etc/default/hostapd
cp -n /etc/default/hostapd{,.bak}
sed -i /etc/default/hostapd \
-e '/DAEMON_CONF=/c DAEMON_CONF="/etc/hostapd/hostapd.conf"'
################################################
## Set up DHCP server for IP address management
################################################
### make sure that the DHCP server is installed
apt-get -y install isc-dhcp-server
### it should not start automatically on boot
update-rc.d isc-dhcp-server disable
### set the INTERFACES on /etc/default/isc-dhcp-server
cp -n /etc/default/isc-dhcp-server{,.bak}
sed -i /etc/default/isc-dhcp-server \
-e "/INTERFACES=/c INTERFACES=\"$wifi_interface\""
### modify /etc/dhcp/dhcpd.conf
cp -n /etc/dhcp/dhcpd.conf{,.bak}
sed -i /etc/dhcp/dhcpd.conf \
-e 's/^option domain-name/#option domain-name/' \
-e 's/^option domain-name-servers/#option domain-name-servers/' \
-e 's/^default-lease-time/#default-lease-time/' \
-e 's/^max-lease-time/#max-lease-time/'
sed -i /etc/dhcp/dhcpd.conf \
-e '/subnet 10.10.0.0 netmask 255.255.255.0/,+4 d'
cat <<EOF >> /etc/dhcp/dhcpd.conf
subnet 10.10.0.0 netmask 255.255.255.0 {
range 10.10.0.2 10.10.0.16;
option domain-name-servers 8.8.4.4, 208.67.222.222;
option routers 10.10.0.1;
}
EOF
#################################################
## Create a startup script
#################################################
cat <<EOF > /etc/init.d/wifi_access_point
#!/bin/bash
ext_interface=\$(ip route | grep default | cut -d' ' -f5)
function stop_wifi_ap {
### stop services dhcpd and hostapd
service isc-dhcp-server stop
service hostapd stop
### disable IP forwarding
echo 0 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -D POSTROUTING -s 10.10.0.0/16 -o \$ext_interface -j MASQUERADE 2>/dev/null
### remove the static IP from the wifi interface
if grep -q 'auto $wifi_interface' /etc/network/interfaces
then
sed -i /etc/network/interfaces -e '/auto $wifi_interface/,\$ d'
sed -i /etc/network/interfaces -e '\$ d'
fi
### restart network manager to takeover wifi management
service network-manager restart
}
function start_wifi_ap {
stop_wifi_ap
sleep 3
### see: https://bugs.launchpad.net/ubuntu/+source/wpa/+bug/1289047/comments/8
nmcli nm wifi off
rfkill unblock wlan
### give a static IP to the wifi interface
ip link set dev $wifi_interface up
ip address add 10.10.0.1/24 dev $wifi_interface
### protect the static IP from network-manger restart
echo >> /etc/network/interfaces
echo 'auto $wifi_interface' >> /etc/network/interfaces
echo 'iface $wifi_interface' inet static >> /etc/network/interfaces
echo 'address 10.10.0.1' >> /etc/network/interfaces
echo 'netmask 255.255.255.0' >> /etc/network/interfaces
### enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s 10.10.0.0/16 -o \$ext_interface -j MASQUERADE
### start services dhcpd and hostapd
service hostapd start
service isc-dhcp-server start
}
### start/stop wifi access point
case "\$1" in
start) start_wifi_ap ;;
stop) stop_wifi_ap ;;
esac
EOF
chmod +x /etc/init.d/wifi_access_point
### make sure that it is stopped on boot
sed -i /etc/rc.local \
-e '/service wifi_access_point stop/ d'
sed -i /etc/rc.local \
-e '/^exit/ i service wifi_access_point stop'
### display usage message
echo "
======================================
Wifi Access Point installed.
You can start and stop it with:
service wifi_access_point start
service wifi_access_point stop
"

I run the script successfully. My device is also showing the SSID, But when I connect it shows obtaining IP address and nothing happens after that!

On Ubuntu 13.04, I found the following problems with this script:

  • $wifi_interface was not correctly filled (it remained empty).
  • $ext_interface is not filled either.

This works nicely for one of my purposes (connecting a phone/tablet to my laptop for UDP control of music applications).

However, on my Ubuntu 12.04, the internet sharing is not working. After sudo service wifi_access_point start, my phone can connect and get an IP address from my laptop, but the phone's browser cannot access any websites.

How to fix that?

Linux dlm-A6200 3.2.0-54-lowlatency #56-Ubuntu SMP PREEMPT Thu Sep 19 17:22:47 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

The wifi card is Atheros AR9285.

Thanks.

@HardikMB Did you find fix for it. I am also facing the same issue now.

Thanks

After running your script and starting the DHCP Server, I get a status fail

dabou@dabou-ubuntu:~/temp$ tail -f /var/log/syslog
Jun  3 12:28:20 dabou-ubuntu dhcpd: Wrote 0 leases to leases file.
Jun  3 12:28:20 dabou-ubuntu dhcpd:
Jun  3 12:28:20 dabou-ubuntu dhcpd: No subnet declaration for wlan0 (no IPv4 addresses).
Jun  3 12:28:20 dabou-ubuntu dhcpd: ** Ignoring requests on wlan0.  If this is not what
Jun  3 12:28:20 dabou-ubuntu dhcpd:    you want, please write a subnet declaration
Jun  3 12:28:20 dabou-ubuntu dhcpd:    in your dhcpd.conf file for the network segment
Jun  3 12:28:20 dabou-ubuntu dhcpd:    to which interface wlan0 is attached. **
Jun  3 12:28:20 dabou-ubuntu dhcpd:
Jun  3 12:28:20 dabou-ubuntu dhcpd:
Jun  3 12:28:20 dabou-ubuntu dhcpd: Not configured to listen on any interfaces!

lshw should be executed as "LANG=C lshw ...". In other case, his output will be unusable for parsing.

Here is the workaround for trusty:

oper = platform.linux_distribution()
if oper[0].lower()=='ubuntu' and oper[2].lower()=='trusty':
#trusty patch
print 'applying hostapd workaround for ubuntu trusty.'
cli.execute_shell('nmcli nm wifi off')
cli.execute_shell('rfkill unblock wlan')
cli.execute_shell('sleep 1')
print 'done.'

Entire code for the hotspot daemon I've written: https://github.com/prahladyeri/hotspotd

proski commented Jul 23, 2014

Nice! Please also see my ap-run script at https://gist.github.com/proski/eae9d1c97fc21748d99d
There are a lot of similarities, but my script is in Python. It looks like I can take some ideas from you and the other way around.

Reading package lists... Done
Building dependency tree
Reading state information... Done
iw is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
AP is not supported by the driver of the wireless card.
This script does not work for this driver

This Problem I m getting when i run that script but, when I removing

check that AP is supported

supports_access_point=$(iw list | sed -n -e '/* AP$/p')
if [ "$supports_access_point" = '' ]
then
echo "AP is not supported by the driver of the wireless card."
echo "This script does not work for this driver."
exit 1
fi

script is run.

and when i m connecting so this error occurs

Starting advanced IEEE 802.11 management hostapd [fail]

plz help

I also get a similar problem as @cmoulliard

Sep 13 01:00:52 crunchbang dhcpd: Wrote 0 leases to leases file.
Sep 13 01:00:52 crunchbang dhcpd: 
Sep 13 01:00:52 crunchbang dhcpd: No subnet declaration for eth0 (124.39.204.230).
Sep 13 01:00:52 crunchbang dhcpd: ** Ignoring requests on eth0.  If this is not what
Sep 13 01:00:52 crunchbang dhcpd:    you want, please write a subnet declaration
Sep 13 01:00:52 crunchbang dhcpd:    in your dhcpd.conf file for the network segment
Sep 13 01:00:52 crunchbang dhcpd:    to which interface eth0 is attached. **
Sep 13 01:00:52 crunchbang dhcpd: 
Sep 13 01:00:52 crunchbang dhcpd: 
Sep 13 01:00:52 crunchbang dhcpd: Not configured to listen on any interfaces!

This is the subnet in my dhcpd.conf file

subnet 10.10.0.0 netmask 255.255.255.0 {
        range 10.10.0.2 10.10.0.16;
        option domain-name-servers 8.8.4.4, 208.67.222.222;
        option routers 10.10.0.1;
}

I don't know anything about subnets etc.

Hi. I am using your script and is working nicely!
I have one question though. I would like the service to start automatically when plugging in the wireless usb. For this I created a udev rule that started your service on entry of usb wirless device. I can see from the log that the service tries to start, but the hostapd sevice fails to run. It fails on

'nl80211: Failed to set channel (freq=2412): -16 (Device or resource busy)'
'/home/sporos/entto-server/hotspotOn.sh'(out) 'Could not set channel for kernel driver'
'/home/sporos/entto-server/hotspotOn.sh'(out) 'Interface initialization failed'

I have tried with many pririties for the udev rule, like 39 (to be loaded first), 70, 79,80,99 (last loaded with no result. I believe there is something to do with the things run by udev. Because manually it starts just fine

Could you anyone help with this situation?

This procedure works fine for setting up the access point.. But the internet sharing is not working,, Could you please help in this regard and suggest some work around if any?

Thanks,
Dev

aero9 commented Apr 16, 2016

thanks , genius job it's solved my handheld wifi problem

aero9 commented Apr 29, 2016

how to restore the default after installing your script , can i undo the modifications?

vishalkg commented Apr 30, 2016 edited

Hey,

All worked good on Ubuntu 12.04. But on 14.04, I am getting this error. Please help.

:~$ sudo service wifi_access_point start
stop: Unknown instance: 
 * Stopping advanced IEEE 802.11 management hostapd                      [ OK ] 
network-manager stop/waiting
network-manager start/running, process 5917
RTNETLINK answers: Operation not possible due to RF-kill
 * Starting advanced IEEE 802.11 management hostapd                      [fail] 
isc-dhcp-server` start/running, process 6074
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment