Skip to main content
All CollectionsWordPress
Editing the hosts-file
Editing the hosts-file

Uudelleenohjaa osoite käyttämään tiettyä IP-osoitetta hosts-tiedostoa muokkaamalla

Kenneth Nyman avatar
Written by Kenneth Nyman
Updated over a year ago

By editing the hosts file, you can locally associate a domain with a different IP address. This allows you to, for example, test the functionality of a website without actually using the registered domain. The local hosts file overrides the addresses provided by DNS services. More information can be found here: https://en.wikipedia.org/wiki/Hosts_(file)

The hosts file can be found in different locations depending on the operating system:

Windows

C:\Windows\System32\drivers\etc\

You can edit the file by double-clicking on it and selecting a text editor from the list (e.g., Notepad).

Mac

/private/etc/hosts

You can edit the file by opening Finder, selecting Go --> Go to Folder, and entering the above file path. Open the file by double-clicking on its icon.

Linux

You can edit the file by entering the following command in the terminal:

sudo vim /etc/hosts


Adding an entry to the hosts file and testing the address

At the end of the hosts file (after lines starting with #), add the desired entry in the following format

IP-address     domain


For example:

185.55.85.4   exampledomain.fi

The IP address and the domain should be separated by at least one space or a tab.

After adding the line, save the file.

NOTE: You need to be logged in to the operating system with administrator privileges to save the changes you made.

You can now open the added domain in your browser, and the site should load from the redirected server.

NOTE: Remember to remove the added line from the hosts file when you no longer need the local redirection.

To remove the redirection, simply delete the line or comment it out by placing a # character in front of it.

Clearing DNS cache

If the website doesn't load as expected, you can try clearing the DNS cache of your system. Here's how to do it:

Windows

  1. Press the Windows key (or open the Start menu) and type "cmd" to search for Command Prompt.

  2. Right-click on the Command Prompt icon and select "Run as administrator." If prompted, select "Yes" to allow changes to the device.

  3. Enter the following command:

    ipconfig /flushdns

  4. The DNS cache is now cleared. A successful flush will result in the following message:
    Windows IP Configuration

    Successfully flushed the DNS Resolver Cache.

  5. You can now try opening the address again in your browser.

Mac OS X 10.11 and later

  1. Open the Terminal application (e.g., press cmd + space and search for "terminal").

  2. Enter the following command:
    sudo killall -HUP mDNSResponder

    NOTE: The system may prompt you to enter your macOS password to execute the command.

  3. The DNS cache is now cleared. You can try opening the address again in your browser.

Linux (systems using systemd)

  1. Open the terminal.

  2. Enter the following command:
    sudo systemd-resolve --flush-caches

  3. The DNS cache is now cleared. You can try opening the address again in your browser.

Did this answer your question?