Android usb tether linux

Is it possible to USB tether an android device using adb through the terminal?

I’m setting up some tests and it will require a decent number of phones to be usb tethered and configured. I’ve been successful in configuring them the way I want to once they have been tethered, however it would be quite tedious to tether the phones through navigating the menus, each and every time I (re)start my computer or move the test bank. I am currently using Nexus S phones running cyanogenmod v10.1.0, however the test bank will likely be Samsung Galaxy S4’s possibly mixed with the few Nexus S phones I have on hand. I want to do this as a bash script, but I’m trying to get it work at the command line (Ubuntu 13.04) first so as to remove issues that could come from scripting. I should be able to handle making it into a script myself, but if it’s simple to provide an answer as bash script, please do. I tried shelling into the device ( adb -s $deviceID shell ) and running:

setprop sys.usb.config rndis,adb 

This promptly kicks me out of the device shell and the device is no longer accessible. If I run an adb devices I see the phone as «. No Permissions» at which point I have to take the USB cable out and then plug it in again, and also restart the adb server with adb kill-server adb start-server . This will not work because I cannot access the phone to make the configuration changes that I need. I’ve googled around but have been unable to find anything fruitful. Any suggestions?

7 Answers 7

Must have root to change values with setprop , and I am on a Mac OS without a rndis driver so I could not test your method of USB tethering. Another way, if you have the connectivity service ( adb shell service list ):

The following commands call ConnectivityManager.setUsbTethering(boolean enable) in Android 4.3:

adb shell su -c service call connectivity 34 i32 1 turns on USB tethering.

adb shell su -c service call connectivity 34 i32 0 turns off USB tethering.

For other Android versions replace 34 with the following setUsbTethering calling codes per Android version:

4.4.4: 34 5.1.0: 30 6.0.1: 30 7.0.0: 33 

For reference, this only works on Android 4.x (more likely, only specific versions). The number «34» is the method number in the list of methods in IConnectivityManager.aidl

For reference, RNDIS seems to be the same as USB, but it depends on what device you are using. It was RNDIS for the nexus s and USB for the galaxy s4. Unfortunately I no longer work on this project so I can’t test your solution, but it seems that it would work so I have accepted your answer.

Читайте также:  Линукс восстанавливаем удаленные файлы

Commands in accepted answer not work on Oreo because now should be additional parameter callerPkg and if put there some random text it works.

int setUsbTethering(boolean enable, String callerPkg);

service call connectivity 34 i32 1 s16 text — turn USB tethering ON

service call connectivity 34 i32 0 s16 text — turn USB tethering OFF

It works for me Android Pie with

service call connectivity 33 i32 1 s16 text — turn USB tethering ON

service call connectivity 33 i32 0 s16 text — turn USB tethering OFF

You can also script the inputs to start the Settings app and tick the checkbox, like in https://github.com/medvid/android-tether/blob/master/tether#L83.

Here’s my script (pretty much the same as in the link, but slightly adapted):

adb shell am force-stop com.android.settings adb shell input keyevent 3 # Home sleep 2 adb shell am start -a android.intent.action.MAIN -n com.android.settings/.TetherSettings sleep 2 adb shell input keyevent 19 # Up adb shell input keyevent 20 # Down adb shell input keyevent 66 # Enter sleep 2 adb shell input keyevent 3 # Home 

For Windows, just replace sleep with timeout -t .

Works fine for my OnePlus 3T running Android Pie (9) (with Google’s Settings app (running the Pixel Experience ROM); can’t verify if it works with other Settings apps or not)

For Android 5.0+ (Lollipop, Marshmallow) use:

adb shell su -c service call connectivity 30 i32 1 to turn USB Tethering ON

adb shell su -c service call connectivity 30 i32 0 to turn USB Tethering OFF

Keep in mind that this requires root.

Use «service list» and «dumpsys activity services» in adb to get a overview of services your device is aware of. From here on its either Google or browsing through the (AOSP) source code since non of this is «officially» documented as far as i know.

