WiFi access point using a Realtek 8192cu based USB WiFi dongle with a Raspberry Pi

By Jack Flushell on Friday 26 September 2014 15:34 - Comments (17)
Categories: Linux, Raspberry Pi, Tutorial, Views: 26.445

Introduction
There are lot of tutorials on creating a WiFi access point with your Raspberry Pi (or other *nix based box) using a USB WiFi dongle, like this one. It is not that difficult, as long the chip in the USB dongle is supported by the hostapd-software in the repositorty (of Raspbian): that means: The nl80211-driver can be used to instruct the chip in AP-mode. This driver supports most new USB WiFi-dongles and creating an access point is pretty easy in those cases.

WiFi USB dongles with the widespread Realtek 8192cu chip however, are not supported by the standard hostapd software: They need a special driver (kernel module) and a special hostapd: both can be compiled on your Raspberry Pi. How to do that can be (partially) found with some clever searching but it is not straightforward and kind of scattered over multiple how-to's / tutorials. Moreover, there are some tweaks that can not be found (at least I didn't find them ;) ). I hope this tutorial will make it easier for those people that want to use their Realtek 8192cu based WiFi dongle to create a WiFi access point (with a Raspberry Pi). This tutorial assumes you have basic/semi-advanced knowledge of linux (commands) :Y).
Goals / End Result:
  • To create a WiFi access point with a Realtek 8192cu-based WiFi USB dongle on a Raspberry Pi.
  • The Raspberry Pi will have a static IP-address.
  • It will be an access point only. Clients connecting will get an IP-address from an external DHCP server (or have a static IP-address).
  • The access point will start automatically after boot.
Hardware / OS:
I used the Raspberry Pi model B with a fully updated Raspbian operating system - Raspbian Wheezy Sept 2014. Don't forget to update your firmware to the latest version (sudo rpi-update). My Pi is connected to the internet with the ethernet cable. The WiFi USB dongle I used, is the Sitecom N300 WLA-2103: which has an excellent range (big antenna!). Needless to say, this dongle has the Realtek 8192cu chip.

To check if you have the Realtek 8192cu chip (first plugin your dongle):
code:
1
2
$ dmesg | grep 8192cu
[    7.434462] usbcore: registered new interface driver rtl8192cu


If your output is similar to the above, you are good to go (with this tutorial). If you have another chip, I suggest following this tutorial (standard mac80211-based chips with working hostapd from the Raspbian repository).
Installation and configuration of bridging:

code:
1
$ sudo apt-get install bridge-utils


Set up the bridge: In my case the network interface is called eth0 and the USB WiFi dongle is called wlan0. In almost all cases this will be the same on your Raspberry Pi. The bridge interface will be called br0. Clients will get their IP-address from a DHCP server elsewhere on the network or have a static IP-address: The Raspberry Pi is a pure Access Point. Nothing more.

Edit /etc/network/interfaces:
code:
1
$ sudo nano /etc/network/interfaces


I should look something like this: Adjust it according to your network. Note that 'address' will be the static address of your Raspberry Pi. It should be out of the range of IP-addresses the DHCP server assigns (if you have one).
code:
1
2
3
4
5
6
7
8
9
10
11
auto lo
iface lo inet loopback

auto br0
iface br0 inet static
address 192.168.1.252
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254
bridge-ports eth0 wlan0

If you like, you can restart networking or reboot. Remember to use the new static IP-adddress if you use SSH.
Install kernel sources
You will need the kernel sources of your current running kernel, so you can compile the driver module. I used rpi-source to achieve that . No need to 'sudo' here, the sources will be installed in your home directory.
code:
1
2
$ cd ~
$ sudo wget https://raw.githubusercontent.com/notro/rpi-source/master/rpi-source -O /usr/bin/rpi-source && sudo chmod +x /usr/bin/rpi-source && /usr/bin/rpi-source -q --tag-update


code:
1
$ rpi-source


This will probably give you an error about a version mismatch of gcc because the kernel sources are compiled with another version of gcc than the one that is installed on your Raspberry Pi. To correct this you will have to install the other (newer) gcc alongside the one already installed.

Version used by the kernel:
code:
1
$ cat /proc/version


Version installed:
code:
1
$ gcc --version | grep gcc


Install gcc 2.8 because that is the one (currently) used to compile the kernel:
code:
1
2
$ sudo apt-get update
$ sudo apt-get install -y gcc-4.8 g++-4.8


Setup gcc versions:
code:
1
2
3
4
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 20
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 20
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50


Now download the kernel sources and install them:
code:
1
$ rpi-source


The kernel sources are now in a directory in your home-directory called linux-somelongbla. There is a symlink to this directory called linux: So cd ~/linux will bring you to the sources. Important: Make a symlink called armv6l to the map ~/linux/arch/arm in ~/linux/arch or you will run into problems later:
code:
1
$ ln -s ~/linux/arch/arm ~/linux/arch/armv6l

Download sources and compiling the rt8192cu driver / kernel module:
The default rt8192cu driver does not support Access Point mode. But there are drivers available thanks to dz0ny. Clone his sources with git:
code:
1
2
$ cd ~
$ git clone https://github.com/dz0ny/rt8192cu.git


