Some links in this post may be affiliate links. We may get paid if you buy something or take an action after clicking one of these, but without addictional costs for you compared to direct buying.

Raspberry PI Portable Hotspot with Android USB tethering

RPI USB tethering featured image
0
(0)

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.

Raspberry PI Zero W

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 favorite e-commerce shopping cart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from the shopping cart. So, hardware will be only:

Check hardware prices with following links:

Amazon raspberry pi boards box
Amazon raspberry pi Zero W box
Amazon Micro SD box
Amazon Raspberry PI Power Supply box
Amazon Smartphone box

Wiring Diagram

Please find in following picture wiring diagram in case of Raspberry PI Zero W:

RPI USB tethering wiring

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!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

7 comments

  1. 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?

    1. 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

  2. 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?

    1. 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?

  3. 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

    1. 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

  4. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy