What is the hosts file?
Servers and computers are usually only accessible via IP addresses.
Before the DNS system existed and you could refer a name to an IP address using DNS records , you either had to know the IP address and enter it manually, or you had your own “DNS records” with the hosts file. created where you can refer a name to an IP address.
One could now say that nowadays, with the DNS records, the file is no longer needed. However, this is not the case. For example, when migrating a website, you can use the hosts file to check whether the website is running on the new server before changing the DNS records. As a result, visitors to your website will still see the website on the old server while you are already seeing the page on the new server.
Where is the hosts file?
So that you can look at it and edit it yourself, of course you have to know how you got to this file in the first place. This depends somewhat on what operating system you are using. Before you change the file, please make sure to make a backup copy of it.
UNIX systems (Mac or Linux)
On operating systems based on UNIX, the file is located under the following path.
/etc/hosts
Windows
On Windows systems, the file is located in the following path.
C:\Windows\System32\drivers\etc
There are two hosts files in the directory. One is the normal hosts file, the one we want to edit. The other is an iCalendar file, which you don’t need.
How to edit the hosts file?
Editing the hosts file also depends on which operating system you are using.
Windows
In order to be able to edit the file, it is recommended that you first start the editor as an administrator and then open the file directly from the editor (e.g. Notepad++ ).
UNIX systems (Mac or Linux)
On the Mac, you can either edit the file in an editor or via the terminal. With Linux systems it depends on whether you have installed a graphical interface or not.
In Notepad, edit the file by clicking “Go” at the top, then selecting “Go to Folder.” Specify the /etc folder and open the hosts file.
The steps in the terminal look like this.
- open terminal
- cd /etc
- sudo vi hosts
- Navigate to the end of the file with the arrow keys
- press a
to press input to press - :wq!
What should the entry in the hosts file look like?
The entry looks the same on all systems. A new line must be added at the end of each file that conforms to the following format.
123.45.67.89 beispiel.deinedomain.eu
If you then save the file, the entry is active. Note that you can only access the old website if you either delete the entry or comment it out. You can comment it out by putting a hashtag (#) at the beginning of the line. This is what it looks like.
#123.45.67.89 beispiel.deinedomain.eu
If you follow the instructions for Terminal on UNIX systems above, the last step is to save.