The service method did not work for me on my Samsung device. I figured out how to do it by configuring the network interface directly, though. Here is a script that sets up a Linux machine and a USB-connected rooted Android device for USB tethering. This does not set up DNS or NAT masquerading, but is sufficient to make the device accessible at 192.168.42.129:

#!/bin/bash set -euo pipefail # Set up USB tethering for an Android device. # Usage: adb-usb-tether [USB-VENDOR USB-PRODUCT] # If USB vendor/product is unspecified, use first USB network interface. # On the Android side, tethering is enabled via adb shell. if [[ $# -eq 2 ]] then any=false vendor=$1 product=$2 else any=true fi function find_if() < local path if for path in /sys/class/net/* do if=$(basename "$path") if [[ "$(readlink "$path")" == */usb* ]] then local ifproduct ifvendor ifproduct=$(cat "$(realpath "$path")/../../../idProduct") ifvendor=$(cat "$(realpath "$path")/../../../idVendor") if $any || [[ "$ifproduct" == "$product" && "$ifvendor" == "$vendor" ]] then echo "Found interface: $if" 1>&2 echo "$if" return fi fi done > function adb_shell() < adb shell "$(printf " %q" "$@")" >function adb_su() < local quoted quoted="$(printf " %q" "$@")" adb shell su -c "$(printf %q "$quoted")" >if=$(find_if) if [[ -z "$if" ]] then echo "Requesting interface:" 1>&2 adb_su setprop sys.usb.config rndis,adb echo " >> OK" 1>&2 fi while [[ -z "$if" ]] do echo "Waiting for network device. " 1>&2 sleep 1 if=$(find_if) done while ! ( ip link | grep -qF "$if" ) do echo "Waiting for interface. " 1>&2 sleep 1 done function configure_net() < local name="$1" local if="$2" local ip="$3" local table="$4" local cmdq="$5" # Query command local cmdx="$6" # Configuration command if ! ( "$cmdq" ip addr show dev "$if" | grep -qF 192.168.42."$ip" ) then echo "Configuring $name interface address:" 1>&2 "$cmdx" ip addr add 192.168.42."$ip"/24 dev "$if" echo " >> OK" 1>&2 fi if ( "$cmdq" ip addr show dev "$if" | grep -qF 'state DOWN' ) then echo "Bringing $name interface up:" 1>&2 "$cmdx" ip link set dev "$if" up sleep 1 echo " >> OK" 1>&2 fi if ! ( "$cmdq" ip route show table "$table" | grep -qF "192.168.42.0/24 dev $if" ) then echo "Configuring $name route:" 1>&2 "$cmdx" ip route add table "$table" 192.168.42.0/24 dev "$if" echo " >> OK" 1>&2 fi > configure_net local "$if" 128 main command sudo configure_net device rndis0 129 local adb_shell adb_su 

Источник

Читайте также:  Visual studio code linux настройка

Android tethering

Tethering is a way to have internet access on your PC through your smartphone using its network connection. USB tethering and Wi-Fi access point tethering are natively supported since Android 2.2 «Froyo». USB #Reverse tethering, to have internet access on your smartphone through your PC, is also possible.

Wi-Fi access point

Using an Android phone as a Wi-Fi access point (to a 3G/4G mobile internet connection) is available for devices running Android 2.2 «Froyo» or newer.

Enable it via one of the following:

  • Settings > Wireless & networks > Internet tethering > Wi-Fi access point
  • Settings > More. > Tethering & mobile hotspot > Mobile Wi-Fi hotspot

Note: On some phones, this method will discharge the battery rapidly and tends to cause intense heating, unlike USB.

USB tethering

This article or section needs expansion.

Reason: Should have a new section for RNDIS networking, which mostly just works. (Discuss in Talk:Android tethering)

