The "hosts" file is a simple text file used to map hostnames to IP addresses on your computer. It can be edited to control how your computer resolves domain names, effectively overriding DNS settings. Here's how to edit the "hosts" file on Windows, Mac, and Linux:
On Windows:
- Open Notepad as an administrator:
- Right-click on the Start button and select "Windows Terminal (Admin)" or "Command Prompt (Admin)" if you're using an older version of Windows.
- If you're using Windows 10 or later, you can also search for "Notepad" in the Start menu, right-click it, and choose "Run as administrator."
- Open the hosts file:
- In the command prompt or terminal, type
notepad C:\Windows\System32\drivers\etc\hostsand press Enter. This will open the "hosts" file in Notepad.
- In the command prompt or terminal, type
- Edit the hosts file:
- You can add or modify entries in the hosts file. For example, to map the IP address 127.0.0.1 to a domain name "example.com," add the following line:
127.0.0.1 example.com
- Save the hosts file:
- After making your changes, save the file and close Notepad.
- Flush DNS cache (optional):
- To apply changes immediately, you can flush the DNS cache by running
ipconfig /flushdnsin the command prompt.
- To apply changes immediately, you can flush the DNS cache by running
On macOS (Mac OS X):
- Open Terminal:
- You can find Terminal in the Utilities folder within the Applications folder or search for it using Spotlight.
- Open the hosts file:
- In the terminal, type
sudo nano /etc/hostsand press Enter. You'll be prompted to enter your password.
- In the terminal, type
- Edit the hosts file:
- Add or modify entries as needed. Use the same format as in the Windows example.
- Save the hosts file:
- After editing, press
Ctrl + O, then press Enter to save the changes. PressCtrl + Xto exit nano.
- After editing, press
- Flush DNS cache (optional):
- You can flush the DNS cache using the command
sudo dscacheutil -flushcache.
- You can flush the DNS cache using the command
On Linux (Ubuntu, Debian, CentOS, etc.):
- Open Terminal:
- You can usually find Terminal or a similar command-line tool in your applications menu, or by pressing
Ctrl+Alt+t.
- You can usually find Terminal or a similar command-line tool in your applications menu, or by pressing
- Open the hosts file:
- In the terminal, use a text editor (e.g., nano or vim) to open the hosts file. For example:
sudo nano /etc/hosts
- In the terminal, use a text editor (e.g., nano or vim) to open the hosts file. For example:
- Edit the hosts file:
- Make the necessary changes using the same format as in the Windows example.
- Save the hosts file:
- After editing, press
Ctrl + O, then press Enter to save the changes. PressCtrl + Xto exit the text editor.
- After editing, press
- Flush DNS cache (optional):
- The procedure for flushing the DNS cache can vary depending on your Linux distribution. You may use commands like
systemctl,service, orsystemdto restart the networking service. Check the documentation for your specific Linux distribution for the appropriate command.
- The procedure for flushing the DNS cache can vary depending on your Linux distribution. You may use commands like
Note: Remember to be cautious when editing the hosts file, as incorrect entries can affect network connectivity. Always back up the original file and make sure you have the necessary permissions to edit the file, especially on Mac and Linux.