Install Hostapd On Rpi

List all enabled services.

systemctl list-unit-files | grep enabled

List all running services.

systemctl | grep running

Get sshd to start on boot as it is disabled by default

sudo systemctl enable ssh.socket

Get dhcpd to stop on boot as it need more time to boot

sudo systemctl disable dhcpcd.service

Install hostapd

1
2
3
4
git clone https://github.com/lostincynicism/hostapd-rtl8188
sudo apt-get install libnl-3-dev # Fix fatal-error-netlink-genl-genl-h-no-such-file-or-directory
sudo apt-get install libnl-genl-3-dev
cd hostapd-rtl8188/hostapd && sudo make all

Add following lines to /boot/my-hostapd.sh file

1
2
3
4
5
6
#!/bin/sh -
#ifconfig wlan0 192.168.42.100 up
#sleep 0.5
#wpa_cli -i wlan0 terminate # Stop wpa_supplicant process
#sleep 0.5
/home/pi/hostapd-rtl8188/hostapd/hostapd /boot/hostadp.8188eu.conf

For ip configuration, it is more applicable to update /etc/network/interfaces file, but for Jessie Debian, it needs to disable dhcpd first refering to http://raspberrypi.stackexchange.com/questions/37920/how-do-i-set-up-networking-wifi-static-ip-address and https://www.raspberrypi.org/documentation/hardware/raspberrypi/bootmodes/net_tutorial.md

1
2
sudo systemctl disable dhcpcd
sudo systemctl enable networking
1
2
3
4
5
6
7
8
9
10
11
12
13
14
auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet static
address 192.168.42.100
netmask 255.255.255.0

#allow-hotplug wlan0
#iface wlan0 inet manual
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp

Settings in /boot/hostadp.8188eu.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface=wlan0
driver=rtl871xdrv
ssid=RasPi-AP
hw_mode=g
channel=1
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=1
wpa_pairwise=CCMP
#wpa=2
#rsn_pairwise=TKIP CCMP
wpa_passphrase=123456789
wpa_key_mgmt=WPA-PSK

Settings in /lib/systemd/system/hostapd.service as below

1
2
3
4
5
6
7
8
9
10
[Unit]
Description=My Hostapd Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/boot/my-hostapd.sh

[Install]
WantedBy=multi-user.target
1
2
3
sudo chmod 644 /lib/systemd/system/hostapd.service
sudo systemctl daemon-reload # 重载所有修改过的配置文件
sudo systemctl enable hostapd.service

Check the hostapd.service

sudo systemctl status hostapd.service

Start/Stop/Restart the hostapd.service

1
2
3
sudo systemctl start hostapd.service
sudo systemctl stop hostapd.service
sudo systemctl restart hostapd.service

Run tmux in Ubuntu

options in /etc/wpa_supplicant/wpa_supplicant.conf

1
2
3
4
5
6
7
ctrl_interface=/var/run/wpa_supplicant
update_config=1

network={
ssid="RasPi-AP"
psk="iplababab1411"
}

Notes

  1. In one pane, run sudo wpa_supplicant -Dnl80211,wext -iwlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
    The option of ‘wext’ is the 8188eu driver for wpa_supplicant
  2. In other pane, run sudo wpa_cli or sudo iwlist wlan0 scan
  3. iw command for “nl80211”, iwconfig/iwlist(wireless_tools) for “wext” (rtl871xdrv)
  4. wpa_supplicant - works with both wext and nl80211.
    1
    2
    3
    iwconfig wlan0 => iw dev wlan0 link (Getting info on wlan0)
    iwconfig wlan0 essid foo => iw wlan0 connect foo (Connecting to an open network)
    iwconfig wlan0 essid off => iw wlan0 ibss leave (Leave an IBSS (ad-hoc network))

Install udhcpd

  1. sudo apt-get install udhcpd

  2. Settings in /etc/udhcpd.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    start 192.168.42.2 # This is the range of IPs that the hostspot will give to client devices.
    end 192.168.42.20
    interface wlan0 # The device uDHCP listens on.
    max_leases 10
    remaining yes
    opt dns 8.8.8.8 4.2.2.2 # The DNS servers client devices will use.
    opt subnet 255.255.255.0
    opt router 192.168.42.100 # The Pi's IP address on wlan0 which we will set up shortly.
    opt lease 864000 # 10 day DHCP lease time in seconds
  3. Enable dhcpd #DHCPD_ENABLED="no" in /etc/default/udhcpd

  4. Start service

    1
    2
    sudo systemctl restart hostapd.service
    sudo service udhcpd restart
  5. Check result
    service udhcpd status Or
    cat /var/log/syslog | grep udhcpd

  6. Get the udhcpd to start on boot
    sudo update-rc.d udhcpd enable

Reference
http://www.raspberrypi-spy.co.uk/2015/10/how-to-autorun-a-python-script-on-boot-using-systemd/
https://wiki.archlinux.org/index.php/WPA_supplicant
https://wiki.archlinux.org/index.php/Wireless_network_configuration
http://elinux.org/RPI-Wireless-Hotspot