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).
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
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"