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.
Related: How to check DNS resolution in Linux
Related: How to change DNS servers in Linux
Steps to add a static hostname entry using /etc/hosts in Linux:
- Open a terminal session with permission to run commands using sudo.
$ sudo -v
- Record the current resolution for the hostname before changing anything.
$ getent hosts app.internal.example 203.0.113.50 app.internal.example
- Back up the existing /etc/hosts file.
$ sudo cp /etc/hosts /etc/hosts.bak
- Open /etc/hosts in a text editor as root.
$ sudo nano /etc/hosts
- 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.
- Save the file.
In nano, press Ctrl+O, press Enter to confirm the filename.
- Exit the editor.
In nano, press Ctrl+X.
- 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.
- Restore the backup to remove the override when it is no longer needed.
$ sudo cp /etc/hosts.bak /etc/hosts
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
