RFC 1123 Hostname Validation: Can Hostnames Start with Digits? A Deep Dive into Domain Naming Rules


2 views

According to RFC 1123 Section 2.1, hostnames must follow these key rules:

1. Allowed characters: a-z, 0-9, and hyphen (-)
2. Labels must start and end with alphanumeric characters
3. Maximum length of 63 characters per label
4. No restriction on starting with digits

The Google Guava library's InternetDomainName.isValid() method implements stricter validation than RFC 1123 requires. When testing with:

boolean isValid = InternetDomainName.isValid("8server");
// Returns false despite RFC 1123 compliance

This discrepancy affects several scenarios:

  • Legacy systems using digit-prefixed hostnames
  • Internal network configurations
  • DNS record validation

For applications needing RFC 1123 compliance:

// Custom validation method
public static boolean isRfc1123Compliant(String hostname) {
    return hostname.matches("^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]$") 
        && hostname.length() <= 253 
        && !hostname.startsWith("-") 
        && !hostname.endsWith("-");
}

// Example usage:
isRfc1123Compliant("8server"); // Returns true

Most DNS implementations actually accept digit-starting hostnames:

$ dig 8server.example.com
;; ANSWER SECTION:
8server.example.com. 300 IN A 192.0.2.1

For maximum compatibility:

  1. Prefer alphabetic-starting hostnames when possible
  2. Document validation rules clearly in APIs
  3. Support both strict and RFC modes when feasible

According to RFC 1123 Section 2.1, hostname labels may begin with digits. The relevant specification states:


The syntax of a legal Internet host name was specified in RFC-952 [DNS:4].
One aspect of host name syntax is hereby changed: the restriction on the 
first character is relaxed to allow either a letter or a digit.

While RFC 1123 permits digit-starting hostnames, real-world implementations vary:

  • Google Guava's InternetDomainName.isValid() rejects bare hostnames starting with digits
  • Modern browsers and DNS resolvers typically accept them
  • Some legacy systems may still enforce stricter rules

Here's how different programming languages handle digit-starting hostnames:


// Java (Guava Example)
boolean isValid = InternetDomainName.isValid("8server"); // Returns false

// Python
import re
pattern = r'^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]$'
matches = bool(re.fullmatch(pattern, "8server")) # Returns True

// JavaScript
const isValid = /^(?!-)[a-zA-Z0-9-]{1,63}(?

When working with digit-starting hostnames:

  • Always test with your specific DNS resolver
  • Consider using punycode encoding for maximum compatibility
  • Be aware of TLD-specific restrictions (some ccTLDs enforce additional rules)

For projects requiring Guava compatibility:


public static boolean isLenientValid(String hostname) {
    // First check standard validation
    if (InternetDomainName.isValid(hostname)) {
        return true;
    }
    // Fallback to RFC 1123 compliance check
    return hostname.matches("^[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]$");
}

Remember that while RFC 1123 permits digit-starting hostnames, application-level validation might still reject them for historical or security reasons.