Testing Server Migration: How to Change the Hosts File
From time to time, a server migration is on the agenda. The complete migration usually takes place through the update of the DNS entries. But how do you test in advance if everything has worked out?
![Testing Server Migration: How to Change the Hosts File Testing Server Migration: How to Change the Hosts File](https://www.codeaware.at/assets/images/posts/_hudc96b7d97aa7a27a391d30d94eb2430b_1502378_7426200cb698caa0ba5664012f7650f3.webp)
![Ing. Philipp Doblhofer Ing. Philipp Doblhofer](https://www.codeaware.at/assets/images/global/doblhofer_hufaff7b805266c713f172f1de593264c0_4648679_160x0_resize_q90_h2_lanczos.webp)
Ing. Philipp Doblhofer
From time to time, server migrations (e.g., when changing a server host) of a website are necessary. This involves copying all necessary website data and possibly the database to another server. To actually deliver the data of the new server when calling the domain (such as www.codeaware.at), it is necessary to adjust the IP address at the DNS server.
However, before this is carried out, it makes sense to extensively test the copy beforehand. But how can you access the server in the best way? The magic word here is: Hosts file. When a domain is entered in the browser, it attempts to find out the corresponding IP address to finally address the server. Simplified, there are two sources for this:
- First, the local hosts file is searched to see if a corresponding entry exists
- If no IP address was found, a DNS server is queried. This server, according to the example above, has the address of the old server stored.
Since the hosts file has a higher priority, the server migration can be easily tested by modifying the hosts file. Here, the new IP address for the domain needs to be entered to access the new server via the usual URL.
The hosts file exists on all common systems like Windows, Linux, and Mac. However, administrator permissions are required to edit it.
How to change the hosts file under Windows?
Open the program Editor/Notepad as Administrator
Open the file C:\Windows\System32\Drivers\etc\hosts
Enter the new server’s IP address in a new line, then – separated by spaces – the desired domain. (e.g., 1.2.3.4 www.demoseite.at)
Save the changes
If the changes are not effective immediately, restart the computer. (The DNS entries are cached by the computer)
How to change the hosts file under Linux?
- Open the program Terminal
- There, enter the command sudo nano /etc/hosts. Sudo allows running programs as administrator. Nano is a console text editor
- Enter the user password
- In a new line, enter the new server’s IP address, then – separated by spaces – the desired domain. (e.g., 1.2.3.4 www.demoseite.at)
- Save the changes (Ctrl+X, then Y and Enter)
- If the changes are not effective immediately, restart the computer. (The DNS entries are cached by the computer)
How to change the hosts file under Mac?
- Open the program Terminal
- There, enter the command sudo nano /etc/hosts. Sudo allows running programs as administrator. Nano is a console text editor
- Enter the user password
- In a new line, enter the new server’s IP address, then – separated by spaces – the desired domain. (e.g., 1.2.3.4 www.demoseite.at)
- Save the changes (Control+X, then Y and Enter)
- If the changes are not effective immediately, restart the computer. (The DNS entries are cached by the computer)