Let's build the driver and install it as a module:
code:
1
2
3
$ cd ~/rt8192cu
$ make
$ sudo make install


Compiling will take 15-20 minutes. Check that the old driver module is blacklisted. If not, add blacklist rtl8192cu to /etc/modprobe.d/blacklist.conf
code:
1
2
$ cat /etc/modprobe.d/blacklist.conf
blacklist rtl8192cu

Download sources for hostapd and compile them:
Go to this page (Realtek) and download the software under the Unix/Linux section called Linux Kernel 2.6.18~3.9

You will get a zip-file called (something like) RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip. You will only need part of the contents of the zip-file (only the hostapd part). Get the zip file to your home-directory of your Raspberry Pi. Unzip it:
code:
1
$ unzip RTL8192xC_USB_linux_*.zip


Now untar the part that you need for compiling the hostapd program:
code:
1
tar zxvf RTL8188C_8192C_USB_linux_*/wpa_supplicant_hostapd/wpa_supplicant_hostapd-0.8_rtw_*.tar.gz


You will now have a separate directory in your home directory with the (correct) hostapd sources. Go there:
code:
1
$ cd ~/wpa_supplicant_hostapd-0.8_*/hostapd/


Edit the Makefile (nano Makefile) and go to the CFLAGS section. Change them so it looks like this:
code:
1
CFLAGS=-MMD -Os


Now compile:
code:
1
$ make


This will take about 5-10 minutes. Copy the executable files to the appropriate location:
code:
1
$ sudo cp hostapd hostapd_cli /usr/local/sbin/

Configuration:
Create or edit the file /etc/hostapd/hostapd.conf.
code:
1
$ sudo mkdir /etc/hostapd


code:
1
$ sudo nano /etc/hostapd/hostapd.conf


Mine looks like this:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
ctrl_interface=/var/run/hostapd
###############################
# Basic Config
###############################
macaddr_acl=0 auth_algs=1
driver=rtl871xdrv
##########################
# Local configuration...
##########################
interface=wlan0
bridge=br0
hw_mode=g
ieee80211n=1
wme_enabled=1
channel=4
ssid=MyAccessPoint
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_psk=MyHexadecimalPassword
wpa_key_mgmt=WPA-PSK
#wpa_pairwise=TKIP
rsn_pairwise=CCMP


Attention:
  • The wps_psk part is a hexadecimal key. One safe and easy way to get the right hexadecimal key from your ssid/password combination is from this website. A key looks something like this: a0159c428d58ef39df217940b2d39de03e75d7aba4d791640b18c293ca9a13c1.
  • The above setup is a WPA2-only network with CCMP encryption with wireless G and N support.
  • Some (older) Windows clients will require you to uncomment 'wpa_pairwise=TKIP'.
Create an init script in /etc/init.d, so the access point (hostapd) starts when you boot your Raspberry Pi.
code:
1
$ sudo nano /etc/init.d/hostapd


Copy the following in:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
# /etc/init.d/hostapd

### BEGIN INIT INFO
# Provides:          hostapd
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Managing hostapd
# Description:       This service is used to manage hostapd (WiFi Access Point)
### END INIT INFO


case "$1" in
    start)
        echo
        echo "Starting hostapd..."
        echo
        if [ ! -d /var/run/hostapd ]; then
                rm -rf /var/run/hostapd
                mkdir /var/run/hostapd
        fi

        /usr/local/sbin/hostapd -B -P /var/run/hostapd/wlan0.pid /etc/hostapd/hostapd.conf
        ;;
    stop)
        echo
        echo "Stopping hostapd..."
        echo
        if [ -e /var/run/hostapd/wlan0.pid ]; then
                read pid < /var/run/hostapd/wlan0.pid
                if [ x$pid != x ]; then
                        kill $pid
                fi
        fi
        ;;
    restart)
        echo
        echo "Restarting hostapd..."
        echo
        if [ -e /var/run/hostapd/wlan0.pid ]; then
                read pid < /var/run/hostapd/wlan0.pid
                if [ x$pid != x ]; then
                        kill $pid
                fi
        fi
        if [ ! -d /var/run/hostapd ]; then
                rm -rf /var/run/hostapd
                mkdir /var/run/hostapd
        fi

        /usr/local/sbin/hostapd -B -P /var/run/hostapd/wlan0.pid /etc/hostapd/hostapd.conf
        ;;
    *)
        echo
        echo "Usage: /etc/init.d/hostapd start|stop|restart"
        echo
        exit 1
        ;;
esac

exit 0


Make it executable:
code:
1
$ sudo chmod +x /etc/init.d/hostapd


Make it actually start at boot:
code:
1
$ sudo update-rc.d hostapd defaults


You can start, stop and restart hostapd (your access point) by:
code:
1
2
3
$ sudo /etc/init.d/hostapd start
$ sudo /etc/init.d/hostapd stop
$ sudo /etc/init.d/hostapd restart


Reboot your Raspberry Pi. Congratulations, you have created a WiFi access point with your WiFi dongle with Realtek 8192cu chip, running on your Raspberry Pi.

P.S. Every time you update your kernel, you will have to build and install your 8192cu module (rt8192cu) again (after updating your new kernel sources).
But hey: how often are you planning to update your kernel? ;) .