DNS
DNS Resolution process
- DNS resolver look in its DNS cache
- DNS resolver breaks
iduoad.comto [.,com.,iduoad.com.] - The DNS resolution start at
.which is called root domain. Its ip addresses are already know to the DNS resolver. => returns(address of the authoritative nameserver of.) - DNS resolver queries the root domain nameserver to find the DNS servers to respond with details on
com.. => returns(address of the authoritative nameserver ofcom.) - DNS resolver queries the
com.authoritative nameserver to get authoritative nameserver foriduoad.com. - DNS resolver queries the authoritative nameserver for
iduoad.comand gets the latter's IP address.
A DNS request using dig utility:
# To visualize the entire process we run the following command
dig +trace iduoad.com
A DNS response looks like the following:
iduoad.com. 1799 IN CNAME iduoad.netlify.app.
# REQUEST TTL(for cache) IN Query TYPE Response
DNS and Layer 4 protocols
Multiplexing/Demultiplexing and UDP in linux
- Multiplexing: When a client makes a DNS request, after filling the necessary application payload, it passes the payload to the kernel via
sendtosystem call. - Demultiplexing: When the kernel on server side receives the packet, it checks the port number and queues the packet to the application buffer of the DNS server process which makes a
recvfromsystem call and reads the packet. - UDP is one of the simplest transport layer protocol and it does only multiplexing and demultiplexing. Another common transport layer protocol TCP does a bunch of other things like reliable communication, flow control and congestion control...
TCP/UDP throughput and Kernel buffer size
- If the underlying network is slow, and the UDP layer can't queue packets down to the Network Layer.
sendtosyscall will hang until the kernel finds some of its buffer freed up. Increasing write memory buffer values usingsysctlvariablesnet.core.wmem_maxandnet.core.wmem_defaultprovides some cushion to the application from the slow network. - Same thing happens in the server side. If the receiver process is slow (slower than the Kernel), the kernel has to drop packets which can't queue due to the buffer being full. Since UDP doesn’t guarantee reliability these dropped packets can cause data loss unless tracked by the application layer. Increasing
sysctlvariablesrmem_defaultandrmem_maxcan provide some cushion to slow applications from fast senders.
DNS Resolution in Linux
- When we head into a website. The browser first looks if the domain is already stored in its DNS cache.
- If the domain name does not exist in the browser's DNS cache, the browser calls the
gethostbynamesyscall. - Linux looks in
/etc/nsswitch.confto know the order it will follow when trying to resolve the domain name to the ip address. - Let's say the NSS file contains the following entry
hosts: files dns. - The OS will look in
/etc/hostsfile first for match of the domain name. - If none is found in the hosts file, it will use
nss-dnsplugin to make a DNS request to the DNS resolvers listed in/etc/resolv.conf(in order from top to bottom).
The DNS resolvers are populated by DHCP or statically configured by an administrator.
nsswitch.conf file
The /etc/nsswitch.conf file is used to configure which services are to be used to determine information such as hostnames, password files, and group files.
An example of the /etc/nsswitch.conf
# Name Service Switch configuration file.
# See nsswitch.conf(5) for details.
passwd: files systemd
group: files [SUCCESS=merge] systemd
shadow: files systemd
gshadow: files systemd
publickey: files
hosts: mymachines resolve [!UNAVAIL=return] files myhostname dns
networks: files
protocols: files
services: files
ethers: files
rpc: files
netgroup: files
The syntax is the following:
database_name: (service_specifications...[STATUS=ACTION])
database_name: is the database name we will be looking for.service_specification: where we'll be looking. Depend on the presence of shared libraries. (e.gfiles,db,ldap,winbind...)STATUS: a resulting status for service_specification if it occursACTIONis taken.
In the previous example:
- for
passwd,group,shadowandgshadowthe system will look in the files first then it will fallback to systemd. - for
groupif the lookup in the files succeeds, the processing will continue to systemd and will merge the member list of the already found groups will be merged together. - for
hostsit will usemymachinesplugin, thenresolve. Ifresolveis available it will return (stop the lookup) otherwise it will continue tofiles,myhostnameand finallydns. - for other services it will use
files.
NSS Plugins
There are many NSS (Name Service Switch) plugins that are used to resolve names to ips. Here are some examples:
- nss-mymachines: provides hostname resolution for the names of containers running locally that are registered with
systemd-machined.service. - nss-myhostname: provides hostname resolution for the locally configured system hostname as returned by
gethostname. - nss-resolve: resolves hostnames via the
systemd-resolvedlocal network name resolution service. It replaces thenss-dnsplug-in module that traditionally resolves hostnames via DNS.
iduoad.com. 1799 IN CNAME iduoad.netlify.app.
# REQUEST TTL(for cache) IN Query TYPE Response
Linux DNS utilities: dig vs nslookup
diguses the OS resolver libraries.nslookupuses is own internal ones.- Internet Systems Consortium (ISC) has been trying to get people to stop using
nslookup. nslookupwas considered deprecated until BIND 9.9.0a3 release.- Source in StackOverflow thread #❔
DNS applications
- There many application for DNS for an SRE for example: internal DNS infrastructure, Service Discovery, DNS Load Balancing, Scalling Services (CNAME), CDNs, DNSSEC ...
- Some usecases from linkedin SRE course
- Cool Networking Excercices