ubuntu hosts file

The Complete Beginner’s Guide to The Ubuntu Hosts File

On Linux, there are a few features that are hidden from the crow, but can be really useful when you know them. The hosts file on Ubuntu is one of them (and Linux in general, it’s the same on any distribution). By using the hosts file properly, you can save time and do some tests that will only affect your computer.

The hosts file on Ubuntu allows creating redirections from custom host names to specified IP addresses. These will only apply on the current computer, unlike adding them in the DNS server configuration.

But let’s start from the beginning. I chose to write this tutorial in an FAQ format, addressing all the questions you might have, so you can quickly find what you are looking for.

Linux doesn’t have to be intimidating. With my e-book, Master Linux Commands, you’ll uncover the secrets of the terminal in a fun, step-by-step journey. From basics to scripts, get ready to level up your Linux skills. Oh, and did I mention the handy cheat sheet you get as a bonus?

What is the hosts file on Ubuntu, and what does it do?

As a whole, the hosts file on any operating system is used to redirect a domain name or host name to an IP address. It’s a file in text format, with one line per host name or IP address.

If you are not used to network management, I might need to start by explaining the basics. When you type “raspberrytips.com” in your web browser, your computer doesn’t know what to do. Servers have IP addresses, not domain names attached to them.

Your computer will then ask the IP address of “raspberrytips.com” to the DNS server. In general, this role is done by your router or at least forwarded to your provider’s main server. A DNS server is basically a correspondence table between domain names and IP addresses.

Join Our Community!

Connect, learn, and grow with other Raspberry Pi enthusiasts. Support RaspberryTips and enjoy an ad-free reading experience. Get exclusive monthly video tutorials and many other benefits.

Learn more

Once your computer, and so the web browser, knows the IP address, the request can be sent to the corresponding server and the page content displayed.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

The hosts file on Ubuntu does basically the same thing, without having to change anything in the DNS server configuration. You can use this file to redirect a domain name to an IP address (on your local network or the Internet).

When should I use the hosts file on Ubuntu?

The hosts file is particularly useful for local servers, to give them a name, making it easier to access them from your computer (without having to remember and type the full IP address).

For example, if you have a file server at home, you can create a custom hostname in your hosts file (let’s say “mynas”) and add this name to the hosts file, with the corresponding IP address.

Once done, you can directly type http://mynas in your web browser to get the interface, or \\mynas in your file explorer to see files. You won’t need to remember the IP address anymore.

The hosts file on Ubuntu can also be used to test servers before changing the IP address in the DNS server. As the hosts file only applies to the current computer, redirecting “raspberrytips.com” to a test IP address won’t break the site access for other users, only you will see the new hosting server.

Where is the hosts file on Linux?

On any Linux distribution, the hosts file is located under /etc: /etc/hosts. It’s a simple text file that can be opened with any editor (administrator privileges are required to edit it).

You can see the current content of the hosts file with:
cat /etc/hosts
You don’t need sudo to read the file.

As you can see on the screenshot, the file format is pretty straightforward, but let’s see how to edit and use it anyway.

Are you a bit lost in the Linux command line? Check this article first for the most important commands to remember and a free downloadable cheat sheet so you can have the commands at your fingertips.

How do I edit /etc/hosts on Ubuntu?

Administrator rights are required to edit the /etc/hosts file on Ubuntu. Use “sudo” to open the file from a terminal:
sudo nano /etc/hosts

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

The file content on Ubuntu looks like:

There is a first column with the IP addresses and the second with the corresponding host names. You can either use hostname directly (myserver), or domain names (domain.com or sub.domain.com). IP addresses are generally set in IPv4 format, but IPv6 is supported in recent versions.

On the previous screenshot, I added a custom hostname (“proxmox”) to my hosts file. Proxmox is a hypervisor you can install on Ubuntu (click on the link to learn more about this). Anyway, if I want to access the interface directly with http://proxmox, I add it to my hosts file, and now my system knows this name:

Just after saving the file, a ping to the hostname I added in the file will redirect to the corresponding IP address.

Play a bit with it, try to add a few host names in your file, and see how it works. By the way, if you want to assign several custom names to one IP address, you can either add one line per hostname or put all of them on the same line, something like this will work:
192.168.1.23 proxmox myserver webserv

What’s the difference between the hosts file and a DNS server?

The main difference is that the DNS server is used by all computers on the network, while the hosts file only affects the current computer. You can see the hosts file as an override of the DNS server, for custom redirections. A DNS server can also handle other types of entries (CNAME, MX, etc.).

If a host or domain name is set in the DNS server and in the hosts file, the operating system will always use the hosts file. So if you are using the hosts file for testing purposes, remember to remove the entries from it once you’re done with your tests.

If you are a bit confused about how DNS servers work, I have a full article on how to set up a DNS server on Raspberry Pi. Even if you are not interested in the project (or don’t have a Raspberry Pi), you’ll learn many important things by reading it.

Can I delete the hosts file on Linux?

The hosts file on Linux is a system file, so you shouldn’t remove it. However, you can edit it and remove the lines that you don’t want to use anymore.

  • Open the hosts file with:
    sudo nano /etc/hosts
  • Find the line you want to remove and press CTRL+K (to cut the line) or DEL (to remove characters one by one).
  • Save and exit (CTRL+O, Enter, CTRL+X).

Once done, the custom name that you removed from the file shouldn’t work anymore, or at least it will check the DNS server as usual, not the hosts file.

Do I need to restart the computer after changing the hosts file?

As a general rule, any change to the hosts file on Linux will be effective immediately. Some apps may have a caching system, requiring you to restart them or purge the cache to see the changes.

For example, if your web browser knows the IP address of raspberrytips.com, even if you add a custom IP to your hosts file, it won’t necessarily point to it on the next page you try to load. A restart will often be required to make sure it’s using the hosts file and not the IP address in the cache.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

What if the hosts file is not working?

If the hosts file is not working on Ubuntu, it’s most likely due to a syntax error in the file or a caching system used by the application you are using.

A good test, in this case, is to try the “ping” command in a new terminal or SSH session:
ping yourhost
If it’s working, it’s probably a cache issue. Purge the cache, restart the app, or even your system to reset it.

If the ping is not working, check your syntax. Compare your file content with the previous screenshots in this article. You can even create a backup of your file, restart from scratch with the default content, and redo all your changes one by one, to see at which point it breaks.

Liked this article? Here are a few other ones you might be interested in:

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Want to chat with other Raspberry Pi enthusiasts? Join the community, share your current projects and ask for help directly in the forums.

Additional Resources

Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.

VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

Similar Posts