Summary:
getprop net.hostname
(Android System Hostname, likely android_24412414 type name)
echo $HOSTNAME
(shell/terminal/env hostname, likely ro.product.device from build.prop)
cat /proc/sys/kernel/hostname
(Linux System Hostname, likely localhost)
head /etc/hosts
(Linux System Hostname, likely localhost)
hostname
(Command not always installed, if installed via busy box, will read /proc/sys/kernel/hostname)
Depends on which version of Android you have.
Android 4.0 and Above
Newer versions will use the right build.prop
entry. Well, that is to say, Android reads the build.prop
as it's normal boot process. Once the Connectivity Service runs, it checks to see if net.hostname is set in the Settings, then if empty, then it uses the "android_" + (permanent) android ID. So that's all you need to do.
Add net.hostname=HOSTNAMEHERE
to /system/build.prop
(Root may be needed). Leave a blank line at the end of the file.
Kitkat's ConnectivityService.java, notice the IF isEmpty statement:
// setup our unique device name
if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
String id = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ANDROID_ID);
if (id != null && id.length() > 0) {
String name = new String("android-").concat(id);
SystemProperties.set("net.hostname", name);
Android 2.3 and Below
Older versions, Gingerbread, Froyo, etc, just clobber it. It doesn't bother to check. It will set net.hostname to android_24412414... no matter what. So you will need to change it after that Connectivity Service has finished loading. Luckily, it only runs the startup once, not on every new connection. So we just need to run setprop net.hostname HOSTNAMEHERE
after that for it to stick. Unfortunately, it runs fairly late in the boot process, so you need something that will make the changes on boot_complete. Most likely an App that runs by itself.
Froyo's ConnectivityService.java:
// setup our unique device name
String id = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ANDROID_ID);
if (id != null && id.length() > 0) {
String name = new String("android_").concat(id);
SystemProperties.set("net.hostname", name);
But then there is another issue.
The net.hostname setting, is not copied to the entire system! If you use a console/terminal application, you might not get the same hostname. My phone, a Samsung Sidekick 4G (semi-Galaxy S based) has an init.rc which runs hostname localhost
, so all underneath commands will not see either your build.prop hostname or the android_id hostname! So you need to set that too.
hostname HOSTNAMEHERE
or echo HOSTNAMEHERE > /proc/sys/kernel/hostname
Note: Depending on how you are doing it, you'll need to run the above command as root in order for it to work. Init.rc is typically on the read only system partition, so root access is needed to edit it.
The dhcpcd command, which is run to get a dhcp address, will not update to a hostname provided by the server (That can be fixed by adding the right dhcpcd-run-hooks and dhcpcd.conf), and it will not provide a hostname to the dhcp server if $(hostname)
(The hostname command) is blank or localhost, and it does not internally use net.hostname. libnetutils is used for calling dhcpcd with -h "net.hostname", but if for some reason it decides not to call with -h, DHCPCD WILL STILL SEND THE SYSTEM HOSTNAME via gethostname(), as long as it's not "(none)", "localhost", or empty. On Stock Froyo anyway. Samsung screwed the pooch on some devices like mine. No hostname pass through, forced network time, etc.
My Solution for Gingerbread and below Samsung, Edit the DHCP Client Daemon config:
The last thing you can do is edit /etc/dhcpcd/dhcpcd.conf. If you add hostname YOURHOSTNAME
to the file, it will use that, unless a hostname is given on the commandline. This won't fix the android_234etc issue, but will if the hostname is localhost (My problem, since Samsung screwed libnetutils and localhost in init.rc). You will need Root, but this works across reboots. This won't work if dhcpcd is called with -h Hostname option.