A static hostname entry forces a hostname to resolve to a specific IP address immediately, bypassing external DNS and cache delays. This is useful for testing a staging environment, pinning a service to an internal address, or troubleshooting when DNS returns an unexpected destination.

Linux hostname lookups typically go through the Name Service Switch (NSS), which can consult /etc/hosts before (or alongside) DNS based on /etc/nsswitch.conf. Adding a line to /etc/hosts creates a local, static mapping for that machine, and most applications that use standard libc resolution will follow it.

This override is local and does not publish a real DNS record for other systems. Root privileges are required, and incorrect mappings can break connectivity or trigger TLS/HTTPS certificate mismatch errors when a hostname is pointed at the “wrong” server, so entries should be kept minimal and removed when no longer needed.

Steps to add a static hostname entry using /etc/hosts in Linux:

  1. Open a terminal session with permission to run commands using sudo.
    $ sudo -v
  2. Record the current resolution for the hostname before changing anything.
    $ getent hosts app.internal.example
    203.0.113.50    app.internal.example
  3. Back up the existing /etc/hosts file.
    $ sudo cp /etc/hosts /etc/hosts.bak
  4. Open /etc/hosts in a text editor as root.
    $ sudo nano /etc/hosts
  5. Add a line mapping the target IP address to the hostname and any optional aliases.
    192.0.2.40  app.internal.example  app

    Keep the existing 127.0.0.1 and ::1 localhost entries intact, and avoid mapping the same hostname on multiple lines.

    Pointing an HTTPS hostname at a different server commonly causes certificate mismatch warnings and can hide real routing or load-balancer issues.

  6. Save the file.

    In nano, press Ctrl+O, press Enter to confirm the filename.

  7. Exit the editor.

    In nano, press Ctrl+X.

  8. Verify the new mapping is being used by NSS resolution.
    $ getent hosts app.internal.example
    192.0.2.40      app.internal.example app

    Most systems apply /etc/hosts changes immediately, but some applications keep their own DNS cache and may need a restart before the new address is used.

  9. Restore the backup to remove the override when it is no longer needed.
    $ sudo cp /etc/hosts.bak /etc/hosts