Autoconnect Android USB Tethering with udev rule

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
User avatar
drunkjedi
Posts: 882
Joined: Mon 25 May 2015, 02:50

Autoconnect Android USB Tethering with udev rule

#1 Post by drunkjedi »

This is my try which, well.... works.

I don't know any side-effects or more efficient way of doing it.
If you do Please tell and I will update this HowTo.
This is done in Fatdog, But should work in other puppies which have udev.

Problem:
Well not a problem, a small inconvenience.
Whenever I connect my android and switch On usb tethering on it, I have to select usb tethering option in Fatdog's control panel.
Which runs an script /usr/sbin/usb-tether.sh
Which presents an gui to select interface and activate network on it.
I wanted to automate it.

HowTo:

First connect your android to your pc by usb cable.
Switch On usb tethering in android settings.
Run ip link command in terminal it should give you interface name like usb? or enp?s??u?. (in place of ? mark there will be some numbers). On my system it's usb0.
Now using above info I can just run command dhcpcd usb0 (or busybox command udhcpc -i usb0) and my net will be On. But to automate it I have to write a udev rule. So....
Create a file /etc/udev/rules.d/81-android-tether.rules with the following content

Code: Select all

KERNEL=="usb0", SUBSYSTEM=="net", RUN+="/usr/sbin/dhcpcd usb0"
Replace the usb0 with what you found on your system
That's it, save your work, run command udevadm control --reload then run udevadm trigger or plug out your android and plugin again.
or simply restart the system.

User avatar
drunkjedi
Posts: 882
Joined: Mon 25 May 2015, 02:50

#2 Post by drunkjedi »

There's chance to modify the udev rule with more details from command

Code: Select all

udevadm info -a -p /sys/class/net/usb0
Like ATTRS{serial}=="8a16e391" (for my phone, your's maybe different) or something else, but I think it would be more specific and wouldn't connect other android device of mine automatically.

I have a almost 4 year old Oneplus One and a Xiaomi Note 4.

Both work fine with this method.

stemsee

#3 Post by stemsee »

I think this is very useful!

I will test it out.

thanks
stemsee

Post Reply