termux-packages icon indicating copy to clipboard operation
termux-packages copied to clipboard

Replace resolv.conf usage with android default nameserver(s)?

Open fornwall opened this issue 6 years ago • 51 comments

From @rugk on August 5, 2016 9:25

Currently termux always uses the servers listed in /usr/etc/resolv.conf. However I'd like it to use the default android dns server. At least when there is no entry in the resolv file it should be used. Otherwise a special entry such as nameserver default would be useful to indicate that the default android name server should be used.

I mean in a mobile network it might of course be the same as configured in the file (google name server), but when connected to a wi-fi network this is different.

Copied from original issue: termux/termux-app#130

fornwall avatar Jul 28 '17 23:07 fornwall

Hm, I thought most apps use the android system dns server - only nmap and dnsutils uses $PREFIX/etc/resolv.conf. Does that seem to be correct?

fornwall avatar Jul 28 '17 23:07 fornwall

From @rugk on August 5, 2016 11:48

nslookup does also use the custom resolv.conf. (I only tested it with this utility)

However when some tools do it differently this is much more confusing. There should be a consistent behaviour, otherwise this complicates debugging and such things. So either should all Termux programs honour the users resolv.conf or they should not.

My initial request is still valid through: Apps honouring the custom resolv.conf should be able to use the built-in Android dns server. At least thee should be a consistent behaviour.

fornwall avatar Jul 28 '17 23:07 fornwall

Normally the libc implementation of the resolver uses /etc/resolv.conf, but that is not the case for the libc on Android. So having most program using resolv.conf is not possible without overriding the libc resolver, which is outside the scope here.

As for nmap and dnsutils (the latter which contains nslookup), perhaps they can be patched to use the system one either by default or as an option?

I guess the system name server can be obtained with getprop net.dns1 (and getprop net.dns2), could you try to temporarily write that to resolv.conf and see if it works as expected for you?

fornwall avatar Jul 28 '17 23:07 fornwall

From @rugk on August 5, 2016 13:46

As for nmap and dnsutils (the latter which contains nslookup), perhaps they can be patched to use the system one either by default or as an option?

Yeah, I am :+1: of "as an option". :smiley:

I guess the system name server can be obtained with getprop net.dns1 (and getprop net.dns2), could you try to temporarily write that to resolv.conf and see if it works as expected for you?

Nice, did not knew about "getprop". Is this Android-specific or is this something Termux adds as a console command?

So I can use "getprop net.dns1" to get the dns server and write it into the resolv.conf file. And of course this works. I mean I also do know the dns server Android currently uses so I can "hardcode" it into the resolv.conf file, but this is only a workaround and no generic solution for this issue. Unfortunately something like setting the nameserver to "nameserver net.dns1" or similar does not work...

fornwall avatar Jul 28 '17 23:07 fornwall

From @glitchphan on September 14, 2016 15:37

What about nameserver getprop net.dns1 ?

fornwall avatar Jul 28 '17 23:07 fornwall

From @rugk on September 14, 2016 17:59

No, this does not work either. In this case it shows a parsing error.

fornwall avatar Jul 28 '17 23:07 fornwall

From @jsainzvela on April 19, 2017 14:23

Just found out about Termux and I think it is awesome already! :)

I have the same problem as depicted in this topic, has anybody found a solution to overwrite the resolv.conf file with the getprop net.dns1 output? (not "hackish", an official one :) )

Thanks!

Jorge

fornwall avatar Jul 28 '17 23:07 fornwall

Some thoughts / questions.

  1. What is the Android standard resolver? Because neither /etc/resolv.conf nor /system/etc/resolv.conf exist on my system.

  2. I just discovered whilst poking around that there's a dnsmasq at the system level on my device (Samsung Tab A). Though it doesn't appear to be running.

  3. A start-up check (or on-network connect hook) to enter the appropriate values into $PREFIX/etc/resolv.conf would seem to be a good approach.

dredmorbius avatar Aug 02 '17 20:08 dredmorbius

getprop net.dns1 is not compatible with VPNs, it still shows system DNS. The right solution is to use getDnsServers though jni but it was introduced very recently. So in all it seems that using 8.8.8.8 for few packages that need DNS server name rather than just resolving through system libc is the best solution available

phcoder avatar Aug 27 '17 00:08 phcoder

I have tried a number of solutions seeking a better resolution to https://github.com/sdrausty/TermuxArch/issues/119 to no avail [edit: currently resolved], including getprop and find+grep in Android /system and /etc; So I have a couple of questions after trying to tackle this issue for a couple of hours today on Android 8:

  1. Is there a valid reason for making nameservers information difficult to find on Android?
  2. Where is the nameserver information stored?

SDRausty avatar Sep 09 '18 00:09 SDRausty

$ getprop net.dns1
49.45.0.1
$ getprop net.dns2
2405:200:800::1

I am using this for making resolv.conf in Alpine

Hax4us avatar Sep 09 '18 05:09 Hax4us

@Hax4us: it doesn't work with VPN. Right way it's to get it from Android network apis

phcoder avatar Sep 09 '18 05:09 phcoder

@Hax4us works well on Android < 8, but not on 8.

@phcoder what is the command?

SDRausty avatar Sep 09 '18 06:09 SDRausty

@Hax4us getprop net.dns1 and getprop net.dns2 returns empty fields on my device.

