Monday, September 5, 2011

How to change your Hostname in Android

DHCP Hostname
With GingerBread, some devices show up in a router's DHCP list as 'android-sdr4r55ed'
Old Ways:
You can change host name in Gingerbread:
 > go to Settings -> Applications -> Development -> Device hostname or
Settings -> Wireless & networks -> Bluetooth settings -> Device name

Using adb shell, then
 > hostname NAME or edit /proc/sys/kernel/hostname
    echo NAME > /proc/sys/kernel/hostname

Using adb shell with busybox installed
 > busybox hostname NAME 

Best Way:
This worked for my Droid X, .602+ Rooted was using adb shell, then:
getprop net.hostname
setprop net.hostname NAME

Since the above code gets reset after each reboot, here is what else
I have found:


after

editing net.hostname
># getprop net.hostname
android_430217a864834bd5
># setprop net.hostname NAME

after the reboot it reverts to android_

so, hook your phone up to your computer:

your computer > adb > phone

> adb shell

> su
 - always backup all files first
> cp /data/data/com.android.providers.settings/databases/settings.db 
/data/data/com.android.providers.settings/databases/settings.db.backup

> cp /data/data/com.android.providers.settings/databases/settings.db 
/mnt/sdcard/settings.db

> rm /data/data/com.android.providers.settings/databases/settings.db

now click disk storage on the usb to access the mnt on your own computer

your computer > access your sdcard

> file settings.db

Ex. settings.db: SQLite 3.x database, user version 57

> sqlite3 settings.db

to show databases:

sqlite> .databases
seq  name             file                                                      
---  ---------------  ------------------------------
0    main             /media/249A-11E0/z/settings.db

to show tables:

sqlite> .tables
android_metadata   bookmarks          system           
bluetooth_devices  secure 

to show the column names:

sqlite> .schema secure
CREATE TABLE secure (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT 
UNIQUE ON CONFLICT REPLACE,value TEXT);
CREATE INDEX secureIndex1 ON secure (name);

The column names go from left to right: _id, name, value.

now select the table, secure, this command with show everything:

sqlite> select * from secure;

Here I picked id = 24, this is where my id was.

sqlite> select * from secure where _id = "24";
24|android_id|
 - just another way
sqlite> select * from secure where name = "android_id";
24|android_id|
 - just another way
sqlite> select * from secure where value = "";
24|android_id|

We can easily change the "ID" value from the <16 digit id> 
to any number. Here I changed it to 0:

sqlite> update secure set value = 0 where _id = 24;

Type .exit in order to exit.

sqlite> .exit

On the USB notification change from storage to charge to mount your 
sdcard to your phone.
phone > sdcard

again
> adb shell

> su

> cp /mnt/sdcard/settings.db 
/data/data/com.android.providers.settings/databases/settings.db

Now simply reboot your phone. You should now see "android_0" as your 
new hostname. I know I didnt change the entire hostname, however I will 
continue to look into this.

16 comments:

  1. Hi,

    This (the setprop method) doesn't persist across reboots. Any idea how to make it do so?

    ReplyDelete
  2. Ok I've updated the post^ but its not perfect.

    ReplyDelete
  3. What's not perfect about it?

    (I really, REALLY appreciate this deep digging you've done.)

    ReplyDelete
  4. Thanks so much, have you tried it? Does it work for you?

    ReplyDelete
  5. Connect your device with usb debugging enabled and do this -

    adb remount
    adb pull /system/build.prop

    Now modify build.prop, add line net.hostname=

    save it

    adb push build.prop /system

    adb reboot

    This works, tried it on my own device!

    ReplyDelete
  6. @Nilesh Govindrajan

    Thanks for posting this as well!

    ReplyDelete
  7. Hey wow, this is the most detailed post I've found yet for this. I've tried the setprop method, and it worked until a reboot. So then I tried adding to build.prop:

    net.hostname=MyDamnPhone

    But it does not really take effect. It seems to be ignored. (MotoDROID x2 "daytona" w. stock ROM Android 2.3.5)

    If I could just find a pre-network boot script, I could add the setprop line to it. Any ideas?

    TIA,
    Nate

    ReplyDelete
  8. @Nate French
    I notice on some phones this can be different. I haven't tried messing around with this in a very long time. I eventually rooted my phone with Gingerbread and then flashed an unofficial CyanogenMod 9 build for Ice Cream Sandwich, which is rooted and it easily let's me change my hostname.

    ReplyDelete
  9. I pull the the build.prop but the command "net.hostname" could not be found

    ReplyDelete
  10. never mind I miss read the line "add" lol

    ReplyDelete
  11. Alright, it's good that you figured it out!

    ReplyDelete
  12. Thank you! :)
    I was able to change my phone host name editing directly the file: /system/build.prop
    After reboot the phone appear into the network using edited hostname.

    Phone: Xperia Neo (MT15i) - Android 4.0.4 (ICS)

    ReplyDelete
  13. No problem! Thanks for posting your device information!

    ReplyDelete
  14. Star the issue so that google makes this feature available:
    http://code.google.com/p/android/issues/detail?id=6111

    ReplyDelete
  15. This is one of my favorite blog because whenever i visit this blog found something interested and different,you are doing very well job,keep it up

    ReplyDelete
  16. The build.prop thing works on Lenovo P780.

    Thnx!

    ReplyDelete

Please make suggestions :)