Introducing XDA:DevCon – A Conference For Developers By Developers
XDA Developers Android and Mobile Development Forum
Forgot your password?
 
Post Reply+
Tip us?
 
Jack_R1
Old
(Last edited by Jack_R1; 23rd September 2010 at 08:12 PM.)
#1  
Senior Member - OP
Thanks Meter 910
Posts: 4,255
Join Date: Aug 2009
Default [MOD] Change WiFi hostname - for custom ROMs (Sept 23)

This is useful for identifying phones on the local networks by looking at DHCP lease tables in the routers. It doesn't make your phone appear on Windows networks, since the phone needs to broadcast NetBIOS name for that. If you want your phone to show up on Windows networks (and share files) - you need Samba server, and JimmyChingala is working on one.

ROM developers can insert the option to customize hostname using the way described below in their Spare Parts options. Feel free to do so.

[SOLUTION]

The following shell command does the job of changing WiFi hostname:
Quote:
echo YOURHOSTNAME > /proc/sys/kernel/hostname
For the change to stay, it should be executed on each boot. And here the things start being more problematic.

For custom ROMs:
Most, if not all, custom ROMs include some user init shell script that will be executed on boot, making the solution easy.
Enter the following line in the Terminal / ADB shell:
Quote:
echo "echo YOURHOSTNAME > /proc/sys/kernel/hostname" >> the_path_and_name_of_userinit_script.sh
chmod 777 the_path_and_name_of_userinit_script.sh
Several examples of custom ROMs and their userinit scripts:
Suggested - will work for most ROMs (creates another file in directory of autoexecuted scripts): /etc/init.d/88hostnameinit
Additional possibility for Enomther's ROM: /data/local/userinit.sh
Additional possibility for CyanogenMOD: /sd-ext/userinit.sh


For stock ROMs:
There is no autorun script for stock ROMs, so they have to be added through modifying boot.img. It's a complicated procedure, and even though guides exist for it - I suggest not to mess with it only because of the hostname. The easiest solution would be to create a script file with the line above using Gscript or other scripting solutions, and execute it after each reboot. If anyone really wishes to modify boot.img - I assume that he/she knows enough about Linux/Android since it can be relatively easily done only on Linux, can find the necessary guides with some googling (like I did), and in this case the modification is easiest to do directly in init.rc - changing "hostname localhost" to "hostname name_of_your_choice".
 
Jack_R1
Old
(Last edited by Jack_R1; 23rd September 2010 at 10:28 AM.)
#2  
Senior Member - OP
Thanks Meter 910
Posts: 4,255
Join Date: Aug 2009
[ORIGINAL POST]
Hi people,

I'm not much of a dev, but I can find my way around with a bit of Google search And sorry about the links that don't link, new user's permissions don't allow me to...

Anyway, after messing with my router today I've noticed that Nexus transmits "localhost" as its host name to DHCP server, causing my DD-WRT to show it as "*". I went to Google and to my surprise, discovered that there isn't such an option in any Nexus ROM yet.

Found this: LINK_www_laslow_net_?p=501
Quote:
To change your hostname on Cyanogen 5.x, add the following line to the bottom of /system/etc/init.d/01sysctl -- and make sure you make a backup of 01sysctl before editing it!

echo NEWHOSTNAME > /proc/sys/kernel/hostname
I tested it, and it didn't work. After booting, the file still read "localhost" in it, and the hostname on DHCP server reflected it.

But, I didn't get frustrated, connected with ADB, manually executed the command:
echo MyHostName > /proc/sys/kernel/hostname
checked that the file was overwritten, disabled WiFi, deleted DHCP lease, enabled WiFi back - and voila, I have a new hostname!

Then I went to search for hostname setting, which got me to /init.rc:
Quote:
on boot
# basic network init
ifup lo
hostname localhost
domainname localdomain
Well, I guess that's the place. A tiny problem, though - it's in the boot image, which can't be easily modified. Thanks to the latest thread on update.zip creation I can probably do it myself, but I wanted to share the findings and ask for the correct way to implement.


There's a "dirty but functional" way of "disable WiFi - override /proc/sys/kernel/hostname - enable WiFi", and it's probably not a problem to stick it somewhere in the boot sequence, or even write an app that writes those changes to one of the boot scripts and allows configuration of host and domain names. But it's not the best way - DHCP might already give out a lease, and the new host name might not register.


And there's a correct (?) way of doing it, introducing it into init.rc. Since it's "on boot", I suppose that it runs after mounting the partitions - which means that the partitions are already accessible.
In this case, the best way would be executing a small shell script that would check for existence of, say, "/system/etc/settinghostdomainnames.rc" and create a default one if it's not there, then use "import /system/etc/settinghostdomainnames.rc" and set a manual trigger, like the guy is trying to do here:

LINK_groups.google.co.jp_group_android-developers_browse_thread_thread_e2f432707b735ff0

