Test website via local DNS resolver
The following help center article describes how you can easily test websites on a server on the Internet without having to make any adjustments to the DNS.
Requirements
- Linux, macOS, Windows
- Administrator authorization
General explanation
All common operating systems have a hosts file in which a simple mapping of IPv4 or IPv6 addresses to a FQDN (Fully Qualified Domain Name) can be stored for name resolution.
The syntax is the same for all operating systems, but the file is located at different positions in the file system depending on the operating system.
As soon as your system attempts to resolve a domain name to an IP address, it first checks the hosts file to see whether entries for the domain are stored there; if not, it sends the request to the configured DNS server as usual.
Remember to remove the changes after your tests to avoid unwanted side effects during DNS name resolution.
Please note that certificate error messages may occur if the web server with the IP address stored in /etc/hosts does not have a valid certificate for the domain you have specified.
Syntax
The file consists of pairs of IPv4 or IPv6 addresses and domain names as in the following example:
# Comment
# <ipv-4-address> <FQDN>
# <ipv-6-address> <FQDN>
1.2.3.4 staging-test.mydomain.com
2001:db8:1234:5678::1 staging-test.meinedomain.de
1.2.3.4 mydomain.com
2001:db8:1234:5678::1 meinedomain.de In the above example, when staging-test.mydomain.com is called using IPv4, it resolves to 1.2.3.4 and with IPv6 to 2001:db8:1234:5678::1, although the DNS servers would resolve a different IP address. The same applies to the main domain meinedomain.de.
Linux and macOS
Under Linux and macOS, the file is located under /etc/hosts and can be edited in any standard editor such as nano.
Manipulating /etc/hosts requires root authorization, which is why the command usually has to be executed with sudo on clients.
sudo nano /etc/hosts Windows
Windows manages the file under the path C:\Windows\System32\drivers\etc\hosts, which also requires administrator authorization to edit.
The following example opens the file for editing with the Windows Notepad editor.
\nCMD (administrator password must be entered manually):
runas /user:Administrator "notepad C:\path\to\file.txt"
Enter the password for Administrator: PowerShell (UAC authorization, GUI-based yes/no):
Start-Process notepad.exe -ArgumentList "C:\Windows\System32\drivers\etc\hosts" -Verb RunAs Frequent error messages
Expected web page is not loaded
If you believe that you are not accessing the correct website, it is possible that your local DNS cache still contains the original IP address (before customization), which means that the DNS query is not performed via /etc/hosts or via your configured DNS server.
Clearing the DNS cache on your client system is a possible solution here, which can be done as follows:
Windows
Open PowerShell or CMD:
ipconfig /flushdns macOS
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder Linux
NetworkManager:
sudo nmcli general reload dns-full Systemd resolver:
sudo systemd-resolve --flush-caches or
sudo resolvectl flush-caches Dnsmasq:
sudo systemctl restart dnsmasq