How the name server is resolved if its from the same domain?
Your understanding is correct on the whole (as a minor point of clarification, it's not your computer that will recursively resolve DNS records, it's typically your ISP.)
The "missing piece" you're looking for is the glue record, which is a DNS record specifically designed to fix that circular reference.
Glue records are DNS records created at the domain’s registrar. The record provides a complete answer when the TLD nameserver returns a reference for an authoritative nameserver for a domain. For example, the domain name “example.net” has nameservers “ns1.example.net” “ns2.example.net”. To resolve the domain name, the DNS would query in order: root, TLD nameserver, and authoritative nameserver. However, by having the authoritative nameservers inside the domain itself, these nameservers cannot be found without outside assistance. This is called a ‘circular reference’. Creating a glue record, an A record served by the TLD nameserver, avoids circular references and allows for both DNS name resolution and listing the nameservers inside the domain itself.
So to look up the A
record of example.com
in your case, your DNS resolver will first go to the .com
nameserver, it will ask for the nameservers for example.com
, and it will receive not just ns1.example.com
and ns2.example.com
but ALSO the IP addresses of those nameservers so that your resolver knows which IP addresses to use to make further DNS queries on that domain.
That is achieved through what it's called a "glue record".
The DNS server has both the NS and the corresponding A (and/or AAAA for IPv6) records for the NS entries and serve them "glued" to the NS response.
So even if you only ask for the NS records, the DNS would respond with both the NS servers and their IP addresses.