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
$ 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.
$ 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.
$ sudo cp /etc/hosts /etc/hosts.bak
$ 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.
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.
In nano, save with Ctrl+O, confirm the filename, then exit with Ctrl+X.
$ 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.
$ getent hosts app 192.0.2.40 app.example app
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.
$ sudoedit /etc/hosts
Prefix the line with # to disable it without deleting it, or restore the backup when the whole file should roll back.