Skip to content

Instantly share code, notes, and snippets.

@zonia3000
Last active January 5, 2024 16:22
Show Gist options
  • Save zonia3000/80674284c27d29f71d37722793c0665b to your computer and use it in GitHub Desktop.
Save zonia3000/80674284c27d29f71d37722793c0665b to your computer and use it in GitHub Desktop.
Android USB tethering with static IP address (fix broken tethering)

Android USB tethering with static IP address (fix broken tethering)

On the custom ROM I'm using (James Rom D500) there is something broken with DHCP setup, so, even if the USB tethering is active from phone settings, the computer will never receive a valid IP address.

These steps worked for me for setting up a static IP address:

Laptop configuration (from GUI)

  • Address: 192.168.42.121
  • Netmask: 255.255.255.0
  • Gateway: 192.168.42.129
  • DNS Servers: 8.8.8.8
  • Search Domains: 8.8.4.4

Notice that gatway IP (192.168.42.129) is hardcoded in Android code.

Phone configuration

Phone is rooted, shell access using adb shell:

ip link set dev usb0 up
iptables -I POSTROUTING 1 -s 192.168.42.0/24 -j MASQUERADE -t nat
echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all
echo 1 > /proc/sys/net/ipv4/ip_forward

The second command is necessary to let the packets go from the usb0 interface to the interface connected to the Internet (rmnet0 in my case). To see all the interfaces run ip addr show.

The 3rd command is not mandatory but it is useful for debugging (otherwise ping will not work).

Other notes

To debug any network issue I suggest to install tcpdump on the phone:

https://www.androidtcpdump.com/android-tcpdump/downloads

adb push ~/Downloads/tcpdump /system/xbin/tcpdump 

Then, from the phone: tcpdump -i usb0

Other workaround to bypass broken tethering

If nothing is working you can install a proxy server on the phone.

Then you can use adb to forward the proxy port to your computer, e.g.:

adb forward tcp:7000 tcp:7000

And you can configure your browser to use localhost:7000 as a proxy (works also for https).

For a lot of command line tools (e.g. curl), setting this environment variable is enough:

export http_proxy="http://localhost:7000"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment