How do I edit my computer’s hosts file?
The hosts file is a text file that maps hostnames to IP addresses. This file is used by your computer's operating system to resolve hostnames to IP addresses. By editing the hosts file, you can point a hostname to a specific IP address. This can be useful for a variety of purposes, such as:
- Testing a website before it goes live to the public
- Bypass DNS issues
- Redirecting traffic to a different server
Here are the steps on how to edit the hosts file on different operating systems. Always take a backup copy of any key files like this before you edit them and bear in mind that mistakes in this file can stop your computer being able to access things on the local network or the internet.
Windows 10 and 11
- Open Notepad as an administrator. You can do this by right-clicking on Notepad and selecting Run as administrator.
- In Notepad, navigate to the following file path:
C:\Windows\System32\drivers\etc\hosts
- Add the following line to the end of the file, replacing `192.168.1.100` with the IP address of the server you want to connect to, and `example.com` with the hostname you want to map to that IP address:
192.168.1.100 example.com
- If this is for a website, consider adding a second line for www.example.com with the same IP address, so that the change will take effect whether or not you include www. in the address you visit.
- Save the file.
macOS
- Open Terminal.
- Type the following command and press Enter:
sudo nano /etc/hosts
- Add the following line to the end of the file, replacing `192.168.1.100` with the IP address of the server you want to connect to, and `example.com` with the hostname you want to map to that IP address:
192.168.1.100 example.com
- If this is for a website, consider adding a second line for www.example.com with the same IP address, so that the change will take effect whether or not you include www. in the address you visit.
- Press `Ctrl`+`X` to exit nano.
- When prompted, save the file.
Linux
- Open a terminal.
- Type the following command and press Enter:
sudo vi /etc/hosts
- Add the following line to the end of the file, replacing `192.168.1.100` with the IP address of the server you want to connect to, and `example.com` with the hostname you want to map to that IP address:
192.168.1.100 example.com
- If this is for a website, consider adding a second line for www.example.com with the same IP address, so that the change will take effect whether or not you include www. in the address you visit.
- Press `:` to exit vi.
- When prompted, save the file.
**On Windows-based systems, after editing the hosts file, you may need to flush your DNS cache. To do this, open a command prompt and type the following command:**
ipconfig /flushdns
**Once you have flushed your DNS cache, you should be able to access the website or server you mapped to the specific IP address.**