Issue: You are traveling, and all you have access to is that crappy sort of Internet that requires you to hit a network login page via your laptop's web browser. You want to connect some other devices, maybe some IoT devices, so that you can keep up with your projects. There is no way to do that since they don't have a web browser. Or do you have access to some pay-as-you-go Internet that only allows one device?
Solution: A Raspberry Pi Zero W (or any Pi with WiFi).
Overview:
- The Raspberry Pi is connected to your computer's USB port and uses what's called a Remote Network Driver Interface Specification (RNDIS) adapter to create a network between your Pi and your computer over the USB.
- Your main computer's network connections are then shared, allowing another network connection to use its Internet connection.
- The Raspberry Pi is set up as a Wireless Access Point (WAP), bridging the Internet connection provided by the OTG
The fastest way to set this up is to flash a new Raspbian image on your SD card. Do not use NOOBS; make sure you get one of the current Raspbian images.
Once your card flashes, do not power up the Raspberry Pi. Use Headless Pi to enable SSH and then Enable OTG on your SD card.
Now, you can plug the SD card into your Pi and connect your Pi via an OTG adapter to your USB. I use this OTG adapter for the Raspberry Pi Zero W. Let it finish doing its initial boot for a few minutes, then SSH to raspberrypi.local and log in as pi.
Note: You may have to install Bonjour Print Services to enable name resolution to .local addresses.
On your PC/Laptop, go to Control Panel -> Network and Internet -> View Network Status and Tasks -> Change Adapter Settings. Here, you should see all of your network adapters. In theory, you should only have two enabled:
- Your current Internet connection (I assume it will show as Wireless)
- Your RNDIS connection
You can right-click and rename either if you wish.
When you have them named as you like:
- Right-click on your Wireless connection and select "Properies"
- Go to the "Sharing" tab and check the box that says "Allow other network users to connect through this computer's Internet connection
- Under "Home networking connection," select your RNDIS adapter
- Click "Ok," and you should be able to access the Internet from your Pi
To work as an access point, the Raspberry Pi must have access point software installed, along with DHCP server software to provide connecting devices with a network address.
We'll need DNSMasq and HostAPD to create an access point. Install all the required software in one go with this command:
sudo apt install dnsmasq hostapd
Since the configuration files are not ready yet, turn the new software off as follows:
sudo systemctl stop dnsmasq
sudo systemctl stop hostapd
We are configuring a standalone network to act as a server, so the Raspberry Pi needs to have a static IP address assigned to the wireless port. This documentation assumes that we are using the standard 192.168.4.x IP addresses for our wireless network so that we will assign the server the IP address 192.168.4.1. If this conflicts with anything, be sure to change the network. We also assume that the wireless device the Pi uses is wlan0.
To configure the static IP address, edit the dhcpcd configuration file with:
sudo nano /etc/dhcpcd.conf
Go to the end of the file and edit it so that it looks like the following:
interface wlan0
static ip_address=192.168.4.1/24
nohook wpa_supplicant
Now restart the dhcpcd daemon and set up the new wlan0 configuration:
sudo service dhcpcd restart
dnsmasq provides the DHCP service. By default, the configuration file contains a lot of information that is not needed, and it is easier to start from scratch. Rename this configuration file and edit a new one:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo nano /etc/dnsmasq.conf
Type or copy the following information into the dnsmasq configuration file and save it:
interface=wlan0
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
So, for wlan0, we will provide IP addresses between 192.168.4.2 and 192.168.4.20, with a lease time of 24 hours. If you provide DHCP service for other network devices (e.g., usb0.), you may have many more options for dnsmasq; see the dnsmasq documentation for more details.
Start dnsmasq to use the updated configuration:
sudo systemctl start dnsmasq
You need to edit the hostapd configuration file, located at /etc/hostapd/hostapd.conf, to add the various parameters for your wireless network. After the initial installation, this file will be new and empty.
sudo nano /etc/hostapd/hostapd.conf
Add the information below to the configuration file. This configuration assumes we are using channel 11, with a network name of MyPiZone, and a password thereisnospooon. Note that the name and password should not have quotes around them. The passphrase should be between 8 and 64 characters in length.
To use the 5 GHz bands, you can change the operations mode from hw_mode=g to hw_mode=a. Possible values for hw_mode are:
- a = IEEE 802.11a (5 GHz)
- b = IEEE 802.11b (2.4 GHz)
- g = IEEE 802.11g (2.4 GHz)
- ad = IEEE 802.11ad (60 GHz)
interface=wlan0
driver=nl80211
ssid=MyPiZone
hw_mode=g
channel=11
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=thereisnospoon
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
We now need to tell the system where to find this configuration file.
sudo nano /etc/default/hostapd
Find the line with #DAEMON_CONF, and replace it with this:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
Now enable and start hostapd:
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl start hostapd
Do a quick check of their status to ensure they are active and running:
sudo systemctl status hostapd
sudo systemctl status dnsmasq
Edit /etc/sysctl.conf and uncomment this line:
net.ipv4.ip_forward=1
Add a masquerade for outbound traffic on usb0
sudo iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE
Save the iptables rule.
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Edit /etc/rc.local and add this just above exit 0 to install these rules on boot.
iptables-restore < /etc/iptables.ipv4.nat
Reboot and ensure it still functions.
Using a wireless device, search for networks. The network SSID you specified in the hostapd configuration should now be present and accessible with the specified password.
If you enable SSH on the Raspberry Pi access point, you can connect to it from another Linux device or any system with SSH access as long as the pi account is available.
ssh pi@192.168.4.1
By this point, the Raspberry Pi is an access point, and other devices can associate with it. Associated devices can access the Raspberry Pi access point via its IP address for operations such as rsync, scp, or ssh.