Understanding How DNS Works: Resolving Domain Names to IP Addresses
When you type abhaydixit.hashnode.dev
into your browser, the browser doesn’t inherently know where the website is hosted. Instead, it uses the Domain Name System (DNS) to find the corresponding IP address. DNS is essentially a database of key-value pairs that map domain names to IP addresses.
List of Common DNS Servers
DNS servers help resolve domain names into IP addresses. Here are some well-known DNS servers:
- Cloudflare →
1.1.1.1
- Google →
8.8.8.8
Your computer is configured to use a specific DNS server, which determines where DNS queries are sent.
The DNS Resolution Process
1. Request to the Root Server
When a DNS query is made, the request is first sent to a root server. There are 13 root servers worldwide, and their details can be checked at root-servers.org.
2. Top-Level Domain (TLD) Server
The root server determines the Top-Level Domain (TLD), such as .com
, .dev
, .org
, .edu
, and returns the corresponding IP address for the TLD’s root servers.
3. Query to the Authoritative Server
The request is then sent to the authoritative DNS server, which is typically managed by hosting providers like Google, GoDaddy, or Cloudflare. This server holds the actual IP address for the requested domain.
Understanding DNS Records
DNS records store information about a domain and its associated resources. Some common types include:
- A Record → Maps a domain name to an IPv4 address (e.g.,
192.168.1.1
). - CNAME Record → Used to alias one domain name to another (e.g.,
www.example.com
toexample.com
). - NS Record → Specifies the authoritative name servers for a domain.
Example:
@
→ Representshashnode.dev
.tutorial
→ Representstutorial.hashnode.dev
.A Record
→ The simplest DNS record type, mapping a domain name to an IP address.CNAME Record
→ Used for pointing subdomains to another domain.NS Record
→ Specifies the authoritative name server, which itself contains anA Record
mapping it to an IP.
Difference Between NS Record and A Record
- NS Record → Points to a name server.
- A Record → Provides the actual IP address.
Example:
abhaydixit.hashnode.dev NS ns.hostinger.com
This sets ns.hostinger.com
as the authoritative name server for abhaydixit.hashnode.dev
.
DNS and Networking Tools
DNS primarily works over the UDP protocol on port 53, which is a connectionless protocol.
Checking Network Interfaces
ifconfig
→ Lists all network interfaces (deprecated on some Linux versions).ip addr show
→ Alternative toifconfig
.
Editing the Hosts File
To manually map IP addresses to hostnames, modify the hosts file:
sudo vi /etc/hosts
Add an entry in the following format:
192.168.1.100 mypc.local
Now, mypc.local
can be used in networking commands.
Checking Open Ports and Processes
netstat -antp
→ Displays a list of all open ports.ss -tnlp
→ Shows process IDs and listening ports.nmap localhost
→ Scans for open ports on localhost.
DNS Query Testing
dig www.google.com
→ Checks if the DNS resolution works from your computer.dig @localhost www.google.com
→ Uses the localhost’s DNS resolver.
Conclusion
DNS is an essential system that translates domain names into IP addresses, enabling seamless web browsing. Understanding how DNS queries work and how to troubleshoot them using networking commands can be crucial for developers and network administrators alike.