How to Edit Hosts File in Windows, MacOS and Linux

Editing the hosts file is a way to manually associate IP addresses with domain names, overriding the DNS (Domain Name System) resolution for specific websites. This can be useful for various purposes, such as testing websites on a new server before updating DNS records, blocking access to certain websites, or redirecting websites to different IP addresses. Here’s how you can edit the hosts file on Windows, macOS, and Linux:


Open Notepad as Administrator: Search for “Notepad” in the Windows search bar, right-click on “Notepad,” and select “Run as administrator.”

Open Hosts File: In Notepad, go to “File” > “Open,” and navigate to C:\Windows\System32\drivers\etc. In the file type dropdown, select “All Files,” then select the “hosts” file and click “Open.”

Edit the Hosts File: You’ll see a list of IP addresses and domain names. To add an entry, you can add a line at the end of the file in the format IP_ADDRESS DOMAIN_NAME. For example:

Save the Changes: After making the necessary changes, go to “File” > “Save.” Note that you might not be able to directly save the changes due to permissions. In that case, save the edited file to your desktop and then replace the original file in the etc folder.

macOS and Linux

Open Terminal: On macOS, you can find Terminal in the Utilities folder within the Applications folder. On Linux, you can use the terminal application that comes with your distribution.

Open Hosts File: In the terminal, enter the following command to open the hosts file with administrative privileges:

sudo nano /etc/hosts

This will prompt you to enter your password.

Edit the Hosts File: In the nano text editor, you’ll see the contents of the hosts file. Similar to Windows, you can add entries in the format IP_ADDRESS DOMAIN_NAME.

Save the Changes: After making the necessary changes, press Ctrl + O to save the file, and then press Ctrl + X to exit nano.

Clear DNS cache

Remember that after editing the hosts file, you might need to clear your DNS cache to see the changes take effect. You can do this by running the appropriate command in your terminal:

  • Windows:
ipconfig /flushdns
  • macOS:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
  • Linux: The method can vary depending on the Linux distribution. You might use sudo
service network-manager restart or sudo systemctl restart NetworkManager

Always be cautious when editing the hosts file, as incorrect entries can lead to connectivity issues for certain websites. It’s recommended to create a backup of the hosts file before making any changes.

Knowledge Base Linux
Knowledge Base Linux

Kblinux is an abbreviation for the phrase "Knowledge Base Linux." The website shares instructional articles related to the Linux system. I hope my small blog will reach many people who share the same passion for Linux.

Articles: 42


Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.