# Automatically run "dhclient ue0" when I turn on USB tethering on Android phone.



## colemakker (Jan 23, 2017)

Hi,

USB Tethering enables internet access over USB. I need to use `sudo` to run `dhclient ue0`, but I would rather log in as a user without sudo privileges for day to day use, so I would like to make it automatic. I understand I could do this with devd.conf

When I log in in single user mode and plug my phone in or turn on USB Tethering a message comes up-
ugen5.2: <MediaTek> at usbus5


```
$ sudo usbconfig list
...
ugen5.2: <VF-795 MediaTek> at usbus5, cfg=0 md=HOST spd=HIGH (480Mbps) pwr=ON (500mA)
```

I wasn't really sure what to do with the little information I have, here were my attempts in /etc/devd.conf


```
#attach 0 {
#   device-name "ugen5.2";
#   action "/etc/rc.d/dhclient ue0";
#};

#notify 0 {
#   match "system"       "USB";
#   match "subsystem"   "INTERFACE";
#   match "type"       "ATTACH";
#   action "/etc/rc.d/dhclient ue0";
#};

#attach 0 {
#   device-name "VF-795 MediaTek";
#   action "/etc/rc.d/dhclient ue0";
#};
```

Actually I tried to check if anything I did in devd.conf worked with the line 
	
	



```
action "mousepad /boot/loader.conf";
```
 but it never opened.

I expect it's pretty easy.
Thanks


----------



## Maxnix (Jan 23, 2017)

You can use an entry like this (Note that is /sbin/dhclient, not /etc/rc.d/dhclient):

```
attach 100 {
        match "vendor"  "0x<vendor_id>";
        match "product" "0x<product_id>";

        action "/sbin/dhclient ue0";
};
```
Here devd(8) will check for a device that has the specific vendor AND product id defined by the user, and then act accordingly.
However you can even have an entry in /etc/rc.conf for ue0:

```
ifconfig_ue0="SYNCDHCP"
```
So when your device will switch to USB tethering mode, the OS will bring up ue0 and run dhclient(8).

HTH


----------



## ASX (Jan 23, 2017)

Thanks, the devd based activation looks quite appealing for use with a laptop wifi switch.


----------



## colemakker (Jan 24, 2017)

Maxnix said:


> You can use an entry like this (Note that is /sbin/dhclient, not /etc/rc.d/dhclient):
> 
> ```
> attach 100 {
> ...


Thanks, can you tell me how I would find the vendor/product ids?


Maxnix said:


> However you can even have an entry in /etc/rc.conf for ue0:
> 
> 
> 
> ...


Thanks again, that worked immediately.


----------



## Phishfry (Jan 24, 2017)

colemakker said:


> can you tell me how I would find the vendor/product ids?



`usbconfig -u 5 -a 2 dump_device_desc`




ASX said:


> the devd based activation looks quite appealing for use with a laptop wifi switch.



I could see `devd` use for cellular modems that need usbmodeswitch as well.


----------



## colemakker (Jan 24, 2017)

Thanks people, the devd.conf method is working now too (much time lost to not realising a restart is required after editing devd.conf).

Can you tell me what the number after `attach` signifies? `attach 100`, `attach 0` and `attach 10` seem to behave similarly.

Another small issue - when I turn the connection off then on on my phone within a certain period of time (~20 seconds) my bandwidth monitor net/bmon adds 4GB to my transmitted and received totals ... I wondered if adding a detach rule would clear it up, if a `dhclient` detach command exists - I found in the templates in devd.conf -

```
# Try to start dhclient on Ethernet-like interfaces when the link comes
# up.  Only devices that are configured to support DHCP will actually
# run it.  [file]No link down rule exists because dhclient automatically exits
# when the link goes down.[/file]
#
notify 0 {
   match "system"       "IFNET";
   match "type"       "LINK_UP";
   media-type       "ethernet";
   action "/etc/rc.d/dhclient quietstart $subsystem";
};
```
Edit: same behaviour when using /etc/rc.conf `ifconfig_ue0="SYNCDHCP"` method.


----------



## Maxnix (Jan 24, 2017)

colemakker said:


> Can you tell me what the number after `attach` signifies? `attach 100`, `attach 0` and `attach 10` seem to behave similarly.


The number sets the priority of the rule. The higher, the first. In this way you prevent that others rule that can match your device would be choosen instead of your defined one.


Phishfry said:


> I could see `devd` use for cellular modems that need usbmodeswitch as well.


That's what I personally use.  devd(8) to switch, /etc/rc.conf to assign address with DHCP.


----------