USB tethering is available since Android 2.2 «Froyo».

  • Connect the phone to your computer via USB (the USB connection mode — Phone Portal, Memory Card or Charge only — is not important, but please note that you will not be able to change the USB mode during tethering)
  • Enable the tethering option from your phone. This is usually done from one of:
    • Settings -> Wireless & networks -> Internet tethering (or Tethering & portable hotspot, for more recent versions)
    • Settings -> More. -> Tethering & mobile hotspot -> USB tethering

    Note: The network interface name may change depending on the USB port you use. You may want to change the interface name to create a unique name for your device regardless of the USB port.

    • If you are using a cellular data plan and you have recently entered a new billing period, you may need to restart your phone.

    Using systemd-networkd with udev

    Using systemd-networkd you can automatically adjust the networking to use the phone as the gateway when plugged in.

    /etc/udev/rules.d/90-android-tethering.rules
    # Execute pairing program when appropriate ACTION=="add|remove", SUBSYSTEM=="net", ATTR=="18d1" ENV=="rndis_host", SYMLINK+="android"

    You may have to adjust the idVendor attribute depending on your phone. You can check using udevadm:

    $ udevadm info /sys/class/net/enp0s26u1u2

    Then create the corresponding systemd-networkd file:

    /etc/systemd/network/50-enp0s26u1u2.network
    [Match] Name=enp0s26u1u2 [Network] DHCP=ipv4

    USB tethering with EasyTether

    Get the easytether linux client software. The commands to set it up and run it are as follows.

    # pacman -U easytether-0.8.5-2-x86_64.pkg.tar.xz # easytether-usb # dhcpcd tap-easytether

    Make sure you have the EasyTether android app installed on your phone for it to connect to. Note: The Lite app disables some connections and you must have the paid app for full functionality. For this reason, using the AziLink setup is recommended instead.

    Reverse tethering

    Reverse tethering is to provide internet connection to Android through PC. gnirehtet AUR provides reverse tethering.

    Connect your phone to your computer via USB like mentioned above and start:

    Tethering via Bluetooth

    Android (from at least 4.0 onwards, possibly earlier) can provide a Bluetooth personal-area network (PAN) in access point mode.

    NetworkManager can perform this action and handle the network initialisation itself; consult its documentation for more details.

    Alternatively: pair and ensure you can connect your computer and Android device, as described on Bluetooth, then, substituting the address of the Android device (here given as AA_BB_CC_DD_EE_FF ), do:

    $ dbus-send --system --type=method_call --dest=org.bluez /org/bluez/hci0/dev_AA_BB_CC_DD_EE_FF org.bluez.Network1.Connect string:'nap'

    This will create a network interface bnep0 . Finally, configure a network connection on this interface; Android offers DHCP by default.

    Tethering with SOCKS proxy

    With this method tethering is achieved by port forwarding from the phone to the PC. This is suitable only for browsing. For Firefox, you should set network.proxy.socks_remote_dns to true in about:config ( address bar )

    Tools needed

    Instructions

    Tetherbot

    Tetherbot is an experimental SOCKS proxy and Port Bouncer that should allow you to connect your laptop to the internet using the internet connection (EDGE, 3G or Wifi) of your T-Mobile G1 Cellphone. It is discontinued and its website is down, but still can be accessed from Wayback Machine[1] where its APK can also be downloaded from.

    In order to do SOCKS proxy via Tetherbot to connect your browser to the Internet, do:

    1. For your phone, open the application Tetherbot, decline Phone & Storage permissions, and press the Start Socks button
    2. Forward the port to your local computer:
    # adb forward tcp:1080 tcp:1080

    Note: Remember to disable these proxy settings in your web browser if you want to stop using your phone’s connection.

    Proxoid

    Follow the instructions demonstrated in the following link.

    Android Proxy Server

    Currently available on the Google Play store and supports HTTP/HTTPS, Socks5, Shadowsocks, and TCP Relay proxies.

    To initiate a SOCKS proxy to connect your browser to the Internet, do:

    1. Open the app Android Proxy Server, and enable the Socks5 Proxy ticker
    2. Forward the port to your local computer:
    # adb forward tcp:1088 tcp:1088

    Note: Remember to disable these proxy settings in your web browser if you want to stop using your phone’s connection.

    Источник

Оцените статью
Adblock
detector