theme: ricetheme
New palette
New palette

Linux Hosts File Guide: Entries, Edits, and Lookup Order

The Linux hosts file maps names to IPs before DNS; see how to format entries, edit /etc/hosts safely, and use nsswitch.conf to troubleshoot changes.

A flat illustration showing a network packet traveling from an app through /etc/hosts to DNS, depicted as a terminal window with glowing paths.

tl;dr the short answer

The Linux hosts file is a plain-text list that connects hostnames to IP addresses, letting your computer look up selected names locally instead of relying only on DNS. Add one address followed by a hostname and any aliases, and remember that the lookup order in your system configuration determines whether the entry is used. Back up the file before editing and test changes afterward, so you can use memorable local names or preview a site on a new server without changing public DNS.

Think of a sticky note on your fridge that reads ‘Mom: 555-0142.’ You skip the contacts app and just dial. Your Linux hosts file works the same way: it pins a hostname to an IP address, and it’s usually checked before your system asks the wider internet.

The file lives at /etc/hosts, and each line pairs an IP address with one or more names. This guide covers how it’s read, how to write entries that behave, and how to edit it from the terminal without locking yourself out.

We’ll also look at nsswitch.conf and systemd-resolved, since those decide whether your edits actually take effect.

How the Linux Hosts File Works

Before your machine asks anyone else for an address, it can check a small file of its own.

What the hosts file is for

The hosts file is a plain text file that maps hostnames to IP (Internet Protocol) addresses. Each line pairs an address with one or more names. It’s a static lookup table you edit by hand, not a live service. Simple, and that’s the point.

On Linux, the file lives at /etc/hosts, and on Ubuntu it sits in that same spot. To find it, run ls -l /etc/hosts in a terminal. Its location also raises the question of which file system Linux uses.

Its role next to DNS today

DNS (Domain Name System) has largely superseded the hosts file, but it still has jobs to do. It handles bootstrapping, when a name must resolve before DNS is running, and it can serve as a backup host table for NIS (Network Information Service).

Isolated small networks are where it still shines. Think of a homelabber running a self-hosted cloud on a Raspberry Pi, on a network with no DNS server.

Adding 192.168.1.20 cloud.home to /etc/hosts lets the laptop reach it by name, with no DNS setup needed.

BIND, the DNS server software, can augment the hosts file or replace it. The lookup order is set in the name service switch configuration, /etc/nsswitch.conf, so check its hosts line if a name resolves oddly.

Where the hosts file came from

Long before DNS, names were resolved in simpler ways, and the hosts file was one of them. RFC 952, one of the numbered Request for Comments documents, set its original format.

The NIC (Network Information Center) hosts.txt files were an early version of the same idea. Old idea, still in use.

Writing Entries in the Linux Hosts File

Each entry is one short line, so once you know the pattern, writing your own takes seconds.

What goes on each line

Every entry starts with an IP (Internet Protocol) address, followed by its canonical name, the main name for that host, and then any aliases, which are extra names for the same address.

Keep one line per IP address, and separate the fields with spaces or tabs. Anything after a # is a comment, so use it freely to label your entries.

Aliases are extra names, not separate machines: every name on the line points to the same address.

Terminal window showing hosts file entries linking IP 10.0.0.12 to db.internal, database, and pg, connected to a single server with matching name tags.

Which characters a hostname can use

A hostname can only use letters and digits (alphanumerics), hyphens, and periods. Two more rules apply:

  • Begin with an alphabetic character.
  • End with an alphanumeric character.

Periods separate the parts of a fully qualified domain name (FQDN), the complete name that includes its domain. Aliases are handy here: a long FQDN can carry a short alternate name, so you type less.

Sample entries for common setups

Take a self-hoster with a network-attached storage (NAS) box on the home network. Without an entry, they type its IP every time. With one line, they can run ping nas instead of ping 192.168.1.25. Fewer numbers to remember.

Common setups look like this:

  • Localhost: 127.0.0.1 localhost maps your own machine to the name localhost.
  • Local host with FQDN and alias: 192.168.1.25 nas.home.lan nas gives the NAS a full name and a short alias.
  • Custom domain pointed to an IP: 10.0.0.15 dev.example.test sends a name you choose to a development box.

Making Changes to the Hosts File

Editing the hosts file is simple, and a few habits keep it safe.

Editing it on Linux and Mac

Edit it in a terminal, or over Secure Shell (SSH) on a remote machine. Because root owns the file, run your editor with sudo, or saving will fail.

Say a homelab tinkerer wants nas.lan to point at her file server. She runs sudo nano /etc/hosts, adds one line, then checks with getent hosts nas.lan. Now her file share loads by name.

Save commands:

  • nano: press Ctrl+O and Enter to write, then Ctrl+X to exit.
  • vim: press i to edit, then Esc, type :wq, and press Enter.

On Linux, verify with getent hosts followed by the name. To block a domain, add 0.0.0.0 before it, like 0.0.0.0 ads.example.com. Requests then go nowhere, so they fail.

Is it safe to delete the hosts file? Not really, because your system expects it to exist. The goal is to change it, not delete it. Back it up first with sudo cp /etc/hosts /etc/hosts.bak if you want a safety net.

