How to edit the hosts file in Linux

The /etc/hosts file lets a Linux system map a hostname to a specific IP address before the resolver asks an external DNS server. That local override is useful for testing a cutover, forcing a service name to an internal address, or keeping a temporary mapping in place while a public or private DNS change is still propagating.

Hostname lookups usually pass through the Name Service Switch defined in /etc/nsswitch.conf. When the hosts: lookup order includes files before dns, entries in /etc/hosts are read first, so most applications that use the normal libc resolver follow the local mapping without querying a DNS server.

The change affects only the local machine, and a wrong entry can send traffic to the wrong service or trigger HTTPS certificate mismatch errors. Keep the default localhost lines intact, avoid placing the same hostname on multiple active lines, and remember that some browsers, runtimes, or long-running daemons keep their own resolver cache and may need a restart before they pick up the new address.

Steps to edit the hosts file in Linux:

  1. Review the active, non-comment entries in /etc/hosts before making changes.
    $ grep -Ev '^(#|$)' /etc/hosts
    127.0.0.1	localhost
    127.0.1.1	host.example host
    ::1	localhost

    The exact default entries vary by distribution, but the localhost lines should remain in place.

  2. Create a backup copy of /etc/hosts so the previous mapping can be restored quickly if needed.
    $ sudo cp /etc/hosts /etc/hosts.bak
  3. Open /etc/hosts with elevated privileges in the system editor configured for sudoedit.
    $ sudoedit /etc/hosts

    sudoedit edits a temporary copy and writes it back as root after the file is saved, which avoids running the editor itself with full root privileges.

  4. Add a new mapping or change the existing line for the target hostname so it appears only once in the file.
    192.0.2.40 app.example app

    The first field is the IP address, the second is the canonical hostname, and any remaining names on the same line are aliases separated by spaces or tabs.

    Do not remove existing localhost entries such as 127.0.0.1 and ::1, or the local-hostname line if the distribution includes one.

  5. Save the file and exit the editor.

    In editors such as nano, save with Ctrl+O, confirm the filename, then exit with Ctrl+X.

  6. Confirm that the new mapping is now returned by the system resolver.
    $ getent hosts app.example
    192.0.2.40      app.example app

    getent hosts follows the same NSS lookup path used by most applications, while tools such as dig and nslookup query DNS directly and do not prove that /etc/hosts is being used.

  7. If the new mapping is ignored, inspect the hosts: line in /etc/nsswitch.conf and confirm files still appears before dns.
    $ grep -E '^hosts:' /etc/nsswitch.conf
    hosts:          files dns

    Distributions that use mDNS or additional resolver modules may include more sources, but /etc/hosts is only consulted when files is present.

  8. Remove the override or comment it out after the temporary test or cutover is finished.
    $ sudoedit /etc/hosts

    Prefix the line with # to disable it without deleting it, or restore the backup copy if the entire file should be rolled back.