A hosts-file entry lets one Linux machine resolve a hostname to a chosen IP address before normal DNS results are used. That local override is useful during migrations, split-environment testing, or short cutovers where only one workstation or server should see the temporary address.
The /etc/hosts file uses one mapping per line in the form IP address, canonical hostname, and optional aliases. Most applications reach that file through the Name Service Switch order in /etc/nsswitch.conf, so the override works when the hosts database includes files before dns or another network resolver source.
The change affects only the local machine. A wrong address can send traffic to the wrong service, and an HTTPS site may still reject the connection when the certificate does not match the hostname. Keep the localhost lines intact, avoid duplicate active entries for the same hostname, and restart long-running clients if they keep an old lookup result in memory.
Related: How to check DNS resolution in Linux
Related: How to change DNS servers in Linux
Steps to edit the hosts file in Linux:
- Check that local files are part of the host lookup order.
$ grep '^hosts:' /etc/nsswitch.conf hosts: files dns
files means the resolver can read /etc/hosts. When files appears before dns, local hosts-file entries win before external DNS answers.
- Review the current /etc/hosts entries before making changes.
$ cat /etc/hosts 127.0.0.1 localhost 127.0.1.1 workstation.example workstation ::1 localhost ip6-localhost ip6-loopback
Do not remove existing localhost entries such as 127.0.0.1 and ::1, or the local-hostname line if the distribution includes one.
- Create a backup copy of /etc/hosts so the previous mapping can be restored quickly if needed.
$ sudo cp /etc/hosts /etc/hosts.bak
- 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.
- Add one active line for the target hostname.
127.0.0.1 localhost 127.0.1.1 workstation.example workstation ::1 localhost ip6-localhost ip6-loopback 192.0.2.40 app.example app
The first field is the IP address, the second field is the canonical hostname, and the remaining names are aliases separated by spaces or tabs.
- Save the file and close the editor.
In nano, save with Ctrl+O, confirm the filename, then exit with Ctrl+X.
- Confirm that the canonical hostname resolves through the system resolver.
$ getent hosts app.example 192.0.2.40 app.example app
getent hosts follows the NSS lookup path used by many applications. Tools such as dig and nslookup query DNS directly and do not prove that /etc/hosts is being used.
- Confirm the alias if the line includes one.
$ getent hosts app 192.0.2.40 app.example app
- Restart any long-running client that still uses the old address.
Browsers, language runtimes, and daemons can keep resolver results in process memory. Restart only the affected client or service instead of rebooting the whole system.
- Remove or comment out the temporary override after the test or cutover is finished.
$ sudoedit /etc/hosts
Prefix the line with # to disable it without deleting it, or restore the backup when the whole file should roll back.
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.