How to Use the Hosts File in Linux

If you saw our article on using the hosts file in Windows and you’re using Linux instead of Windows, you may be wondering how to do exactly the same thing in Linux. Well, you’re in luck. Here are the exact steps to use the hosts file in Linux.

In Linux, and other Unix-based systems, including the BSDs and Mac OS X, it’s located in “/etc/hosts”, and it works almost exactly the same way it does under Windows. In fact, it existed under Unix for longer than it has under Windows.

The hosts file originally performed the same role that DNS did: matching IP addresses to hostnames. It worked fine under the early Internet, where there were only a handful of hosts. System administrators would download a copy of the file from a central source that had a list of known hosts.

Even as soon as the early 1980s, it started to become unwieldy, and the need for a more flexible system was born. DNS quickly became the solution, because it’s decentralized and distributed among many machines across the Internet.

The old, humble “/etc/hosts” is still around, and still comes in handy sometimes, as you saw in the earlier article. It’s great if you’re in charge of a home or small office network, and setting up a DNS server would be overkill.

The “/etc/hosts” file belongs to the superuser, so you’ll have to use “sudo” to be able to modify it, provided your system has it. Otherwise, you can just use “su” and issue the command as if you were root.

As with any other important system file, it’s a good idea to make a copy of it first as a backup. Then if anything goes wrong, you can just copy your backup over it and you’ll be back to normal.

Here’s how to make the backup:

You can call the file anything you like, however. It’s just a plain ASCII file. And speaking of plain ASCII files, you’ll be editing /etc/hosts in your favorite text editor. Here’s a screenshot of Emacs, just because that’s my favorite.


Yes, my netbook is named after Sam Gamgee from The Lord of the Rings.

Alternatively, you can also use

to edit the file in your terminal.

The format is also almost exactly the same on Unix-like systems as it is on Windows, which, considering that the Internet was implemented on these systems before it was on Windows, isn’t exactly surprising.

I’m not going to spend too much time rehashing the earlier article, but I’ll just point out some of the things you can do with the file.

For example, here’s how you’d block a website from your computer:

And to make a personal shortcut:

By the way, if you want to look up a site’s IP address, you can use the “nslookup” utility, just as you would with windows. Actually, a lot of these Internet utilities first showed up on Unix.

If you want more detailed information on a domain name, you can use this command:

This post should show you that if you can do something on Windows, you can do it in Linux or any other Unix-like system as well.

Image credit: Domain search vector icon. .com .net .org domain finder by Big Stock Photo.