How to Edit Hosts File in Windows, Mac & Linux

Before we jump into How to edit Hosts File, it’s important to know, what a host file is?

Hosts file is your local operating system file. It’s a part of networking design of the computers. Basically hosts file is a system level DNS. So, used for resolving Domain Names and netBIOS names at system level only.

edit hosts file windows mac linux - dots created

Since the inception, hosts file varied a bit only. It is due to the similarities in functionality and syntax for Windows, Mac, and Linux operating systems. but wait…

Why should we edit the hosts file at first place?

The Hosts file is used to resolve the domain names. Hence sometimes, developers used this to test the changes before the final push to the production or live portal.

Also, hosts file can be used to block some websites at the system level and to control the access on the network in case of attacks.

Enough of talks, now we will discuss how to edit hosts file on windows, mac, and Linux one by one…

How to edit Hosts File on Windows?

Editing the hosts file on windows requires administrative privileges. But being an administrator of the system, you may still not able to edit and save hosts file.

The trick is, open the application first with administrative privileges and then open the file using it instead of directly opening the file in the application.

Here is the solution…

Edit Hosts File on Windows 10 or 8.

STEP 1

Open Notepad as Administrator on Windows - Dots Created

Search for Notepad in the windows search bar. Then right click on the notepad

application in the search results. Open the Notepad as an Administrator. In windows 10 this will look like as shown in the image.

STEP 2

Now the notepad is open with administrative privileges. Just open the hosts file from the notepad menu using File → Open.

Hosts file location is..

c:\windows\system32\drivers\etc\hosts

Don’t forget to select All Files from the right bottom drop down. Else the files will not appear in the etc directory.

STEP 3

Now you can edit anything on the hosts file and save it without issues. 🙂

To block any website on the system, a entry of IP address and the domain name is sufficient. As an example, I want to block Twitter on my system. The simplest thing I can do is, open the hosts file and make the below entry.

0.0.0.0 www.twitter.com

windows edit hosts file - dots created

Don’t put at the beginning of the entry as it is used to make comments in hosts file. Now, if you try to access the twitter, it will not open the web page. Do try and let me know in the comments.

Edit Hosts File on Windows 7 or Vista.

STEP 1

Technically the process will be the same as that of Windows 10 with few changes in the flow.

Go to Click Start All Programs Accessories.
Right click Notepad and select Run as Administrator to open. Windows will need your permission on UAC window, so press continue.

STEP 2

Now the notepad is open with administrative privileges. Just open the hosts file from the notepad menu using File → Open and the below path…

c:\windows\system32\drivers\etc\hosts

Don’t forget to select All Files from the right bottom drop down. Else the files will not appear in the etc directory.

STEP 3

Now you can edit hosts file on windows 7 or vista and save it without any permission errors.

How to Edit Hosts File on Mac?

Editing hosts file in Mac is very easy as you just need a command to do so.

STEP 1

Open the terminal on your mac. You can follow the following path..

Finder → Applications → Utilities → Terminal

STEP 2

In the terminal window, you just need the below command with your password. Enter the command:

sudo nano /etc/hosts

Press Return. Enter Your Password. Again Press Return.

STEP 3

Now you can edit hosts file and save it using nano editor. You can choose another editor as well like vim. but don’t forget to flush the DNS using below command…

dscacheutil -flushcache

How to Edit Hosts File on Linux?

Editing hosts file in Linux is very much similar to the mac. you only need to run a single command in the terminal.

STEP 1

Open the terminal your you Linux Distro. You can either use CTRL+ALT+T shortcut or search through the Ubuntu dash.

STEP 2

In the terminal window, you just need the below command with your password. use the command:

sudo nano /etc/hosts

Press Enter. Enter Your Password. Again Press Enter.

STEP 3

Now you can edit hosts file and save it using nano editor. You can choose other editor as well like vim.

That’s all of today. 🙂
If you still face any issues or errors while editing the hosts file, it’s a good idea to post them in the comment section.

I’ll try to solve them for you.

 

Leave a Comment