A tinkerer adds a NAS entry to their hosts file, connecting to a server icon in a dark terminal interface.

Doing the same on Windows

Notepad needs administrator rights to save this file. Follow these steps:

  1. Right-click Notepad and choose Run as administrator.
  2. Choose File, then Open, and paste C:\Windows\System32\drivers\etc\hosts into the File name box.
  3. Set the filter to All Files, since the hosts file has no extension.

The steps are identical on Windows 7 and 10.

When edits take effect

Changes are usually immediate. The exception is caching: browsers and other apps can hold an old address until they restart.

To flush the cache, run resolvectl flush-caches if systemd-resolved handles lookups, or sudo nscd -i hosts if you use the name service cache daemon (nscd). Flush, then test.

Setting Localhost and Your Hostname

Your hosts file needs a few lines that name your own machine. Here’s what they mean and how distributions set them up.

What does 127.0.0.1 mean?

So what is 127.0.0.1? It’s the loopback address, a host address that always points back to your own machine. It’s less about reaching a server and more about where the traffic stays. Nothing leaves the box. To see the addresses your own interfaces hold, the IP commands are the next step.

Your hostname becomes the canonical name, the main name your machine answers to. It also shows up in syslog, the system log where services write their messages, so you can tell which machine wrote each line.

Give your interface address its own line, apart from the loopback line, so each name points where it should.

Say you’re a student on a ten-year-old ThinkPad with a local development server you want to reach by name. One line pointing that name at 127.0.0.1 lets your browser and scripts find it every time, even offline.

How distributions usually set it up

If you installed Arch, the installer and the Arch wiki both give you defaults to start from. Debian forum guidance covers similar ground, so read both before you edit.

Here’s how to tell a correct layout from a broken one:

  • Correct: each address on its own line, with its names after it.
  • Incorrect: several addresses crammed onto one line, or an entry with no name.

Four-part entries get questioned too, so don’t copy one just because it looks official. Check whether your setup needs it.

Practical Uses and Other Options

Editing the hosts file is most useful when you want to see a site before the rest of the world does, so we’ll start there. A few other options get you a preview without touching the file at all.

Previewing a site before DNS moves

Say you’re moving your blog to a new virtual private server (VPS). Before you change the domain’s DNS (Domain Name System) records, you can add a line to your hosts file that points the domain at the new server’s IP address.

This is a local override: your machine loads the new site while everyone else still reaches the old one. You see the new site before anyone else does. Your readers notice nothing.

Here’s the routine:

  1. Add a line with the new server’s IP address and your domain name.
  2. Load the site in your browser and click through the pages that matter.
  3. Remove the line when you’re finished, so your machine goes back to normal lookups.

A developer previews a new blog on a laptop while old server traffic continues elsewhere, visualized via split paths and hosts file edits.

Ways to preview without editing

Picture a Linux user who just rebuilt their machine from a fresh install and wants one quick look at a staging server. A one-off preview means no stray hosts entry to hunt down later.

Try these instead:

  • cPanel IP/~username URL. If your host runs cPanel, you may be able to reach your site through the server’s IP address followed by /~ and your account username.
  • Skipdns.link tool. The Skipdns.link tool is another option if you’d rather not change anything locally.
  • Preview without a hosts file. You get the look without any change to clean up afterward.

Where to Read More

Most of what you need is already on your machine, so you rarely have to leave the terminal. Here’s where to look and what each source is good for.

Man pages and the RFC 952 spec

Start with the man pages, the built-in reference that ships with nearly every Linux install. Run man hosts to read about the file itself, then check the neighboring pages:

  • man hostname covers how a machine names itself, handy when your shell prompt and your bar show different names.
  • man resolver covers how a name gets turned into an address.
  • man named covers the named name server, which matters once you run DNS on your own network.

Consider a homelabber who points a few machines at a new name in their hosts file, then finds one box still reaching the old address. A quick look at the resolver page is a faster start than guessing.

RFC 952 is the original specification for host names and the host table format. It’s dense, but it’s the rulebook the format was built on.

Read the man pages to learn how the file behaves, and read the RFC to learn why it was designed that way. Skim it once, then keep it bookmarked.

The hosts file stays simple on purpose: plain text, one mapping per line, and a lookup order you can check yourself. Once you know where it lives, how entries are written, and how to edit it safely, it becomes a tool you control.

Keep the habits that prevent trouble: back up before you change anything, comment your entries, and run a quick lookup after every edit.

Pin the names that matter, and let everything else find its own way.

Questions

What is 127.0. 0.1 host?

127.0.0.1 is the loopback address, which points back to the computer using it. Traffic sent to this address stays on that machine rather than going out to the network.

How do I find the hosts file?

On Linux, the hosts file is at `/etc/hosts`. Find it from a terminal with `ls -l /etc/hosts`.

Where are etc hosts in Ubuntu?

In Ubuntu, the hosts file is located at `/etc/hosts`. You can view or edit it there, using `sudo` for changes.

Is it safe to delete the hosts file?

No. The system expects the hosts file to exist, so change it rather than deleting it. Back it up first with `sudo cp /etc/hosts /etc/hosts.bak`.