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: 36.768

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? ;) .

Comments


By Goncalo, Thursday 8 January 2015 19:11

Hi Jack,

Many thanks for the great tutorial. Great structure, explanations to the right level of relevance and hey, it works! :D

Having said that, I've done a few tutorials before this one that had me messing with IP tables etc... so at this point I can see the ssid but if I try to connect from ubuntu or android I can't get an IP address ("Failed to obtain an IP address"). I wonder if you have any clues to what I should be looking at?

Cheers,
Goncalo

By Rene Munsch, Sunday 11 January 2015 16:25

The only thing i was confused, was you NOT advice to change the driverline (6) in /etc/hostapd/hostapd.conf.

But Hey, same here: Works like a charm. Many thanks!
Rene´

By Tweakers user Jack Flushell, Sunday 11 January 2015 21:04

Goncalo wrote on Thursday 08 January 2015 @ 19:11:
[...]

Having said that, I've done a few tutorials before this one that had me messing with IP tables etc... so at this point I can see the ssid but if I try to connect from ubuntu or android I can't get an IP address ("Failed to obtain an IP address"). I wonder if you have any clues to what I should be looking at?

Cheers,
Goncalo
Thank you Goncalo!

Maybe the client is not supporting the key (although I have seen this only with some older Windows-clients). It is also in the tutorial somewhere ;). Try to change /etc/hostapd/hostapd.conf: TKIP instead of CCMP so it looks like this:


code:
1
2
wpa_pairwise=TKIP
# rsn_pairwise=CCMP


By Massimiliano, Sunday 22 February 2015 10:23

Hello,
I tried to follow your instructions (I'm using raspbian wheezy with kernel 3.18.7) but I got some error:

make ARCH=armv6l CROSS_COMPILE= -C /lib/modules/3.18.7+/build M=/home/pi/rt8192cu modules
make[1]: ingresso nella directory "/home/pi/linux-0be82f722c097340632a59b879fbfee9c6148f53"
Building modules, stage 2.
MODPOST 1 modules
/bin/sh: 1: scripts/mod/modpost: not found
scripts/Makefile.modpost:90: set di istruzioni per l'obiettivo "__modpost" non riuscito
make[2]: *** [__modpost] Errore 127
Makefile:1384: set di istruzioni per l'obiettivo "modules" non riuscito
make[1]: *** [modules] Errore 2
make[1]: uscita dalla directory "/home/pi/linux-0be82f722c097340632a59b879fbfee9c6148f53"
Makefile:584: set di istruzioni per l'obiettivo "modules" non riuscito
make: *** [modules] Errore 2


Previously, I had a similar error but I solved with a "make recordmcount" inside the kernel source directory.
I tried to build also "modpost" but I get the error

modpost.h:12:23: fatal error: elfconfig.h: File o directory non esistente
#include "elfconfig.h"


alias elfconfig.h is missing.

Have you and idea how to solve?

Regards
Massimiliano

By Massimiliano, Sunday 22 February 2015 10:26

I forgot to say that the errors occour when I try to do the "make" inside the rt8192cu directory.

Massimiliano

By Firas, Sunday 1 March 2015 18:39

Massimiliano

I got the same your error. although I rebooted the pi. I got the same thing.
this tutorial is a great one. but I need you guys to help me to get ride of those errors, when I try to type "make" command within the rtl8192cu.

thanks in advance.

By Wally, Thursday 2 April 2015 11:28

you are talking about ARM6... but I purchased the new RPI 2B. That one has the ARM7.... is there a solutions for it.

thanks in advance

By Michel jacobs, Monday 4 May 2015 17:20

for compiling problems see this site: https://somewideopenspace...-8-on-raspberry-pi-wheezy

... :)

By Fred Agenjo Weinhold, Wednesday 6 May 2015 16:01

For the raspberry 2 I made the following changes :

-I ran rpi-source with the parameter --skip-gcc
- in stead of "ln -s ~/linux/arch/arm ~/linux/arch/armv6l" I did "ln -s ~/linux/arch/arm ~/linux/arch/armv7l"
-I did not make the proposed change to the CFLAGS section of the Makefile of hostapd

if you do this, it will work

By jown, Sunday 30 August 2015 18:36

