Wednesday, July 4, 2012

setting nameserver ip addresses on android

Almost all the android devices will be using dhcp service to get ip address and nameservers. But how to set custom nameservers on android devices? In this article we will see how. Is resolv.conf is there in android? if not? how?

From the command prompt :
run the following command to set the nameservers. We will use google's public nameserver ips 8.8.8.8 and 8.8.4.4 in this example.



#setprop net.dns1  8.8.8.8
#setprop net.dns2  8.8.4.4

NOTE:
in some post you can see like "setprop net.eth0.dns1" which is deprecated now.

You can also make this variable permanent by adding to default.prop file
vi default.prop
net.dns1 8.8.8.8
net.dns2 8.8.4.4

In mobile devices you can set the ips as follows:
Settings > Wireless & network settings > Wi-Fi settings > Advanced > Use static IP


The dhcp uses the following scipt in android devices. you can modify the script to use google's public dns also.
“/system/etc/dhcpcd/dhcpcd-hooks/20-dns.conf”


You can modify the script as 
-----------------------------------------------------------------
# Set net.<iface>.dnsN properties that contain the
# DNS server addresses given by the DHCP server.
set_dns_props()
{
    case "${new_domain_name_servers}" in
    "")   return 0;;
    esac
    count=1
    for i in 1 2 3 4; do
        setprop dhcp.${interface}.dns${i} ""
    done    
    count=1
    for dnsaddr in ${new_domain_name_servers}; do
        setprop dhcp.${interface}.dns${count} ${dnsaddr}
        count=$(($count + 1))
    done 
 setprop dhcp.eth0.dns1 8.8.8.8
 setprop dhcp.eth0.dns2 8.8.8.4
}
unset_dns_props()
{
    for i in 1 2 3 4; do
        setprop dhcp.${interface}.dns${i} ""
    done
}
case "${reason}" in
BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)       set_dns_props;;
EXPIRE|FAIL|IPV4LL|RELEASE|STOP)                unset_dns_props;;
esac
-----------------------------------------------------------------

Recommended Reading

1. Programming Android
2. Android for Programmers: An App-Driven Approach (Deitel Developer Series)
3. Professional Android 4 Application Development (Wrox Professional Guides)



1 comment:

  1. thank you Randeep...my internet on android is very fast now. My internet speed has been doubled!!!!

    you are the boss!!

    ReplyDelete