[XDA-University] GokulNC:
Here is a compilation of different ways to change/set the DNS Server in Android Devices

In this thread, I'll be setting the DNS Servers to 8.8.88 & 8.8.4.4 (Google DNS).

You can use any DNS server of your wish.

FOR NON-ROOTED DEVICES:

Method 1:

(by changing WiFi Settings)

Note: For some devices, this will work only if you can set a static IP.

1. Go Settings->WiFi

2. Long press the Network you're connected to, and tap 'Modify Network'.

3. Check 'Advanced'. (may not be required in some phones)

Switch the IP settings from DHCP to Static.

Add required DNS servers to the entries for DNS 1 and DNS 2.

4. Click 'Save'

(Don't worry about the editing the IP address as the Android device may fill in the IP address it has acquired through DHCP.)

[IMG="Google DNS"]http://xdaforums.com/attachment.php?attachmentid=3581709&stc=1&d=1450511657[/IMG]

Method 2:

(Easiest Method using app)

You could just use any app like:

1. Engelsiz : DNS Changer

2. DNS Changer (NO ROOT)

3. DNS Changer (no root 3G/WiFi)

4. DNSet

5. DNS Changer

(or any other app in Play Store.)

(Some apps may require root access)

FOR ROOTED DEVICES:

Easiest way:

The best apps I would recommend to do this automatically are:

1. Override DNS (Root)

2. Set DNS

3. DNS + DNSCrypt Manager

4. DNS Forwarder

Method 3:

(by editing DNS config)

1. Open the file 20-dns.conf from /etc/dhcpcd/dhcpcd-hooks/ using a Text Editor (I use ES File Explorer)

2. Add this line to the beggining & reboot device:

new_domain_name_servers="8.8.8.8 8.8.4.4 $new_domain_name_servers"

So, finally the script finally looks something like this:

##THIS CAN VARY slightly FOR DIFFERENT ROM BUILDS
# Set net.<iface>.dnsN properties that contain the
# DNS server addresses given by the DHCP server.

[B]new_domain_name_servers="[I]8.8.8.8[/I] [I]8.8.4.4[/I] $new_domain_name_servers"[/B]

if [[ $interface == p2p* ]]
then
intf=p2p
else
intf=$interface
fi

set_dns_props()
{
case "${new_domain_name_servers}" in
"") return 0;;
esac

count=1
for i in 1 2 3 4; do
setprop dhcp.${intf}.dns${i} ""
done

count=1
for dnsaddr in ${new_domain_name_servers}; do
setprop dhcp.${intf}.dns${count} ${dnsaddr}
count=$(($count + 1))
done

separator=" "
if [ -z "$new_domain_name" ]; then
separator=""
else
if [ -z "$new_domain_search" ]; then
separator=""
fi
fi
setprop dhcp.${interface}.domain "${new_domain_name}$separator${new_domain_search}"
}

unset_dns_props()
{
for i in 1 2 3 4; do
setprop dhcp.${intf}.dns${i} ""
done

setprop dhcp.${interface}.domain ""
}

case "${reason}" in
BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) set_dns_props;;
EXPIRE|FAIL|IPV4LL|RELEASE|STOP) unset_dns_props;;
esac

This is a permanent change. (& sets only for WiFi)

Remove that added line to undo the changes.

(Source: http://xdaforums.com/showpost.php?p=51650282&postcount=7 )

Method 4:

(by changing DNS properties using Terminal Emulator)

Note: This may not work in Android 4.4 and above devices.

1. Open Terminal Emulator and type:

getprop | grep dns

[IMG="Terminal: getprop | grep dns"]http://xdaforums.com/attachment.php?attachmentid=3580970&d=1450447628[/IMG]

2. Note down all the properties related to dns1, dns2.

These are the properties you need to set to change the DNS Server.

Use the following commands in Terminal Emulator to set them (using setprop):

su
setprop dhcp.wlan0.dns1 8.8.8.8
setprop dhcp.wlan0.dns2 8.8.4.4
setprop net.dns1 8.8.8.8
setprop net.dns2 8.8.4.4

Other optional properties for mobile networks (may vary with devices) (rmnet0 for 3G I guess)

setprop net.ppp0.dns1 8.8.8.8
setprop net.ppp0.dns2 8.8.4.4
setprop net.rmnet0.dns1 8.8.8.8
setprop net.rmnet0.dns2 8.8.4.4
setprop net.pdpbr1.dns1 8.8.8.8
setprop net.pdpbr1.dns2 8.8.4.4

Setting DNS for very older devices:

setprop ro.kernel.android.ndns 2
setprop dhcp.eth0.dns1 8.8.8.8
setprop dhcp.eth0.dns2 8.8.4.4

(You may similarly set dns3 & dns4, but that's not necessary)

These properties get reset to the original values upon reboot..

So, to keep these changes permanent, add the required commands to init.d

(Source: https://github.com/ukanth/afwall/wiki/DNS#changing-default-dns )

Method 5:

(by using iptables command)

1. Open Terminal Emulator and type the following:

su
iptables -t nat -A OUTPUT -p tcp --dport 53 -j DNAT --to-destination 8.8.8.8:53
iptables -t nat -A OUTPUT -p udp --dport 53 -j DNAT --to-destination 8.8.8.8:53
iptables -t nat -D OUTPUT -p tcp --dport 53 -j DNAT --to-destination 8.8.8.8:53 || true
iptables -t nat -D OUTPUT -p udp --dport 53 -j DNAT --to-destination 8.8.8.8:53 || true
iptables -t nat -I OUTPUT -p tcp --dport 53 -j DNAT --to-destination 8.8.8.8:53
iptables -t nat -I OUTPUT -p udp --dport 53 -j DNAT --to-destination 8.8.8.8:53

(you may also use this as AFWall+ Custom Script)

For more detailed info and better understanding, click here.

These properties get reset to the original values upon reboot..

So, to keep these changes permanent, add the required commands to init.d

Method 6:

(by editing resolv.conf)

Note:

I think this method works only for old devices. Nothing wrong in trying :)

1. Open resolv.conf from /system/etc/ using a Text Editor (I use ES File Explorer)

(If there's no such file, create it and try.)

2. Modify the nameservers in the file to your desired DNS servers.

The file finally looks like this:

nameserver 8.8.8.8
nameserver 8.8.4.4

This change is permanent..

So, it's better to backup the original resolv.conf before editing.

(Source: https://butterflydroid.wordpress.com/2011/10/19/how-to-set-dns-server-on-android-phone/ )

Method 7:

(by using ndc resolver command)

1. Open Terminal Emulator and type the following:

su
ndc resolver flushif [SIZE="1"][I]## to flush old DNS servers[/I][/SIZE]
ndc resolver flushdefaultif [SIZE="1"][I]## to flush resolver[/I][/SIZE]
ndc resolver setifdns <iface> <domains> <dns1> <dns2> ... [SIZE="1"][I]## Add the new servers[/I][/SIZE]
ndc resolver setdefaultif [SIZE="1"][I]## Set as the default device[/I][/SIZE]

This may vary for different Android Versions, so check this also..

These properties get reset to the original values upon reboot..

So, to keep these changes permanent, add the required commands to init.d

Method 8:

(by using DNSCrypt command)

Install DNSCrpt on your Android Device with this instructions here.

Then check this answer here on Android Stack Exchange on how to set DNS using a script.

Hit the 'Thanks!' button if you found this helpful.

Leave a reply if you need any help :)

Do leave a reply if any of these methods worked on your device...