Edimax EW-7811Un |
In this post, I will cover (1) the hardware and software environment that I succeeded with, (2) a brief introduction to the generic solution for Wi-Fi AP hosting on Debian, including related posts, (3) the difficulties I had with Edimax EW-7811Un, (4) my solutions and workarounds to these problems, and (5) the results.
I. Environment
First thing first, what I am trying to achieve is:
- Creating a Wi-Fi access point (AP) with my personal laptop or my Raspberry Pi, using an additional Edimax EW-7811Un adapter which works on USB 2.0
- The additional adapter should be dedicated for AP hosting
- All devices that are connected to the AP I created with this adapter will join a private network, and get Internet access through NAT using another network interface on my machine that has a working Internet connection
- Everything is ready at system start-up, automatically
The last one of my goals is important. If you only want a temporary AP that you can start/stop manually, configurations will NOT be exactly the same. However, you can still refer to the steps in this article to make this specific Edimax EW-7811Un adapter work on your machine.
As I mentioned above, I succeeded on two different platforms.
1. Laptop PC
CPU: Intel® Core™2 Duo CPU T7700 @ 2.40GHz × 2
RAM: 4GB
OS: Ubuntu 14.04 LTS (trusty) 64-bit, Linux 3.13.0-44-generic
Network adapters:
- Marvell Technology Group Ltd. 88E8055 PCI-E Gigabit Ethernet Controller (eth0)
- Intel Corporation PRO/Wireless 3945ABG [Golan] Network Connection (wlan0)
- Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS] (wlan4)
2. Raspberry Pi 1 Model B
RAM: 512MB
OS: Raspbian, Debian 7.8 (wheezy), Linux 3.10-3-rpi
Network adapters:
- Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS] (wlan4)
- Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS] (wlan9)
II. Brief introduction to Wi-Fi AP hosting on Debian
Since you have made it here, I will assume that you know why you want to do Wi-Fi AP hosting instead of ad hoc or other alternative solutions, and I agree with you on that. :-)
Generally speaking, if you would like to host a Wi-Fi AP on your Linux machine, these are the steps you should go through:
- Check if your adapter supports AP mode
- Install and configure hostapd
- Give your Wi-Fi interface a static IP address so it works as a gateway
- Install and configure a DHCP server and a DNS server
- Configure IP forwarding and firewall (if necessary)
I will list some steps and issues below that I believe to be important.
1. Mode compatibility
A Wi-Fi adapter may work in one of several modes. managed mode is the most commonplace mode, also known as infrastructure mode, in which the adapter connects to the infrastructure (AP) and looks for network connectivity. To host a Wi-Fi access point with your adapter, it is required to work in AP mode. Not every model/driver supports AP mode.
To check if your adapter supports AP mode, you will need the package iw. Install it with
sudo apt-get install iw
Then, you will be able to check the list of modes supported by your adapter with
iw list
You are looking for "AP" in the "Supported interface modes" section. For example, the build-in Wi-Fi adapter on my laptop gives me results like this:
Supported interface modes:
* IBSS
* managed
* monitor
If you found "AP" in this list, then congratulations. Otherwise you will probably think of buying some other Wi-Fi adapter or simply throw up :-)
If you are using Edimax EW-7811Un, it supports AP mode. Unfortunately I cannot show you any screenshot with the results for it, because I installed a third-party driver for this adapter in order to make it actually work, but iw only supports the default nl80211 driver. I will talk about this again in Section III.
2. Host-AP daemon
hostapd is the AP hosting daemon you will use for hosting an AP on your Linux machine. This package can also be installed with apt-get.
The configuration file for hostapd is at /etc/hostapd/hostapd.conf. A typical working configuration will look like the one presented in [1]:
interface=wlan0
driver=nl80211
ssid=
hw_mode=g
channel=1
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=3
wpa_passphrase=
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
I will briefly explain some of the fields:
- interface: Device name of the Wi-Fi adapter you would like to use for AP hosting. You can list the device names with ifconfig (all network interfaces) or iwconfig (wireless network interfaces only).
- driver: Driver to use with hostapd. nl80211 is the default wireless driver but it doesn't work well with Edimax EW-7811Un [Realtek RTL8188CUS].
- ssid: SSID of your wireless network, also known as ESSID. This is the name you will see on your mobile devices when you are trying to connect them to your wireless network.
- channel: A 2.4GHz Wi-Fi device will work in one of the 11~13 channels, named 1, 2, 3, ..., 13 respectively. As far as I see, many Wi-Fi routers work on channel 6 by default. To avoid conflicts, you can define your AP to broadcast on a different channel, that's far away from 6. The example configuration above will make the AP work on channel 1.
- wpa: WPA mode. This is for authentication purpose. Set it to 1 for WPA, 2 for WPA2, and 3 for both. Personally speaking I will recommend that you use WPA2 only.
- wpa_passphrase: Commonly known as "Wi-Fi password", should be no less than 8 characters.
Notice for Ubuntu 14.04 LTS network manager
It is observed on Ubuntu 14.04 LTS, probably also on some other Ubuntu releases, that the built-in network manager conflicts with hostapd, which makes hostapd fail to start [5]. In order to resolve this conflict, we need to disable network manager on the interface we would like to use for AP hosting. The configuration file is /etc/NetworkManager/NetworkManager.conf. Add these lines:
[keyfile]
unmanaged-devices=mac:80:1f:02:xx:xx:xx
You will need to restart the networking service or reboot your machine in order for the new configurations to take effect. Then the device with the MAC address identified here will be released from the built-in network manager, and available for hostapd. Then you will find it to be "not managed" in your network manager.
Wi-Fi network appears to be "not managed" in Ubuntu's built-in network manager |
If you would like to change the device name of your adapter to make it persistent on each system start-up, you can add a line in /etc/udev/rules.d/70-persistent-net.rules. If the file does not exist (which is likely to happen on a Raspberry Pi), you can create it yourself. The line to add is:
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="80:1f:02:xx:xx:xx", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan4"
I concealed the MAC address of my adapter in the example above, please replace the MAC address to that of the adapter, which you are going to use for AP hosting.
You can change "wlan4" to whatever you like - it doesn't even need to start with "wlan". However, don't name it something that's likely to conflict with a system default name, e.g. "eth0", "ppp0", etc.
You will need to reboot your machine in order for the new configurations to take effect.
3. Static IP address
The IP address of a device can be static - which is guaranteed to be persistent, or dynamic - which is assigned to the device by a DHCP server on the network upon connection.
Generally speaking, static public IP addresses on Internet are expensive but pretty lovely for DNS A records, thus they are mostly owned by big companies and organizations. Normal Internet users usually get assigned dynamic IP addressed by their Internet service providers (ISP) upon connection. In some areas, it's pretty common that many users share one IP address using Network Address Translation (NAT). This is not that related to our topic, so I would not prefer to explain it here too much.
In private networks, devices use private IP address, which are restricted in the same private network only. A typical private IP address may look like 10.*.*.*, e.g. 10.0.0.1, or 192.168.*.*, e.g. 192.168.0.1. If a private network is connected to the Internet, there must be one machine that serves as the gateway (also known as the router) and also runs NAT for them.
Network Address Translation (NAT) |
In order to assign your interface a static IP address, we are going to edit the network configuration at /etc/network/interfaces. By default on Ubuntu it may look like this:
auto lo
iface lo inet loopback
That's for the loopback interface... There's nothing else because other interfaces are managed by the built-in network manager. On Raspberry Pi there might be some other configurations. Don't change any of those default lines, unless it operates on the same interface we are going to use, e.g. wlan4 in our case. Add the following lines for the interface we want to use for AP hosting:
allow-hotplug wlan4
auto wlan4
iface wlan4 inet static
address 192.168.244.1
netmask 255.255.255.0
The "address" field doesn't need to be the same with the one I use here. However, it is expected to be a private address, in order not to mess up with public addresses (in your private network only). For different ranges of private IPv4 addresses, see the Wikipedia page [2]. Also, "netmask" should better be consistent with the private address space you choose in order to avoid future problems.
4. DHCP server and DNS server
There are multiple choices for DHCP and DNS servers on Debian. I will just recommend my choices.
My DHCP server is isc-dhcp-server as is suggested in [1] as well. That's the package name with which you can install it using apt-get install. We need to modify multiple places in its configuration file /etc/default/isc-dhcp-server and /etc/dhcp/dhcpd.conf.
First, in /etc/default/isc-dhcp-server, the interface which the DHCP server listens on. It should look like
INTERFACES="wlan4"
Where "wlan4" should be consistent with the name of your interface used for AP hosting. Don't have your DHCP server listen on an interface that is connected to the public network or your home router. That may lead to DHCP conflicts and chances to prevent your device from accessing the Internet correctly.
Second, in /etc/dhcp/dhcpd.conf, comment out domain name options if there's any, and add the following lines at the end of the file:
subnet 192.168.244.0 netmask 255.255.255.0 {
range 192.168.244.130 192.168.244.254;
option broadcast-address 192.168.244.255;
option routers 192.168.244.1;
option domain-name "local";
option domain-name-servers 192.168.244.1;
#option domain-name-servers 8.8.4.4, 8.8.8.8;
}
Explanations:
- range: When a device (that is configured to use a dynamic IP address) connects to your network and asks for a dynamic IP address, the DHCP server will respond with an address from this range.
- option routers: The IP address of the router (gateway). This should be consistent with the static IP address you assigned to your interface.
- option domain-name-servers: It should be filled with either your machine's address (when it is set to that static IP address, 192.168.244.1) or addresses of some public DNS servers, e.g. 8.8.4.4 and 8.8.8.8, the Google DNS servers. If you prefer to use your machine as the DNS server on your private network (recommended), please also configure a DNS server on your machine.
Post [1] doesn't cover DNS server installation and configuration. I am using dnsmasq. If you would like to use it, please install it with apt-get install. Its configuration is at /etc/dnsmasq.conf. All you need to do is to add this line:
interface=wlan4
Where "wlan4" should be consistent with the name of your interface used for AP hosting. And also, uncomment this line:
bind-interfaces
You will need to restart the dnsmasq service or reboot your machine in order for the new configurations to take effect. Then the DNS server dnsmasq will do whatever you expect it to do as a normal DNS server. It will query higher-level DNS servers for records it doesn't have.
5. IP forwarding
To enable IP forwarding, execute
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
However, this effect is not persistent. As is suggested in [3], to make it persistent instead, you can edit /etc/sysctl.conf, uncomment this line or add it at the end of the file:
net.ipv4.ip_forward=1
This configuration takes effect on next boot of your machine. Also, we need to configure the IP table. Execute this command:
sudo iptables -t nat -A POSTROUTING -s 192.168.244.0/24 -o wlan0 -j MASQUERADE
In the command above, replace "192.168.244.0/24" with your private network IP space and the length of mask, and replace "wlan0" with the name of the interface you would like to forward the traffic from your private network TO, namely, an interface that has access to Internet. If you have multiple interfaces or potential interfaces, for example, when you use PPPoE at home or VPN at school dormitory, you can execute the command above for multiple times with different "-o" options.
However, this effect is not persistent either. Actually IP table changes are not designed to be persistent. In order to achieve the persistence (so we don't execute this command every time we reboot our machine, we need package iptables-persistent. It loads /etc/iptables/rules.v4 at system start-up. To save your changes, execute iptables-save and write its output to that file [4].
Notice for firewall
If you have a firewall on your machine, you will need to allow related traffics through your machine so it works as a gateway correctly. By default, neither Ubuntu or Raspbian as any firewall enabled, therefore I will not cover this. Configurations are expected to be different, for different firewalls.
III. Problems with Edimax EW-7811Un
In this section, I will explain why I chose this specific model, and what problems I was encountered with when I attempted to configure it with the steps I presented in Section II.
1. Why Edimax EW-7811Un
These are the reasons why I chose this model:
- It is the #1 best seller on Amazon among USB 2.0 Wi-Fi dongles.
- It is cheap.
- It is power efficient, so it work on a Raspberry Pi.
2. Driver problems
The essential problem which leads to all difficulties on my laptop is, the built-in wireless driver nl80211 doesn't work with hostapd on the RTL8188CUS chip. I forgot what error I got exactly, but it was like to be
nl80211: Failed to set interface wlan4 into AP mode
The explanation was that, the chip RTL8188CUS which is used in Edimax EW-7811Un was too new and the built-in wireless driver nl80211 was not perfectly compatible with it. The imperfect compatibility may also cause failures and instabilities even when operated in managed mode.
Therefore there came a series of problems:
- Since nl80211 doesn't work with hostapd on RTL8188CUS, we hope to find an official driver from Realtek's website.
- You will find out that the driver from Realtek's website is obsolete and won't compile on Linux 3.10 or later! Actually if your search on Google "RTL8188CUS linux driver", you will get a lot of "problem" results: https://www.google.com/search?client=ubuntu&channel=fs&q=RTL8188CUS+linux+driver&ie=utf-8&oe=utf-8
- You will find some third-party driver for RTL8188CUS, but you may have problem patching it to hostapd.
3. Dual-adapter problems on Raspberry Pi
When it came to Raspberry Pi, there were other difficulties too.
The first problem was, I was using two Edimax EW-7811Un on my Raspberry Pi. Except for the one I used for AP hosting, the other one of them was configured in wpa-supplicant whose configuration file is at /etc/wpa_supplicant/wpa_supplicant.conf, for Internet access. This behavior is also defined in /etc/network/interfaces.
Then I found that, upon connection of any device to the AP hosting adapter, the Internet adapter got turned off. Thus, of course, the Internet sharing plan could not work at all because I no longer had Internet on my Raspberry Pi after any device connected to the private network. This issue was also mentioned in this post on the official Raspberry Pi forum [6]. It seemed that for some stupid reasons, Raspbian allowed at most one active network interface at the same time. In previous tests in my other projects, I found that plugging an Ethernet cable into the Ethernet port on the Pi will disable the Wi-Fi connectivity.
There was probably not a solution for that, but a workaround was published in a following-up post in the same thread. I will describe this workaround in Section IV.
After the first problem got worked around, there came the second problem. This time, after only a short period of connection, the AP hosting adapter got turned off (while the Internet adapter was still on and working).
With a series of experiments, I believe that the second problem was due to the short of power supply. Again, possible solutions will be described in Section IV.
IV. Solutions and Workarounds
To fix the driver problems I encountered with in III.2, I did a lot of research on Google and found a mixed solution that solved my own problems and hopefully would also solve yours - problems with the Realtek RTL8188CUS chip. The steps for this solutions are:
- Get a compatible third-party driver of RTL8188CUS for Linux 3.10+
- Compile and install the third-party driver, including disabling and enabling corresponding system modules
- Get a compatible version of hostapd source and patch it with the third-party driver
- Compile and install the patched hostapd, remove the one we installed with apt-get, and make sure the new executive can be called the same way as the one installed by apt-get
- Modify hostapd.conf
1. Compatible third-party driver for Linux 3.10 or later
We need a third-party driver because the official driver provided by Realtek (on this website) is obsolete, and does not support Linux kernel 3.10 or newer.
Drivers are closely related to the OS kernel and usually need to be compiled yourself, with the kernel headers on your own machine. Linux kernel is occasionally updated with changes in function prototypes or implementation. If a driver source contains some code that happen to depend on the changed functions it may fail to compile with the updated kernel headers.
Chinese Linux user frogsu [7] has presented his research in his post 解决Ubuntu 14.04 Realtek RTL8188CUS USB无线网卡不稳定问题, where the author uses a third-party driver for RTL8188C and RTL8192C provided by dr0zy on GitHub [8].
That's the third-party driver I succeeded with. To get the driver, you will need git on your Linux system. Install it with
sudo apt-get install git
When git is successfully installed, we can download the entire git repository using
git clone https://github.com/dz0ny/rt8192cu
Execute the command above in your preferred path. This command will create a sub-folder called rt8192cu in your present working directory, and put everything in the repository in that sub-folder.
2. Driver compilation and installation
When you have the rt8192cu source repository ready, change your working directory to the source path, and execute the following commands:
make modules
sudo make install
These commands will compile the driver on your machine with your Linux header and install the compiled rt8192cu module to your system. If you update your Linux kernel later, you may need to redo this step.
Then, we need to disable the built-in rtl8192cu module. Note that there's an "L" in the module name. You can manually add this line
blacklist rtl8192cu
to the end of /etc/modprobe.d/blacklist.conf, or you may also do this with one command:
echo "blacklist rtl8192cu" | sudo tee -a /etc/modprobe.d/blacklist.conf
Please be aware that the following command will not write anything to a system configuration owned by root, and set writable by owner only, e.g. 644:
sudo echo "blacklist rtl8192cu" >> /etc/modprobe.d/blacklist.conf
That's because the output redirection is actually applied to sudo instead of echo, and the redirected output will still be opened with the current user ID. sudo will only be applied to echo.
Finally, we need to enable our new module. Add this line
8192cu
to the end of /etc/modules. Again, you can achieve this by simply using one command with echo and sudo tee:
echo "8192cu" | sudo tee -a /etc/modules
You will need to reboot your machine in order for the changes to take effect. Hopefully, you will get a much more stable network interface on your RTL8188CUS adapter now. Make sure the interface is working before you move to the next step.
3. Patching the daemon source
What we have done so far would be enough for the built-in networking functionality to use our third-party rt8192cu driver. However, hostapd will not work with it because it is not prepared with the driver essentials. Actually, the integration of hostapd with the driver needs to be accomplished before compilation.
Therefore, we need to download the source code of hostapd. The source code can be found at https://w1.fi/hostapd/. The current (June, 2015) latest release of hostapd is 2.4. The patch hostapd-rtl871xdrv can be found at https://github.com/pritambaral/hostapd-rtl871xdrv [9].
However, The current (June, 2015) version of hostapd-rtl871xdrv does not support hostapd 2.4. According to this GitHub issue [10], the latest supported release of hostapd is 2.2. Therefore on the hostapd website, we need to go to "Old releases" and download hostapd-2.2.tar.gz.
Put the downloaded archive somewhere you prefer, and extract it with
tar zxvf hostapd-2.2.tar.gz
Then you will find a source directory called hostapd-2.2. Next, in the same parent directory with hostapd-2.2, download the GitHub repository of hostapd-rtl871xdrv with
git clone https://github.com/pritambaral/hostapd-rtl871xdrv
Now, you will have two directories in your present working directory: hostapd-2.2 and hostapd-rtl871xdrv. Patch the hostapd source with instructions presented in the "Installation" section of the README.md of hostapd-rtl871xdrv.
Finally you will get two executives as is said in the README.md. What we will need is the hostapd executive.
4. Customized daemon installation
First of all, we need to know that the hostapd executive we got in last step, which is a binary executive, IS the daemon. In an apt-get installation, the binary daemon executive is placed at /usr/sbin/hostapd, and a Linux service script is placed at /etc/init.d/hostapd - this is not a same file, though named the same.
If we have already compiled our customized (patched) daemon, we will have the binary executive now. However, we may want to start hostapd automatically at system start-up, that's where we need the service script. To get the service script, you can install hostapd with apt-get install first, keep the service script at /etc/init.d/hostapd somewhere, remove hostapd with apt-get remove or apt-get purge, and restore the script.
To use our customized hostapd, make sure you have already removed the one installed by apt-get. Put the binary file wherever you like, e.g. /usr/local/bin/hostapd. Note that the service script is configured to find the daemon binary at /usr/sbin/hostapd. Thus, we need to make a symbolic link at /usr/sbin/hostapd that refers to /usr/local/bin/hostapd. This can be achieve by executing this command:
sudo ln -s /usr/local/bin/hostapd /usr/sbin/hostapd
Now if you run hostapd -v you will see:
hostapd v2.2 for Realtek rtl871xdrv
User space daemon for IEEE 802.11 AP management,
IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator
Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi> and contributors
Where you can find "rtl871xdrv" in the output, which means that our patch is successful.
5. Modification on daemon configuration
This step will be trivial. Just replace the "driver=nl80211" line in hostapd.conf with
driver=rtl871xdrv
And append this line to the end of the same file:
ieee80211n=1
The patched hostapd will now work with Edimax EW-7811Un and other USB Wi-Fi adapters that use the Realtek RTL8188CUS chip. My working configuration file on my laptop looks like this:
interface=wlan4
driver=rtl871xdrv
ssid=PEPORunAPAtVAIO
hw_mode=g
channel=9
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_psk=
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
ieee80211n=1
6. Alternative network manager for Raspberry Pi
wicd-curses is a good alternative network manager for Raspberry Pi.
wicd-curses |
7. Power supply improvement for Raspberry Pi
There are two solutions if you are faced with a power supply problem with your Raspberry Pi.
- Use a better power adapter for your Raspberry Pi. It is recommended to get a USB power adapter with at least 2.0A output (or 10W capacity).
- Use a powered USB hub to power your Wi-Fi adapters separately
Sometimes, even if I read a much lower current intensity with a USB amp-meter, say 0.5A, the Raspberry Pi still seems to suffer from insufficient power supply.
V. Results
Now I have both my laptop and my Raspberry Pi hosting Wi-Fi access points.
Wi-Fi networks discovered on an iPod touch |
VI. References
[1] How to setup an Access Point mode Wi-Fi Hotspot? http://askubuntu.com/questions/180733/how-to-setup-an-access-point-mode-wi-fi-hotspot
[2] Private IPv4 address spaces. https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces
[3] How to Enable IP Forwarding in Linux. http://www.ducea.com/2006/08/01/how-to-enable-ip-forwarding-in-linux/
[4] IptablesHowTo. https://help.ubuntu.com/community/IptablesHowTo
[5] hostapd error “nl80211: Could not configure driver mode”. http://askubuntu.com/questions/472794/hostapd-error-nl80211-could-not-configure-driver-mode
[6] Wifi Access Point with Two Adapters. https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=89756&p=783127#p783127
[7] 解决Ubuntu 14.04 Realtek RTL8188CUS USB无线网卡不稳定问题. http://blog.chinaunix.net/uid-22883023-id-4392914.html
[8] Realtek driver for USB wlan cards: 8188C, 8192C. https://github.com/dz0ny/rt8192cu
[9] Hostapd driver for RTL8188{C|CU|CUS} wifi chips. https://github.com/pritambaral/hostapd-rtl871xdrv
[10] patch failed for hostapd-2.3. https://github.com/pritambaral/hostapd-rtl871xdrv/issues/2
The hostapd-rtl871xdrv repo has been updated to support the latest releases of hostapd
ReplyDeleteIs that hostpad-2.5?
DeleteI like this nice post. thnsk for shared....
ReplyDeletelinux hosting
best linux reseller hosting
Dear Charles,
ReplyDeleteCan you share a working SD card image with the compiled wifi driver for it?
I tried to compile but it always gives this error message:
make[1]: *** /lib/modules/4.19.75+/build: No such file or directory. Stop.
Thanks in advance!
I was reading some of your content on this website and I conceive this internet site is really informative ! Keep on putting up. https://secominteractivesystem.medium.com/
ReplyDeleteOn this page you can read my interests, write something special. Homepage günstig
ReplyDeleteAlso, with free web has you can just expect such space as your_site.host.com. Such space will barely influence the standing of your site decidedly; it will sooner hurt its picture, particularly in case it's an organization's site. website hosting pakistan
ReplyDeleteNice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share. price of biometric access control
ReplyDelete