How to ping a local network host by hostname? I recently wiped my phone and now I am unable to ping a machine by the hostname. Pinging by IP works.

I have a machine, let's say it is called NAS. From my Router and PC, ping NAS finds the machine. However, from my Android Phone, ping NAS returns ping: unknown host NAS.

I am rooted, so I went to /etc/hosts and added 192.168.1.### NAS and it was able to resolve NAS to an IP but what is the solution for making it ping all machines by hostname.

In my Wifi settings on the phone, DNS shows 3 entries: 8.8.8.8, 8.8.4.4, 192.168.1.1.
SHORT ANSWER:

You would be able to ping a local host by name only if your Wi-Fi router (or some other local host) is running a DHCP/DNS server which does know the name of the host you are trying to ping. This is a screenshot from my router:



Also make sure your Android device is sending DNS queries to local server and not on internet. Android loves Google so much, so you will often find queries going to 8.8.8.8 (may be when router is advertising the IPv6 DNS server) as it's in your case. 192.168.1.1 must be the first (and preferably only) DNS. Configure static IP in Wi-Fi settings or if you have root you can use Android kernel's built-in firewall netfilter to force a nameserver. Since DNS uses UDP port 53, do a Destination Network Address Translation (DNAT):

~# iptables -t nat -I OUTPUT -p udp --dport 53 -j DNAT --to 192.168.1.1:53


Non-root solution which works for both Wi-Fi and Mobile data is to use a VPN app like Virtual Hosts which intercepts DNS traffic and makes queries to configured upstream DNS server.
On Android Pie+, you need to set Private DNS off because by default DNS over TLS (DoT) sends encrypted DNS queries on port 853 which can't be interpreted by ordinary DNS servers.

For more details see How to configure DNS properly?



DETAILS:

WHAT IS DNS
Network hosts (anything connected to network including computers, phones, printers, routers etc.) identify each other with addresses (IPv4 most common these days). Hostname - which maps to an IP address - is relatively easy to remember for humans. When a host wants to access other host by name, hostname needs to be resolved first to the corresponding IP address. This name resolution was easy in early days particularly before internet when the networks were smaller. /etc/hosts file on each host used to contain the IP vs. hostname mapping of all hosts on a network, still valid today. When network grew in size, having thousands and then millions of hosts, it was no more possible to maintain hosts file of an ever-growing network, so Domain Name System was introduced.

In simple words DNS consists of servers - one or more hosts on a local or public network - which maintain a huge hosts database. So one can configure the DNS resolver on his/her host to contact a specific DNS server (which in turn may contact other servers recursively) when it needs to resolve a name. IP address of DNS server (nameserver) is usually provided by DHCP server or can be set manually. There are a number of popular public DNS like 1.1.1.1 (CloudFlare) and 8.8.8.8 (Google) etc. One can configure DNS resolver to use desired DNS e.g. from GUI settings or using /etc/resolv.conf file on Linux but Android doesn't offer a very straightforward way to achieve this. See How does Android OS do DNS name resolution? for details.

LOCAL DNS SERVER
So now if you want to ping a local host by name, you either need to add an entry to /etc/hosts file on your phone or there should be some DNS server on your local network. Actually there would be already one. Most of the routers offer the services of DHCP/DNS servers in addition to acting as gateways. Even when you create a hotspot on your phone (an it works as a simple router), it runs dnsmasq (up to Pie) as its DHCP/DNS server. But in order to resolve local hosts the DNS server should be a resolver, not just forwarder.

DYNAMIC DNS WITH DHCP
Next thing is how a local DNS server would know the hostnames of all hosts on network. This belongs to collaboration between DHCP client and server. DHCP communication occurs using different option codes. Code 12 is the one which lets client send its hostname to server (or may also request for a hostname) when asking for an IP address. Windows and Linux hosts are usually configured out of the box to send their hostnames to DHCP server (1) . Since Android 8 DHCPDISCOVER and DHCPREQUEST packets don't include hostname, so Android hosts can't be accessed by hostnames this way. See How do I change the name of my Android device?

DHCP server then forwards the assigned IP along with received/assigned hostname to DNS server so that to update its database (2, 3). A quote from a router's advanced configuration:


  Register DHCP leases in the DNS Resolver allows you to register DHCP static mappings. This, in turn, enables the resolving of host names that have been assigned IP addresses by the DHCP server.


Same is for dnsmasq:


  Machines which are configured by DHCP have their names automatically included in the DNS


An alternate solution could be to configure your DHCP server to assign static IP addresses and set hostname for each IP. See this answer for a little more details on this.

DNS ALTERNATIVES
DNS queries are unicast i.e. a host requests a DNS server for name resolution by calling the later explicitly by its IP address. However it's also possible to call hosts by name on a local network without using hosts file or a DNS server. This is done using multicast DNS (a zero-configuration protocol) in which each host on network acts as client as well as server. When a host sends mDNS query to all hosts on network at multicast address 224.0.0.251 where &lt;hostname&gt;.local is, the host with name hostname itself responds with its IP address, so the communication starts (4).
Android does have mDNS built-in but it works only if an app makes use of NSD APIs. For instance adbd force starts mdns service (5) so as to easily access an Android device (e.g. Raspberry Pi) from PC. See this answer for details.

There are also other name resolution solutions like NIS and LDAP/AD usually used on large private networks or NetBIOS used by Windows's file sharing. But Android's Bionic libc doesn't provide a phenomenon like NSS to prioritize NIS, LDAP or mDNS over DNS. Android's resolver only looks for hosts file before going for DNS (6). So you have to go for one of the two.