How to Add Hostname to Etc Hosts Linux

How to Add Hostname to Etc Hosts Linux

To how to add hostname to etc hosts linux, open the /etc/hosts file with root privileges, add an IP address followed by the hostname you want to map, save the file, and verify the change with getent hosts or ping. This creates a local name-to-IP override on that Linux system, it does not update global DNS.

What /etc/hosts does

The /etc/hosts file is a simple local lookup table used by Linux to translate hostnames into IP addresses before, or alongside, DNS depending on your system’s name resolution order. It is useful when you need a quick override for a server name, a lab machine, a test domain, or a hostname that should resolve only on one computer.

Unlike DNS, /etc/hosts applies only to the machine where you edit it. If you add an entry there, other systems will not see it unless they also have the same line in their own hosts file.

How to add a hostname to /etc/hosts

How to add a hostname to etc hosts

Here is the basic process:

  1. Open the hosts file with root privileges. Most Linux systems require sudo because /etc/hosts is a protected system file.
  2. Add the IP address and hostname on one line. The usual format is IP_address hostname.
  3. Save the file. Then exit your editor.
  4. Verify the mapping. Use getent hosts hostname or a similar command to confirm it resolves correctly.

Using nano

If you prefer nano, run:

sudo nano /etc/hosts

Add a line like this:

192.168.1.50 myserver

Save with Ctrl+O, press Enter, then exit with Ctrl+X.

Using vim

If you prefer vim or vi, run:

sudo vim /etc/hosts

Press i to enter insert mode, add your entry, then save and quit with :wq.

Example entries you can copy

The format is simple, but small syntax errors can break resolution. These examples show the most common patterns.

ExampleMeaning
127.0.0.1 localhostMaps localhost to the loopback address.
192.168.1.50 myserverMaps one hostname to one IP address.
192.168.1.50 myserver alias1 alias2Maps multiple names to the same IP address.
203.0.113.10 app.example.localMaps a fully qualified hostname to a specific address.

You can place more than one hostname on the same line. The first name is usually treated as the primary one, and the rest act as aliases.

What the syntax should look like

What the syntax should look like

A correct /etc/hosts entry usually follows this pattern:

IP_address    hostname    alias1 alias2

Whitespace can be spaces or tabs. Comments can be added with #.

192.168.1.50 myserver # test server

Keep the line clean and avoid extra characters. A typo in the IP address or hostname will prevent the mapping from working as expected.

How to verify the mapping

After saving /etc/hosts, check that Linux can resolve the hostname locally.

Use getent

getent is one of the best verification tools because it follows the system’s configured name lookup order.

getent hosts myserver

If the entry is correct, you should see the IP address returned with the hostname.

Use ping

You can also test with:

ping -c 1 myserver

This shows whether the hostname resolves to the expected address. If the host is unreachable, resolution may still be correct, so treat ping as a basic check, not a complete network test.

Use nslookup carefully

nslookup often queries DNS directly and may not reflect your /etc/hosts entry, depending on the tool and system configuration. If you want to confirm the local hosts file is being used, getent hosts is usually a better choice.

Common mistakes and fixes

If the hostname does not resolve after editing /etc/hosts, one of these issues is usually the cause:

  • Wrong file edited: Make sure you changed /etc/hosts on the correct machine.
  • Missing sudo: If the file was not saved properly, your edit may not have been applied.
  • Incorrect format: The line should start with an IP address, then the hostname.
  • Typo in the hostname: A small spelling difference matters.
  • Duplicate entries: Another line in the file may override or confuse the lookup.
  • Resolver order issue: Some systems rely on nsswitch.conf or systemd-resolved settings that affect whether /etc/hosts is consulted first.

Check name resolution order

On many Linux systems, host lookup order is controlled by /etc/nsswitch.conf. If the system is not using files before DNS, your hosts file entry may not be preferred. A typical line looks like this:

hosts: files dns

If your setup is unusual, that file is worth checking.

Be aware of cached results

Some environments use local caching layers such as systemd-resolved or a network service cache. If resolution seems stale, try rechecking with getent hosts, opening a new shell, or restarting the relevant resolver service if your system uses one.

System hostname versus hosts file mapping

System hostname versus hosts file mapping

It is easy to confuse these two settings, but they are different:

  • System hostname: The name your machine identifies itself with, often stored in /etc/hostname or managed with the hostnamectl command.
  • /etc/hosts entry: A local mapping from a name to an IP address.

If you want your computer’s own hostname to change, editing /etc/hosts alone is not enough. If you want a name to resolve to a specific address on this machine, /etc/hosts is the right place.

When to use /etc/hosts instead of DNS

When to use etc hosts instead of DNS

Use /etc/hosts when you need a simple, local override. Good examples include:

  • Testing a server before DNS is updated
  • Working with a lab or development machine
  • Adding a temporary name for a private IP
  • Preventing dependence on external DNS for a single system

Use DNS when the hostname should resolve consistently across many devices, users, or networks. DNS is the better long-term choice for shared infrastructure because it is centralized and easier to manage at scale.

Practical example

Suppose you want fileserver to point to 10.0.0.25 on your Linux machine. You would add this line to /etc/hosts:

10.0.0.25 fileserver

Then verify it:

getent hosts fileserver

If the command returns 10.0.0.25, the mapping is working.

Frequently Asked Questions About how to add hostname to etc hosts linux

How do I add a hostname to /etc/hosts in Linux?

Open /etc/hosts with sudo, add a line in the format IP_address hostname, save the file, and verify it with getent hosts hostname.

What is the format of an /etc/hosts entry?

The standard format is IP_address hostname. You can also add aliases on the same line, for example 192.168.1.50 myserver alias1 alias2.

Do I need sudo to edit /etc/hosts?

Yes, on most Linux systems you need sudo or root access because /etc/hosts is a protected system file.

Why is my hostname not resolving after editing /etc/hosts?

Common causes include a typo, incorrect formatting, editing the wrong file, duplicate entries, or a resolver order that does not consult /etc/hosts as expected.

Should I use /etc/hosts or DNS for hostname mapping?

Use /etc/hosts for local, machine-specific overrides and quick testing. Use DNS when the hostname needs to resolve consistently across multiple systems.

Can I map multiple hostnames to one IP address?

Yes. Put them on the same line after the IP address, separated by spaces or tabs.

Does /etc/hosts change global DNS?

No. It only affects the local Linux system where you edit the file.

Conclusion

Adding a hostname to /etc/hosts on Linux is a fast way to create a local name-to-IP mapping. The key is to edit the file with root privileges, use the correct IP_address hostname format, and verify the result with getent hosts. If the mapping does not work, check for formatting mistakes, duplicate lines, and your system’s name resolution order.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *