Automatically setup network config ifcfg-eth0? After I connect a USB network adapter to the phone I can configure it using ifconfig:

ifconfig eth0 up 192.168.1.2


Problem is that eth0 is not aviable until the USB device is connected. How can store the network config or apply it as soon as it's mounted?

Or is there any way I can set the config in kernel build process?

I know this is normally defined in ifcfg-eth0 that can be found in /etc/sysconfig/network-scripts/ or /etc/network/interfaces but it looks like android does not have this. As we use netcfg in android I looked up the netcfg profiles manual but /etc/network.d/${profile_name} also does not exist.
I thing this will work:

In /init.rc add

service ethernet /eth0.sh


and create /eth0.sh

#!/system/bin/sh

netcfg eth0 up 192.168.0.2


Still got problems chainging the ramdisk but that should work.

See http://elinux.org/Android_Networking



I was not able to change the ramdisk so I made a simple app the does run the netcfg command after boot and after USB device is attached.

Here are the key points for this:

&lt;manifest ...
  &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  &lt;uses-feature android:name="android.hardware.usb.host" /&gt;
  &lt;uses-permission android:name="android.permission.USB_PERMISSION" /&gt;
  &lt;application ... &gt;
    &lt;receiver
        android:name=".MyBroadcastReceiver"
        android:enabled="true"
        android:exported="true" &gt;
        &lt;intent-filter&gt;
            &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
            &lt;action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /&gt;
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;
...


and the sudo function in the receiver:

public static void sudo(String...strings) {
    try{
        Process su = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

        for (String s : strings) {
            outputStream.writeBytes(s+"\n");
            outputStream.flush();
        }

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        try {
            su.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        outputStream.close();
    }catch(IOException e){
        e.printStackTrace();
    }
}