Sub topic 2
The domain name hierarchy
This chapter assumes you know what an IP address is and can run commands in a terminal. It also assumes you have read the previous chapter on why DNS exists. Here you will learn how names are structured, how to read them, and which rules limit them. Later chapters cover who serves each part of the tree.
A tree of names
DNS does not store names in one flat list. It arranges them in a tree. Each point in the tree is a node, and each node has a label, which is one piece of text such as www or example. A domain name is the sequence of labels on the path from a node up to the top of the tree. The labels are written from the most specific on the left to the least specific on the right, separated by dots.
Take www.example.com:
comis a child of the root.exampleis a child ofcom.wwwis a child ofexample.
The tree makes it possible to split the work of managing names. Nobody has to maintain a global list, because the owner of example.com decides what exists beneath it. Two organizations can both have a host called www, because www.example.com and www.example.org sit at different places in the tree.
The two example nodes in the diagram are unrelated. example.com and example.co.uk share only a label, not a position in the tree.
The root and the trailing dot
The top of the tree is the root. Its label is empty, with zero characters. Because the root's label is empty, the full name of a host ends with a dot that stands for the root:
www.example.com.A name that includes every label up to the root is a fully qualified domain name (FQDN), also called an absolute name. The trailing dot marks it as complete. A name without the dot, such as www, is relative. A resolver may complete a relative name by adding a suffix, which a later chapter describes under search domains. A name with no trailing dot can still be an FQDN in everyday use. Browsers add the dot for you, and in most tools example.com and example.com. mean the same thing. The dot matters in zone files and in configuration, where a missing dot changes the meaning of a name.
| Term | Example | Meaning |
|---|---|---|
| Label | `example` | One piece between dots |
| Relative name | `www` | Incomplete, depends on a suffix being added |
| FQDN | `www.example.com.` | Every label up to the root |
| Hostname | `www` or `www.example.com` | A name that identifies a machine, usually restricted to letters, digits and hyphens |
Reading a name from right to left
People read names left to right, but DNS reads them from right to left. A resolver starts at the root, then handles com, then example, then www. Each step moves one level down the tree. The depth of a label counts from the root:
| Label | Level | Common name |
|---|---|---|
| (empty) | 0 | Root |
| `com` | 1 | Top-level domain (TLD) |
| `example` | 2 | Second-level domain |
| `www` | 3 | Third-level domain, often a hostname |
A name that falls under another name is a subdomain of it. www.example.com is a subdomain of example.com, and both are subdomains of com. The word "domain" here means a whole subtree. example.com is the node itself and everything beneath it.
Top-level domains
The top-level domain (TLD) is the label directly under the root. The IANA function, run by ICANN, manages the root. These are the main categories of TLD:
| Category | Examples | Notes |
|---|---|---|
| Generic (gTLD) | `com`, `org`, `net`, `app` | Open or lightly restricted registration |
| Country code (ccTLD) | `uk`, `de`, `jp` | Two letters, based on ISO 3166 country codes |
| Sponsored | `edu`, `gov`, `int` | Limited to eligible organizations |
| Infrastructure | `arpa` | Used for internal purposes such as reverse lookups |
Some countries structure their space further. In example.co.uk, the label co is a second-level label that the uk registry reserves for commercial use, so the name you register is the third level. A browser or certificate authority cannot find the boundary by counting dots. They consult the Public Suffix List, a maintained list of the names under which the public can register. For example.co.uk, the public suffix is co.uk, and the registrable domain is example.co.uk.
Rules for labels and names
DNS puts hard limits on names, and breaking them produces errors that look unrelated to length:
- A label is 1 to 63 octets long.
- A full name is at most 255 octets in the wire format. That is 253 characters in the usual text form, without the trailing dot. The wire format stores each label with one length octet in front, plus the final zero octet for the root.
- Comparison is case-insensitive.
WWW.Example.COMandwww.example.comare the same name. Servers may preserve the case they received, but they must not treat it as significant. - DNS itself can carry almost any octets in a label. The traditional rules for hostnames are stricter. They allow letters, digits and hyphens, and a label may not start or end with a hyphen. The underscore appears in service names such as
_dmarc.example.com, where the name labels a record and not a host.
Internationalized domain names (IDNs) allow non-ASCII characters. They do this without changing DNS. The name is converted to an ASCII form that starts with xn--, called Punycode. The word münchen becomes xn--mnchen-3ya. DNS servers only ever see the ASCII form.
Inspecting a name in code
The script below splits a name into labels, adds the root, and checks the limits above. It uses only the standard library.
def analyze(name: str) -> None:
fqdn = name if name.endswith(".") else name + "."
labels = fqdn.rstrip(".").split(".")
# Wire length: one length octet plus the label, for each label,
# plus one zero octet for the root.
wire_length = sum(len(label) + 1 for label in labels) + 1
print(f"Name: {fqdn}")
print(f"Wire length: {wire_length} octets")
for depth, label in enumerate(reversed(labels), start=1):
problem = "" if 1 <= len(label) <= 63 else " <-- invalid length"
print(f" level {depth}: {label}{problem}")
if wire_length > 255:
print(" <-- name is too long")
analyze("www.example.com")
analyze("www.example.co.uk.")Running it prints:
Name: www.example.com.
Wire length: 17 octets
level 1: com
level 2: example
level 3: www
Name: www.example.co.uk.
Wire length: 21 octets
level 1: uk
level 2: co
level 3: example
level 4: wwwThe output lists the levels in the order a resolver visits them. Notice that example.co.uk has a level the script cannot interpret. The code can count labels, but it cannot tell you which of them the public registers. Only the Public Suffix List can do that.
Common mistakes
- Treating the number of dots as the registration level.
a.b.example.co.ukandexample.comboth have a registrable domain, but at different depths. - Forgetting that a trailing dot changes meaning in a zone file. In a zone file,
www.example.comwithout the dot is relative, and the server appends the zone's origin to it. The result iswww.example.com.example.com.. - Assuming that a domain and a server are the same thing. A name is a position in the tree. Which machines answer for that position is a separate question, covered in the chapters on zones and name servers.
- Counting length in characters for non-ASCII names. The limit applies to the encoded form, so a name with IDN labels reaches the limit sooner.
Exercise
For the name api.shop.example.co.uk., list the labels from the root downward, give the level of shop, and say what its registrable domain is, assuming co.uk is a public suffix.
Show answer
The labels from the root downward are uk, co, example, shop, api. The label shop is at level 4. The registrable domain is example.co.uk, because co.uk is the public suffix and one label is added to it. shop.example.co.uk and api.shop.example.co.uk are subdomains that the owner of example.co.uk can create freely.
Exercise
A zone file for example.com contains the line www IN A 192.0.2.80 and another line mail.example.com IN A 192.0.2.25, with no trailing dot on the second name. What name does the second line actually define, and how do you fix it?
Show answer
The server treats mail.example.com as relative and appends the origin, so it defines mail.example.com.example.com.. Fix it by writing the name as mail.example.com. with the trailing dot, or by writing the relative name mail.