dazz100
Posts: 47
Joined: Mon Apr 17, 2017 4:54 am

How to tether a Headless Raspi Pi to the Internet with a USB cable via Android phone??

Sun Aug 01, 2021 10:07 am

Hi
I need to test connection to a remote headless Raspi Pi running Raspian Lite connected to my home via the internet. The remote device will initiate a reverse ssh tunnel back to my firewall using a dynamic DNS service to find the IP address. It is this tunnel that I want to test, which I can't fully test inside my network. My home network connects to the internet via a firewall. I only have one internet connection, so I can't test the remote Pi running somewhere out on the Internet.

The remote Pi is setup as a Wifi Access Point, so I can't use Wifi to connect to the phone. I need a wired USB connection to simulate a wired LAN.

I have found various instructions that are either wildly out of date, or don't do what I want. The closest match is here:
https://peppe8o.com/raspberry-pi-portab ... tethering/
but it may need some tweaks to do what I want.

I want to:
1. connect a mobile phone to the remote Raspberry Pi with a USB cable.
2. configure the USB connection so it looks and acts like a LAN with internet access.
3. all configuration needs to be at the command line.

I do not require wifi traffic on the Raspi wlan0 interface to get direct access to the Internet. It would be acceptable given that this is only for testing.

Looking at the link above, I have extracted the notes I think are almost what I need. Note that I have not worked with iptables before. The notes are quoted below.

Before setting traffic routing in RPI, we need to enable IP forwarding process:

Code: Select all

sudo nano /etc/sysctl.conf
Uncomment:

Code: Select all

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:

Code: Select all

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:

Code: Select all

sudo su -
iptables-save > /etc/iptables.rules
exit
sudo nano /etc/rc.local
Add following line before the final “Exit 0”:

Code: Select all

iptables-restore < /etc/iptables.rules
so that your rc.local should appear similar following one:

Code: Select all

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


Open /etc/dnsmasq.conf file with your terminal editor:

Code: Select all

sudo nano /etc/dnsmasq.conf
and add the following lines:

Code: Select all

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.

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:

Code: Select all

sudo systemctl unmask hostapd && sudo systemctl enable hostapd && sudo reboot

I suspect I would need to make some subtle changes to the above configuration to make it work the way I want.
Last edited by dazz100 on Thu Aug 05, 2021 11:53 am, edited 1 time in total.

dazz100
Posts: 47
Joined: Mon Apr 17, 2017 4:54 am

Re: How to connect a Headless Raspi Pi to the Internet with a USB cable via mobile phone??

Sun Aug 01, 2021 9:22 pm

Hi
This link to a really ancient video here: https://www.youtube.com/watch?v=x_yhJ_QBfaU
but this is what I want to do and how to do it.

dazz100
Posts: 47
Joined: Mon Apr 17, 2017 4:54 am

Re: How to connect a Headless Raspi Pi to the Internet with a USB cable via mobile phone??

Thu Aug 05, 2021 11:44 am

Hi
I connected my Android phone to the Raspberry Pi with a USB tether.
I configured the phone. Note data saver feature needs to be turned off.

When I plugged in the phone and enabled USB tether, the Raspberry, running Rasbian Lite, recognised the connection and allocated it the label usb0.
Running ifconfig showed that the usb0 interface was connected complete with dhcp ip address. Everything looks like it should work.

I have configured the Raspi as a Wifi access point. I can ssh to the Raspi with PuTTY. From the same laptop at the same time, I can ssh to the Raspi via Ethernet. The laptop is not connected to the Internet.

When I ping 8.8.8.8 I get unreachable.
When I ping -I usb0 8.8.8.8, I get valid returns.

My wifi is in the xxx.30.22.0/24 range.
My Lan is in the xxx.30.21.0/24 range
The dhcp address given from the phone is 198.162.42.45 (it will probably vary from day to day, phone to phone)

So the problem was that the default route remained with the static setup of the eth0 interface in the dhcpcd.conf file.

I looked at a number of posts but I fixed the problem by deleting the existing default route and adding a new one.
This command shows the route table. Look for "default"

Code: Select all

$ route 
This command deletes the existing default eth0 route.

Code: Select all

$ sudo ip route delete default via xxx.30.21.1
This command sets a new default route that goes fia the usb0 tether.

Code: Select all

$ sudo ip route add default via 198.162.42.1 dev usb0
This command returns the route used to get to Google.

Code: Select all

$ sudo ip route get 8.8.8.8
This command pings Google to check that all is working.

Code: Select all

$ ping 8.8.8.8


I did not have to flush any interfaces or do anything else to get it all working.
The above commands are not persistent. The setup will not survive a reboot. That was OK for what I was doing.

Return to “Advanced users”