Raspberry PI Portable Hotspot with Android USB tethering
Connecting devices from Android WiFi Hotspot fastly drains smartphones battery and can consume in a few days your mobile subscription traffic. Raspberry PI, connected to smartphones USB tethering, works greatly as WiFi hotspot and adds advanced features
In this tutorial I’m going to show you how to configure Raspberry PI to share Smartphones USB connectivity creating a WiFi Network.
A common Raspberry PI usage makes its ethernet port connected to old routers (without WiFi) to create an home Access Point. This works with boards including ethernet ports (RPI models B), but can’t be applied to all those boards (like RPI Zero W and PI 3 Model A+) which have an ideal form factor for portability.
Moreover, with increasing smart working needs, our smartphone usage as WiFi hotspot can drain mobile batteries while having WiFi hotspot always on.
Using a small Raspberry PI (like Raspberry PI Zero W) to share smartphone connectivity across a WiFi network (created in RPI) also increases you WLAN capabilities, as you can run on same single board computer also traffic blocking systemas (like Pi-Hole), Proxy capabilities (like Squid, Privoxy or TOR) or creating local services (like a git web portal or wordpress host).
What We Need
As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if continue with the project or remove them from the shopping cart. So, hardware will be only:
- Raspberry PI Zero W (including proper power supply or using a smartphone micro usb charger with at least 3A) or newer Raspberry PI Board. If you use a RPI Zero W, you could need also a usb to microusb adapter to connect smartphone with RPI Zero out port.
- high speed micro SD card (at least 16 GB, at least class 10)
- Smartphone with USB tethering function (available in quite all recent smartphones)
Check hardware prices with following links:
Wiring Diagram
Please find in following picture wiring diagram in case of Raspberry PI Zero W:

Step-by-Step Procedure
We’ll start preparing Operating system.
Then we’ll configure a static IP for wireless interface (wlan0), as it will be used from client devices as default gateway. All connection will be routed to usb0 interface, which is usb port connected to smartphone in tethering mode (you can see it with ipconfig terminal command when USB tethering is enabled).
Then routing and dhcp server will be configure to correctly drive traffic and assign dynamic IPs to clients. Finally, WiFI hotspot settings will complete our installation.
All of these setting can be performed with a classic headless installation, with Raspberry PI connected to your home WiFi by wpa_supplicant. Last command in this procedure will restart Raspberry PI telling it to not consider client wifi configuration, as it will become an Access Point.
Install OS and Packages
Start preparing Raspberry PI operating system. I suggest to install Raspberry PI OS Lite to have an headless, fast OS. You can also use Raspberry PI OS Desktop (in this case, you will use its internal terminal).
Make your OS up-t-date. From terminal:
sudo apt update -y && sudo apt upgrade -y
Install required packages:
sudo apt install hostapd dnsmasq
Configure Routing
Start defining a static configuration for Raspberry PI WiFi interface. Even if it can lead to misunderstandings, in linux the dhcpcd service identify for RPI its client IP configuration. From terminal, edit dhcpd configuration file:
sudo nano /etc/dhcpcd.conf
And append at the end the following:
interface wlan0
static ip_address=192.168.2.1/24
nohook wpa_supplicant
“nohook wpa_supplicant” will disable the usage of wpa supplicat configuration file from next reboot. “192.168.2.1” will become the IP address your Raspberry PI will use inside its WiFi LAN.
Before setting traffic routing in RPI, we need to enable IP forwarding process:
sudo nano /etc/sysctl.conf
Uncomment:
net.ipv4.ip_forward=1
All routing capabilities will be set with a single command line, thus allowing traffic from all Rasapberry PI external clients, which will consider usb0 interface as exit gateway:
sudo iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE
This configuration is similar to the one used for a ethernet-to-wifi RPI Access Point, just changing usb0 with eth0 (only for boards with cable connection and only if you want to share router ethernet internet connection instead of usb connection).
This routing command will run until reboot. To make it persistent, we need to export running iptables configuration to a file and configure RPI in restoring this ip routing settings at each reboot. “iptables-save” needs to be logged as root (sudo is not enought), so these steps will be performed with following:
sudo su -
iptables-save > /etc/iptables.rules
exit
sudo nano /etc/rc.local
Add following line before the final “Exit 0”:
iptables-restore < /etc/iptables.rules
so that your rc.local should appear similar following one:
....(omitted)....
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
iptables-restore < /etc/iptables.rules
exit 0
Configure DHCP and DNS
Start creating a backup of your config:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.backup
Open /etc/dnsmasq.conf file with your terminal editor:
sudo nano /etc/dnsmasq.conf
and add the following lines:
interface=wlan0
dhcp-range=192.168.2.2,192.168.2.100,255.255.255.0,24h
domain=wlan
address=/gw.wlan/192.168.2.1
This configuration will configure IP assigned to WiFi clients in a range in a local “192.168.2.x” subnet, where devices will get an IP ending with a number between 2 and 100. All devices will be advised to use 192.168.2.1 IP address (your RPI) as gateway.
Configure access point:
sudo nano /etc/hostapd/hostapd.conf
Add the following lines (changing country_code with your one; SSID and password with your favourite one):
country_code=US
interface=wlan0
ssid=RpiWiFi
hw_mode=g
channel=8
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=wifipassword
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
Connect Smartphone and Reboot Raspberry PI
If still not done, you can connect now your smartphone to Raspberry PI USB port. Enable USB tethering:
- Android Smartphones: this is usually available in Settings -> Connections -> Mobile Hotspot and Tethering
- Apple Smartphones: this is usually available in Settings -> Cellular (some iOS versions skip this step) -> Personal Hotspot
Finally, enable wireless access point and reboot Raspberry PI with new configuration by using following terminal command:
sudo systemctl unmask hostapd && sudo systemctl enable hostapd && sudo reboot
Enjoy!
I have run all the codes, I did not get any errors. But at last I was unable to get wifi from raspberry.
What to do?
Hi. Before starting, what Raspberry PI model are you using?
If you aren’t getting WiFI SSID available, we need to inspect hostapd service. You should start checking its status with following command:
sudo systemctl status hostapd.service
I am getting Failed to fetch… after sudo apt install hostapd dnsmasq
I am running a pi zero W with a OS lite. I am in the terminal on the pi connected to a monitor.
Any help?
Hi Dan. Failed to fetch errors usually depends on connection problems with repositories. This may be a both temporary problem from remote servers or user problems. Did your “sudo apt update” command worked?
After reading through many tutorials about this, and finally landing on this one. I now have my RPI3 (for some reason my Pi Zero W is not detected by cellphone but I digress), its outputting a wifi signal, and connecting via USB Tethering to a lte connection, and changing its TTL to 65. The last thing that I am having difficulty is adding a VPN connection to my home self hosted VPN.
I have the setup and connection to VPN working (on the RPI3 only) useing the PPTP-Linux setup and the “route add -net 0.0.0.0/0 dev ppp0” working from the RPI command line, but once I run this command, the RPI has full access to my home network, but none of the connected devices have any internet access anymore.
Im sure I am missing something in the IPTables area, but not sure what.
Thanks for all your help in advanced
Hi Aaron,
I can’t test this configuration right by now. Please, can you try to repeat this procedure by changing:
sudo iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE
with
sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
Of course you can use this command after that ppp0 connection is setup.
Hope this can help you
Thanks for the attempt but this did not work.
I tried just adding this rule after everything in the above tutorial is up and running, and after the pon homevpn command, (no connection to the internet)
Then thinking the 2 rules were conflicting I restarted the pi, and after everything is up and running. and the pon homevpn command, I cleared all iptable rules with ”
sudo iptables -F
sudo iptables -t nat -F
sudo iptables -X
”
and then added the -o ppp0 rule. same result.
Again thanks for the help, and the tutorial.
Excellent post, got this up and running on Pi. Going to use it as a portable hotspot in the car.
Just a tip if this isn’t connecting to the Internet, switch off your phone’s wifi. This happened on my Samsung Note 8. Once switched off all connected devices could connect to the internet via mobile data
Thank you for your tip, Keith!
Hi, I got the same error as Prabhuram.S.S above, and ran your recomended command to get the following results:
pi@raspberrypi:~ $ sudo systemctl status hostapd.service
● hostapd.service – Advanced IEEE 802.11 AP and IEEE 802.1X/WPA/WPA2/EAP Authent
Loaded: loaded (/lib/systemd/system/hostapd.service; enabled; vendor preset:
Active: active (running) since Thu 2021-12-30 15:48:37 GMT; 10min ago
Main PID: 589 (hostapd)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/hostapd.service
└─589 /usr/sbin/hostapd -B -P /run/hostapd.pid -B /etc/hostapd/hostap
Dec 30 15:48:37 raspberrypi systemd[1]: Starting Advanced IEEE 802.11 AP and IEE
Dec 30 15:48:37 raspberrypi hostapd[568]: Configuration file: /etc/hostapd/hosta
Dec 30 15:48:37 raspberrypi hostapd[568]: wlan0: interface state UNINITIALIZED->
Dec 30 15:48:37 raspberrypi systemd[1]: Started Advanced IEEE 802.11 AP and IEEE
lines 1-12/12 (END)
Many thanks for your article and help,
Ewan
Hi Ewan,
from your log I see the “wlan0: interface state UNINITIALIZED->”, which I suppose has been truncated. I’ve also checked in stackexchange posts that someone had a similar problem (it’s line is “wlan0: interface state UNINITIALIZED->ENABLED”) and he solved by setting static IP address for wlan0 also into /etc/networks/interfaces file, then rebooting.
Please can you try this too?
I’m sorry, but for the moment I can’t test this, but the wlan0 configuration there should be something like:
iface wlan0 inet static
address 192.168.2.1
netmask 255.255.255.0
gateway 192.168.1.1 #this should be your Android gateway IP address
Hi there, thanks for the writeup. I have followed the steps on my rpi4 and I have gotten this error.
Could you help me make sense of why this is occuring? Cheers.
hostapd.service – Access point and authentication server for Wi-Fi and Ethernet
Loaded: loaded (/lib/systemd/system/hostapd.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Tue 2022-01-04 06:30:15 GMT; 87ms ago
Docs: man:hostapd(8)
Process: 1478 ExecStart=/usr/sbin/hostapd -B -P /run/hostapd.pid -B $DAEMON_OPTS ${DAEMON_CONF} (code=exited, status=1/FAILURE)
CPU: 22ms
Jan 04 06:30:15 raspberrypi systemd[1]: hostapd.service: Control process exited, code=exited, status=1/FAILURE
Jan 04 06:30:15 raspberrypi systemd[1]: hostapd.service: Failed with result ‘exit-code’.
Jan 04 06:30:15 raspberrypi systemd[1]: Failed to start Access point and authentication server for Wi-Fi and Ethernet.
Hi Pooper,
in these days I’ll test everything again. Just to know, did you start from a fresh OS installation?
Thank you for this clear tutorial! I managed to get everything work, except that connected devices had the “No internet” problem. It turned out being an incomplete routing configuration, for which to solve I had to add the two following commands before the iptables-save step (as suggested in https://raspberrypi.stackexchange.com/questions/106871/share-usb-tethered-phone-to-raspberry-pi-4-lan-port ):
sudo iptables -A FORWARD -i usb0 -o wlan0 -m state –state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o usb0 -j ACCEPT
With this done everything works perfectly.
Thank you again for the tutorial!
Hi Axel,
testing the change for a few days. If no issues will occur, I’ll add these lines to my tutorial. Many thanks!
It works many thanks 🙂
How can i share usb-tethering not only to wlan but also to ethernet?
Hello Paul,
It should be quite simple, by adding the related iptable entry for every port routing you want to add and configuring the dnsmasq.conf for exposing dhcp service also the ethernet
“….also increases you WLAN capabilities, as you can run on same single board computer also traffic blocking systemas (like Pi-Hole), ….”
It is funny because this exact article detects I am using Pi-Hole in my network, says I need to turn off my ad blocker and is blocking me from reading it. Had to resort to disabling JS on the site to make it work.