- Resolve domain to another domain’s IP on Linux via local DNS
- 4 Answers 4
- How can I resolve a hostname to an IP address in a Bash script?
- Ping
- Nslookup
- Host
- Dig
- Bash script to resolve a hostname to an IP address
- About the author
- Karim Buzdar
- How do `/etc/hosts` and DNS work together to resolve hostnames to IP addresses?
- 3 Answers 3
Resolve domain to another domain’s IP on Linux via local DNS
I have a local domain name «SomeName» and an Internet domain «AnotherName.dyndns.org» whose IP can change at any time (it uses a dynamic DNS). I want to resolve «SomeName» to «AnotherName.dyndns.org«‘s IP. In other words, if «AnotherName.dyndns.org» points to 1.2.3.4, I want «SomeName» to point to that same IP, namely 1.2.3.4. Is there any easy and straightforward way to achieve this? I couldn’t find such an option for DNSMasq or BIND. I suppose one could write a script that gets the domain’s IP and rewrites the config file, then reloads the DNS server, but that would be so ugly. Why do I want this? To put it simply, I want to setup a Bacula server and client. The server has to resolve «SomeName» to a local IP (static, no problem here) and the client has to resolve it to a public IP (dynamic, namely «AnotherName.dyndns.org«).
4 Answers 4
If I understand correctly, what you want is a CNAME DNS record:
Thank you for your answer. Unfortunately, I tried for several hours to use something like SomeName IN CNAME AnotherName.dyndns.org in DNSMasq, BIND and Unbound but to no avail. Either I’m doing something terribly wrong or it isn’t possible. I’m guessing the former, but right now I think I’m better off just writing a bash script that updates /etc/hosts .
In bind, a major trip up is the use of a «.» at the end of a record. On the left hand side if you put domain.com this will be pre-pended to the zone’s main domain. if you put domain.com. then there is no pre-pending. This is not the case for all setups but many default bind config files work this way. When in doubt fully specify the domain names and end them with a «.».
How can I resolve a hostname to an IP address in a Bash script?
Every system in a TCP/IP network is assigned a unique identifier known as IP address that helps to connect it with other system o the network and all over the internet. All the websites you access on the internet also have unique IP addresses. As it is difficult for everyone to remember the IP addresses of these websites, the DNS system comes which helps to translate these hard to remember IP addresses into human-readable names. With DNS, you no longer have to remember the IP addresses. Instead, you have to just have to remember the domain name and all done. Actually, on the backed, the DNS server takes the hostname and resolves it to an IP address which the browser or application then connects to.
In this article, we will explain how to resolve a hostname/domain name to an IPv4 and IPv6 address in a Bash script. However, before proceeding towards creating the script, let us review some of the commands that can be used to resolve the hostname/domain name to an IP address.
Ping
Ping is the most simple and built-in tool that is available on almost all operating systems. It is used to verify the reachability of a host in a network. However, we can also used it to find the IP address against any hostname/domain name. Use the following syntax to find the IP address of a targeted hostname/domain name:
Nslookup
Nslookup is widely used to resolve the hostname to an IP address. In order to use this command for an IP lookup, use the following syntax:
Host
Another command-line utility “host” can be used to find IP address against any hostname/domain name. In order to use this command, use the following syntax:
Dig
Dig is another useful command line tool that is used to query various DNS related records. It can be used to find IP address against any hostname/domain name. Use Dig command in the following way to find an IP address against a specific hostname/domain name.
Bash script to resolve a hostname to an IP address
In order to use the bash script for an IP lookup, follow the below steps:
- Create a bash file using any text editor. Here I will be using the Nano editor to create a script named “iplookup.sh”.
# Specify DNS server
dnsserver = «8.8.8.8»
# function to get IP address
function get_ipaddr
ip_address = «»
# A and AAA record for IPv4 and IPv6, respectively
#
ip_address = «»
# A and AAA record for IPv4 and IPv6, respectively
# $1 stands for first argument
if [ -n «$1» ] ; then
hostname = » $ »
if [ -z «query_type» ] ; then
query_type = «A»
fi
# use host command for DNS lookup operations
host -t $ $ &>/ dev / null $
if [ «$?» -eq «0» ] ; then
# get ip address
ip_address = » $(host -t $ $ $| awk ‘/has.*address/’) »
else
exit 1
fi
else
exit 2
fi
# display ip
echo $ip_address
stands for first argument
if [ -n »
ip_address = «»
# A and AAA record for IPv4 and IPv6, respectively
# $1 stands for first argument
if [ -n «$1» ] ; then
hostname = » $ »
if [ -z «query_type» ] ; then
query_type = «A»
fi
# use host command for DNS lookup operations
host -t $ $ &>/ dev / null $
if [ «$?» -eq «0» ] ; then
# get ip address
ip_address = » $(host -t $ $ $| awk ‘/has.*address/’) »
else
exit 1
fi
else
exit 2
fi
# display ip
echo $ip_address
» ] ; then
hostname = » $ »
if [ -z «query_type» ] ; then
query_type = «A»
fi
# use host command for DNS lookup operations
host -t $ $ &>/ dev / null $
if [ «$?» -eq «0» ] ; then
# get ip address
ip_address = » $(host -t $ $ $
hostname = » $ »
for query in «A-IPv4» «AAAA-IPv6» ; do
query_type = » $(printf $query | cut -d- -f 1) »
ipversion = » $(printf $query | cut -d- -f 2) »
address = » $(get_ipaddr $) »
if [ «$?» -eq «0» ] ; then
if [ -n » $ » ] ; then
echo «The $ adress of the Hostname $ is: $address »
fi
else
echo «An error occurred»
fi
done
The output would be similar to this: Similarly, to resolve the IP address of “yahoo.com”, the command would be:
The output would be similar to this: That is all there is to it! In this article, we have learned to resolve the hostname to an IPv4 and IPv6 address using a bash script. We also learned some other command-line tools such as Ping, Nslookup, Host, and Dig that can be used to perform an IP lookup.
About the author
Karim Buzdar
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.
How do `/etc/hosts` and DNS work together to resolve hostnames to IP addresses?
Beware that many Web browsers implement their own DNS servers and DNS cache and do not consult any name-lookup mechanisms that have been configured on the system. In other words, some Web browsers completely ignore /etc/hosts and the locally-defined name servers. It’s quite confusing to witness the first time around. (Looking at you, Chromium-based browsers!)
@Christopher I was coming here to say the same thing. Related unix.stackexchange.com/questions/363498/…
@Christopher After I re-add the line to /etc/hosts, I can still connect to the website, even after refreshing the webpage. Why doesn’t /etc/hosts apply again, so that I can’t connect to the website? Is it because of DNS cache of Firefox?
@RuiFRibeiro This Chromium build seems to respect /etc/hosts and the system-defined DNS servers: (github.com/Eloston/ungoogled-chromium). Installation on macOS with Homebrew: brew cask install eloston-chromium .
3 Answers 3
This is dictated by the NSS (Name Service Switch) configuration i.e. /etc/nsswitch.conf file’s hosts directive. For example, on my system:
hosts: files mdns4_minimal [NOTFOUND=return] dns
Here, files refers to the /etc/hosts file, and dns refers to the DNS system. And as you can imagine whichever comes first wins.
Also, see man 5 nsswitch.conf to get more idea on this.
As an aside, to follow the NSS host resolution orderings, use getent with hosts as database e.g.:
Thanks. In my part 2, is it because my web browser’s DNS server does not work, but web browser’s DNS cache works?
How does systemd.resolver affects resolutions? Where do NIS and LDAP fit into the resolution system? What order follows a MacOS system or a Windows system?.
To answer just your last question: /etc/hosts doesn’t apply again immediately because firefox is caching the last hostname it got for google.com ; if you want it to always fetch it again, you’ll have to set network.dnsCacheExpiration to 0 in about:config . More info (though a bit outdated) here. Sorry if this is offtopic.
As a sidenote, many programs don’t use the standard resolver ( getaddrinfo(3) , getnameinfo(3) [1]) because it sucks.
First, the interface is not asynchronous; any moderately complex program will have to spawn a separate thread doing just the getaddrinfo() and then invent its own protocol to communicate with it (and let’s not even enter into getaddrinfo_a() , which is sending a signal upon completion, so it’s even worse).
Second, the resolver implementation in glibc (the standard C library in linux) is horrible, expecting you to let it pull random dynamic objects into the address space via dlopen() behind your back, and making it impossible to contain it in any way or use it in statically linked executables.
Since many programs don’t use the standard resolver directly, they also don’t bother to replicate its behavior exactly, and ignore some or all of /etc/resolv.conf , /etc/hosts , /etc/nsswitch.conf or /etc/gai.conf .
[1] and don’t even mention the non-reentrant, ipv4-only gethostbyname() , which was deprecated since ages.