After git clone https://github.com/dz0ny/rt8192cu.git download, make fails..


make ARCH=armv7l CROSS_COMPILE= -C /lib/modules/4.1.6-v7+/build M=/home/pi/rt8192cu modules
make[1]: Entering directory '/home/pi/linux-4507c9752292506fa6ef136114ad14ffd92b2ca5'
Makefile:610: arch/armv7l/Makefile: No such file or directory
make[1]: *** No rule to make target 'arch/armv7l/Makefile'. Stop.
make[1]: Leaving directory '/home/pi/linux-4507c9752292506fa6ef136114ad14ffd92b2ca5'
Makefile:584: recipe for target 'modules' failed
make: *** [modules] Error 2

*Wheezy 05-05-2015. Kernel stuff done and ok.,

By jown, Monday 31 August 2015 06:27

Hi.
Thank you for all the help and info, but I am having problems making this.
I have Raspberry Pi 2 with Rasbian 2015-05-05 image.
USB stick is rtl8192cu as requested.

I have many times tried to compile this whole thing, but this does not work.
I tried that " rpi-source with the parameter --skip-gcc" and "ln -s ~/linux/arch/arm ~/linux/arch/armv7l" but the pri-source now crashes truly badly..

I just need to have the Drivers to the USB stick in order to make Bridge AP or with local DHCP server..

Thank you

By Tweakers user Jack Flushell, Monday 31 August 2015 09:17

jown wrote on Monday 31 August 2015 @ 06:27:
Hi.
Thank you for all the help and info, but I am having problems making this.
I have Raspberry Pi 2 with Rasbian 2015-05-05 image.
USB stick is rtl8192cu as requested.

I have many times tried to compile this whole thing, but this does not work.
I tried that " rpi-source with the parameter --skip-gcc" and "ln -s ~/linux/arch/arm ~/linux/arch/armv7l" but the pri-source now crashes truly badly..

I just need to have the Drivers to the USB stick in order to make Bridge AP or with local DHCP server..

Thank you
It is very hard (read: impossible) for me to assist you with another board than mine and also another kernel, although they are similar. Best change to get some help: https://www.raspberrypi.org/forums/

Good luck!

[Comment edited on Monday 31 August 2015 09:18]


By John Masseria, Wednesday 7 October 2015 21:39

Fantastic, I couldn't have gotten this to work without this tutorial!

One question, if we load the modified 8192cu Kernel driver, why doesn't the standard Raspbian hostapd just work?

Could it be that when running the modified kernel driver, the driver name changes from nl80211 to rtl871xdrv?

Thank you,
John

By Rui Ribeiro, Friday 23 October 2015 05:10

I have just compiled and working successfully with the very same wifi in a raspberry compatible, Lamobo/Banana PI R1. This page helped me a lot pointing me in the right direction of installing the realtek files to compile the hostap files. I left my instructions at http://forum.armbian.com/...-lamobo-r1-wifi-unstable/ for now. (the codes are a bitch, I am already on my 6th try)

By Rui Ribeiro, Friday 23 October 2015 05:12

I have just compiled and working successfully with the very same wifi in a raspberry compatible, Lamobo/Banana PI R1. This page helped me a lot pointing me in the right direction of installing the realtek files to compile the hostap files. I left my instructions at http://forum.armbian.com/...-lamobo-r1-wifi-unstable/ for now. (the codes are a bitch, I am already on my 9th try)

By Rui Ribeiro, Friday 23 October 2015 05:13

I have just compiled and working successfully with the very same wifi in a raspberry compatible, Lamobo/Banana PI R1. This page helped me a lot pointing me in the right direction of installing the realtek files to compile the hostap files. I left my instructions at http://forum.armbian.com/...-lamobo-r1-wifi-unstable/ for now. (the codes are a bitch, I am already on my nth try)

By Rui Ribeiro, Friday 23 October 2015 05:13

I have just compiled and working successfully with the very same wifi in a raspberry compatible, Lamobo/Banana PI R1. This page helped me a lot pointing me in the right direction of installing the realtek files to compile the hostap files. I left my instructions at http://forum.armbian.com/...-lamobo-r1-wifi-unstable/ for now. (the codes are a bitch, I am already on my 6th try)

Comment form
(required)
(required, but will not be displayed)
(optional)