Imagine that you went to another country, you have a smartphone by yourself and you really need to go to the Internet from it (for example, synchronize contacts or install some kind of software). What are the options?
- The first option is the mobile Internet. Unfortunately, it is not always easy and not always cheap.
- The second option is WIFI. You are lucky if you can connect to WIFI where you have arrived, in which case the problem has already been solved.
- The third option - LAN. What if we only have a LAN? The simplest thing is to take a small access point from home with you, but what if you didn’t take it with you or you don’t have it? In this case, a laptop or netbook accidentally captured with you will help you :)
We have
- EeePC 901 with Ubuntu 10.10
- Smartphone
- The local network
Task
- Get network access from a smartphone using WIFI
Decision
This solution is based on the fact that it is not advisable to have WIFI configured on the netbook in the access point mode, so it is implemented in the form of a script that includes this mode temporarily.
')
To solve the problem we need:
- hostapd - provides network card operation in access point mode
- dnsmasq - acts as a DHCP and DNS server
- install.sh and start.sh scripts to automate the process
The first script will help install the necessary packages.
install.sh#!/bin/bash # Install wifi hotspot daemon and dns/dhcp server sudo apt-get install hostapd dnsmasq # Stop installed services sudo service hostapd stop sudo service dnsmasq stop # Disable autostart on boot sudo update-rc.d hostapd disable sudo update-rc.d dnsmasq disable
And this is the minimum config for hostapd.
/etc/hostapd.conf # Define interface interface=wlan0 # Select driver driver=nl80211 # Set access point name ssid=laptop-wifi # Set access point harware mode to 802.11g hw_mode=g # Set WIFI channel (can be easily changed) channel=6 # Enable WPA2 only (1 for WPA, 2 for WPA2, 3 for WPA + WPA2) wpa=2 wpa_passphrase=wifipass
The minimum config for dnsmasq.
/etc/dnsmasq.conf # Bind to only one interface bind-interfaces # Choose interface for binding interface=wlan0 # Specify range of IP addresses for DHCP leasses dhcp-range=192.168.150.2,192.168.150.10
And the second script that puts the network card in the mode of the access point and vice versa.
start.sh #!/bin/bash # Start # Configure IP address for WLAN sudo ifconfig wlan0 192.168.150.1 # Start DHCP/DNS server sudo service dnsmasq restart # Enable routing sudo sysctl net.ipv4.ip_forward=1 # Enable NAT sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE # Run access point daemon sudo hostapd /etc/hostapd.conf # Stop # Disable NAT sudo iptables -D POSTROUTING -t nat -o eth0 -j MASQUERADE # Disable routing sudo sysctl net.ipv4.ip_forward=0 # Disable DHCP/DNS server sudo service dnsmasq stop
Using
To you probably already guessed, to use this solution is very simple. So preparation.
1. Run
install.sh .
2. Create a configuration file with our /etc/hostapd.conf parameters.
3. We substitute the installed configuration file /etc/dnsmasq.conf with ours, which is described above.
And now use! In order to switch the network card to the access point mode, you must:
1. Disable the Wireless Network by right-clicking on the Network Manager applet in the system tray.
2. Start the
start.sh script . Stopping the script is accompanied by disabling the access point server, as well as the DHCP / DNS server.
Result : you will have an access point with WPA2 authorization named laptop-wifi and wifipass password.
The solution was tested on
Ubuntu 10.10 , but can be easily adapted to another Ubuntu release or another distribution. In connection with the next trip abroad, an attempt was made to find ready-made software implementing this functionality, but it was not crowned with success. In connection with this, the above solution was born.
Enjoy your experiments!
Donations are welcome.
UPDATE :
- Updated script on GitHub
https://github.com/giner/helplinux/tree/master/scripts/wifi-host-ap