What is a hosts file and how do you set it up?
Do you want to test your website on a new server before your domain name officially points to that new location? Then you have two options: edit the hosts file or use our handy preview tool. In this article I explain both methods.
The easy method: preview.theory7.net
Before you start tinkering with system files, there is a much simpler solution: our Website Preview Tool at preview.theory7.net. With this tool you can view your website on a new server without changing anything at all on your computer.
How does preview.theory7.net work?
- Go to preview.theory7.net
- Enter your domain name (for example: mywebsite.nl)
- Enter the IP address of the new server
- Choose the port (usually 443 for HTTPS or 80 for HTTP)
- Select the protocol (HTTP or HTTPS)
- Click the button to generate a preview URL
- Copy the preview URL and view your website!
Benefits of the preview tool
- No technical knowledge needed: you don't have to edit any system files
- Instant results: a working preview URL within seconds
- Shareable: you can share the preview URL with others (colleagues, clients)
- Safe: you don't change anything on your computer
- Temporary: preview links expire automatically after a few hours
Tip: This tool is ideal for a quick check or for sharing a preview with a client. For more extensive testing (several days, filling in forms, backend access), the hosts file method is more suitable.
The traditional method: the hosts file
A hosts file is a simple text file on your computer in which you can manually specify which IP address belongs to which domain name. Normally your DNS server handles this automatically, but with a hosts file you can temporarily take over. This is handy when you want to test a new website on a new server without visitors noticing anything.
When do you use the hosts file method?
- You want to test for longer than a few hours
- You need to log in to an admin panel or CMS
- You want to test forms that point to your domain
- You don't need internet access to go to the preview URL
- You are technically skilled and don't mind editing system files
Why would you use a hosts file?
Say you have a domain name, for example www.mywebsite.nl, and you have just bought a new hosting plan from Theory7. Maybe you want to build a completely new website, or you are migrating from another hosting provider. But you first want to be sure that everything works properly on the new server before you change the DNS or nameservers to Theory7.
That makes sense, of course. This way you avoid surprises or downtime for your visitors.
The solution? Temporarily edit your hosts file.
By manually specifying in your hosts file that www.mywebsite.nl should point to the IP address of your new Theory7 server, you can already test how the website looks and works in the new environment. Only you see that new version, while the rest of the world keeps using the old server. Ideal for building at your own pace, checking everything or verifying the migration.
Please note: SSL certificates (such as Let's Encrypt) will not work yet as long as your domain does not actually point to the new server. These certificates require a DNS check to confirm that you are the owner of the domain name. Only once the DNS or nameservers have been changed can you request an SSL certificate.
Once you are completely satisfied, you switch the DNS or nameservers to Theory7. From that moment on, everyone sees the new version of your website, complete with a valid SSL certificate.
Setting up the hosts file on Windows
-
Open Notepad as administrator:
- Search for "Notepad" in Start, right-click it and choose "Run as administrator".
-
Open the file:
- Go to
C:\Windows\System32\drivers\etc\hosts. - Set the file type at the bottom right to "All Files" to make
hostsvisible.
- Go to
-
Add these lines at the bottom:
213.154.231.200 www.mywebsite.nl 213.154.231.200 mywebsite.nlNote: Add both the www and the non-www version!
-
Save the file and close it.
-
If needed, restart your browser or flush your DNS cache with:
ipconfig /flushdns
Setting up the hosts file on Mac
-
Open Terminal (via Spotlight or in Applications > Utilities).
-
Type:
sudo nano /etc/hostsPress Enter and enter your password.
-
Add these lines at the bottom of the file:
213.154.231.200 www.mywebsite.nl 213.154.231.200 mywebsite.nl -
Press
CTRL + Oto save andCTRL + Xto exit. -
Flush your DNS cache with:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Setting up the hosts file on Ubuntu / Linux
-
Open your terminal.
-
Type the following command to open the hosts file in a text editor:
sudo nano /etc/hosts -
Add these lines at the bottom:
Note: replace 123.123.123.123 with your own server IP.123.123.123.123 www.mywebsite.nl 123.123.123.123 mywebsite.nl -
Press
CTRL + Oto save, andCTRL + Xto close nano. -
If needed, flush your DNS cache with:
- On Ubuntu (depending on your version), you can try:
sudo systemd-resolve --flush-caches- Or simply restart your network service:
sudo systemctl restart NetworkManager
Now only your computer points that domain name to the new IP address, without you changing the DNS for anyone else.
What does an edited hosts file look like?
Here is a small example:
127.0.0.1 localhost
213.154.231.200 www.mywebsite.nl
213.154.231.200 mywebsite.nl
Note: replace 123.123.123.123 with your own server IP.
Undoing the change
It's very simple: open the hosts file again and remove the lines you added earlier. Save the file again and you are back to the original situation. Don't forget to flush your DNS cache!
Common mistakes
- Only www added: always add both www.domain.nl and domain.nl
- Browser cache: clear your browser cache or use an incognito window
- VPN active: some VPNs ignore your local hosts file
- Typo in the domain name: double-check the spelling
- No administrator rights: on Windows you must open Notepad as administrator
Which method is best for you?
| Situation | Recommended method |
|---|---|
| Quick check whether the website works | preview.theory7.net |
| Sharing a preview with a client | preview.theory7.net |
| Extensive testing over several days | Hosts file |
| Testing an admin panel / CMS | Hosts file |
| Testing forms and redirects | Hosts file |
| No technical knowledge | preview.theory7.net |
Can't figure it out? Our support team is ready to help! Submit a ticket through the customer portal and we'll usually help you within a few hours.
0 of 0 found this helpful