"trigger someeventtobringupnetworkinterface"
That would allow to use a custom setting for host and domain names that can be changed by SW, and adding that as another option into ROM Settings app or external app.
But the guy in question didn't succeed. What did he miss? Would it be better to do something like "on fakesystemproperty=something" and instead of manual trigger, doing "setprop fakesystemproperty something"?


I can probably test it and find out myself, but it would take loads of time compared to one of the kernel devs, and I don't even have the environment set up for modifying boot images. I was kinda hoping that one of the kernel devs would test it. I can write and post the modifications to init.rc and the custom script, they're very simple.

So, who can help me with answering the questions in the thread, and/or testing the modification?
 
Jack_R1
Old
#3  
Senior Member - OP
Thanks Meter 910
Posts: 4,255
Join Date: Aug 2009
Thanks! It's back.
Oh well, I'll keep preparing Ubuntu VM anyway
 
Jack_R1
Old
#4  
Senior Member - OP
Thanks Meter 910
Posts: 4,255
Join Date: Aug 2009
OK, first test fired - updated /init.rc in my own boot.img, checked the values. It's working, hostname is indeed modified.

Now I'll try to rewrite /init.rc in such way as to load the hostname setting from elsewhere, while not screwing the security. Will post results soon.
 
persiansown
Old
#5  
Recognized Developer
Thanks Meter 715
Posts: 851
Join Date: Jan 2007
Setting it to the same value as the BT value would be ideal. I'm not sure how you could do that though, because the init scripts run before the frameworks load
I do CyanogenMod stuff.

Circle me on Google+
 
Alclub
Old
#6  
Junior Member
Thanks Meter 0
Posts: 23
Join Date: Feb 2010
Looking forward for a fix to this problem.
 
Jack_R1
Old
#7  
Senior Member - OP
Thanks Meter 910
Posts: 4,255
Join Date: Aug 2009
Update, but only partially on topic:

God, I hate SH scripting. Couldn't even google a normal tutorial that would explain where I went wrong. A script of 10 lines, and I can't make it work.

Let's see, I need something like this:

#!/system/bin/sh

echo "on service-exited-network_prepare" > /system/etc/net_init.rc
echo " ifup lo" >> /system/etc/net_init.rc
if [ -e "/system/etc/net_init.domain" ];
then
echo "hostname `cat /system/etc/net_init.host`" >> /system/etc/net_init.rc
else
echo "hostname localhost" >> /system/etc/net_init.rc
fi
if [ -e "/system/etc/net_init.domain" ];
then
echo "domainname `cat /system/etc/net_init.domain`" >> /system/etc/net_init.rc
else
echo "domainname localdomain" >> /system/etc/net_init.rc
fi
echo >> /system/etc/net_init.rc

Of course, this thing fails miserably with -
Syntax error: end of file unexpected (expecting "then")

What the hell am I doing wrong? Never used SH before, mostly tcsh and perl.
Thanks.
 
Jack_R1
Old
#8  
Senior Member - OP
Thanks Meter 910
Posts: 4,255
Join Date: Aug 2009
Oh well, I guess I got the problem.. Unix vs Windows file format. Sorry for bothering.
 
iamwhoamnot
Old
#9  
Member
Thanks Meter 0
Posts: 57
Join Date: Feb 2010
its always bothered me that you cant change the device name for wifi networks, and ive always looked for a way to change it.

kudos to you for the ambition and diligence to do it!

dont give up, if you can get it smoothed out enough im sure cyanogen will implement it in his next mod. ive always wished there was an option in wifi settings to change device name. itd be very useful for lan ip configuring and when your connected to a random hotspot lol

Treo 650 - Dead
Blackjack 2 - Retired
Blackberry Pearl - Sold for G1
G1 - Backup
Iphone 3g - Sold for Nexus one
Nexus One - Best phone ive ever had.
 
Jack_R1
Old
#10  
Senior Member - OP
Thanks Meter 910
Posts: 4,255
Join Date: Aug 2009
Ok, after fighting for a day, I still didn't manage to import another .RC file and run on service exit (I don't even see the trace of the process I'm trying to start - the first thing it does is attempting to write log, and there is no log, no matter where I put the start command), but at least for a "quick-and-dirty way" there's a very simple solution, given SD-EXT partition (I believe everyone creates it):

open terminal application, type the following command:

echo "echo YOURHOSTNAME > /proc/sys/kernel/hostname" > /sd-ext/userinit.sh

That would override the hostname of the system before boot completion.

 
Post Reply+
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Go to top of page...

XDA PORTAL POSTS

Forums Added for the LG Optimus G Pro and Sony Xperia Tablet Z

Not too long ago, we talked about how Sony was continuing its AOSP efforts on … more

Permanent Screen Off for the Visually Impaired

Most of us take the simple act of waking up our devices and having a quick glance at the … more

App Analytics, or the Death of the Independent App Developer

This is entry number one in a series of articles about tools for app developers, … more

Serajr Power Toggles Brings Settings to the Small App World

Ever since the introduction of multi-window with things like Cornerstone and … more