So I prefer to use one/two of the following DNS servers:

## Cloudflare
1.0.0.1
1.1.1.1
2606:4700:4700::1111
2606:4700:4700::1001

## Level 3
4.2.2.1
4.2.2.2

## Google
8.8.4.4
8.8.8.8
2001:4860:4860::8888
2001:4860:4860::8844

## OpenDNS
208.67.220.220
208.67.222.222

ghost avatar Sep 09 '18 09:09 ghost

getprop net.dns1 and getprop net.dns2 returns empty fields on my device

This was a behaviour change in Android 8.0 which hides/drop these:

The system properties net.dns1, net.dns2, net.dns3, and net.dns4 are no longer available, a change that improves privacy on the platform.

fornwall avatar Sep 09 '18 13:09 fornwall

To obtain networking information such as DNS servers, apps with the ACCESS_NETWORK_STATE permission can register a NetworkRequest or NetworkCallback object. These classes are available in Android 5.0 (API level 21) and higher.

Can this be queried from the CLI in Termux?

SDRausty avatar Sep 09 '18 14:09 SDRausty

@sdrausty No. This can be queried from Android application only.

ghost avatar Sep 09 '18 15:09 ghost

@xeffyr can a small apk or similar be made to run within Termux to accomplish reading from https://developer.android.com/about/versions/oreo/android-8.0-changes#o-pri:

To obtain networking information such as DNS servers, apps with the 
ACCESS_NETWORK_STATE permission can register a NetworkRequest 
or NetworkCallback object. These classes are available in Android 5.0 
(API level 21) and higher.

Similar to ../usr/libexec/termux-am/am.apk?

SDRausty avatar Sep 10 '18 00:09 SDRausty

Too much nuisance. I would just forge a Magisk module to get a /etc/resolv.conf.

y0umu avatar Sep 12 '18 12:09 y0umu

Too much nuisance

@y0umu it is just two api calls:

ACCESS_NETWORK_STATE 
 NetworkRequest
 NetworkCallback

SDRausty avatar Sep 12 '18 12:09 SDRausty

just forge a Magisk module to get a /etc/resolv.conf.

@y0umu do you really believe that https://github.com/topjohnwu/magisk-module-template can fulfill this request easily? How? Beginner steps requested.

SDRausty avatar Sep 12 '18 12:09 SDRausty

I would just forge a Magisk module

@y0umu Not all users have root/Magisk installed.

ghost avatar Sep 12 '18 12:09 ghost

have root/Magisk installed

Thanks for pointing out this small detail @xeffyr.

SDRausty avatar Sep 12 '18 12:09 SDRausty

just forge a Magisk module to get a /etc/resolv.conf.

@y0umu do you really believe that https://github.com/topjohnwu/magisk-module-template can fulfill this request easily? How? Beginner steps requested.

No it is not going to fulfill this request easily since not everyone would like to void the gurantee to have Magisk installed. As for the issue itself, the module I cooked myself merely satifies my needs in that Go applications now have smooth network connections with the presence of /etc/resolv.conf. But I have not tested others applications that faced with DNS lookup problems. I guess my module will work, but with no gurantee.

Too much nuisance

@y0umu it is just two api calls:

ACCESS_NETWORK_STATE 
 NetworkRequest
 NetworkCallback

Sorry for my zero knowledge on Android devloping...

y0umu avatar Sep 13 '18 01:09 y0umu

This confused me for a while, as I was using nslookup to confirm my DNS changes via Termux

vinniejames avatar Feb 20 '19 19:02 vinniejames

Would it be possible to expose nameserver info via termux:API? That might be a workable short-term solution.

MacGyverNL avatar Jun 06 '19 22:06 MacGyverNL

even more complicated: https://github.com/termux/termux-packages/issues/3909

on Termux, to resolve the "real IP" same with APPs', now I have to run

in=$@
t=$(/systerm/bin/ping -c1 -n -q -w2 $in 2>&1)
ip=$(echo $t | head -n1 | cut -d "(" -f2|cut -d ")" -f1)
echo "... resolved by system PING: $ip..."

the progress of this resolving must consider the records of /etc/hosts, in other non-termux-linux, I prefer getent, invalid here ...

BTW, command host checks no /etc/hosts and dig or nslookup do the same.

Justsoos avatar Jun 19 '19 06:06 Justsoos

Confused me as well as whenever I was trying to send some DNS queries my default name servers were statically set in $PREFIX/etc/resolv.conf regardless.

Would be good for it to use the configured DNS servers from within the network settings.

benlavender avatar Jul 15 '19 20:07 benlavender

Was there any progress/thoughts on this?

benlavender avatar Oct 19 '19 13:10 benlavender

Not sure if this helps, but i had a similar problem while running some binaries (relying on /etc/resolv.conf) using sshdroid. I tried:

  • chrooting into a temporary jail which has /etc/resolv.conf present (=lot of hassle)
  • remounting /system as read/write (=worked)

Basically I did:

$ mount -o remount,rw -t ext4 /dev/block/mtdblock8 /system  # rw means read/write
$ echo -e "nameserver $(getprop net.dns1)\nnameserver $(getprop net.dns2)" > /etc/resolv.conf

as a sidenote, this allowed me to install alpine linux v3.9 (armv7) in a chroot pretty easily as well

coderofsalvation avatar May 22 '20 07:05 coderofsalvation