Code and Stuff

Aug 11, 2011

Sharing Ethernet over WLAN (Buggy)

Requirements: A capable WIFI card, Ubuntu 11.04
Packages: hostapd, dhcp server

In an attempt to share my wired network to my android cell phone using wifi I found the following solution. It is still quite rough and not very stable, but fits my needs.

The solution is composed of 3 parts: hostapd, dhcp and a script.


Hostapd is a software to create an accesspoint with a wifi card (exactly what I need). The configuration is quite simple and well documented. The config I used is the following (/etc/hostapd/hostapd.conf):


You might have to change it a little to match your config (Especially the driver, interface, ssid and wpa_passphrase). You can get a list of the interfaces with ifconfig

DHCP server

Next thing is to setup the DHCP server. This will give devices an IP when they connect to the WIFI network. My config file (/etc/dhcp/dhcpd.conf) looks like this:
# listen on wlan0

ddns-update-style none;

# change this according to your needs
option domain-name-servers;

default-lease-time 600;
max-lease-time 7200;
log-facility local7;

subnet netmask {
  option broadcast-address;
  option routers;
You might have to change it a little to match your environment especially the DNS. I'm using the network for the Wireless network. The machine that is running the server has IP The wlan interface has to be configured to this IP, or you will get an error.

The script

The last part is the script that starts it all. I've placed it in the /usr/local/sbin folder and set my path to look in there for executables.

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# set the interface's IP
ifconfig wlan0 inet netmask

# start the AP
hostapd /etc/hostapd/hostapd.conf > /dev/null &

# restart the DHCP server
/etc/init.d/isc-dhcp-server restart

here as well you will have to change the IP and interfaces if needed. This script could also be linked from some runlevel folder to be started at boot (you probably have to add the start/stop cases.


One major problem of this approach is when the computer goes into sleep mode. I never checked what happens exactly, but looks like the interface goes down and hostapd isn't that happy about it.

Other solution

I read on various forums that ubuntu's NetworkManager should be able to share the LAN as well.

No comments: