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.
Related: How to check DNS resolution in Linux
Related: How to change DNS servers in Linux
$ 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.
$ 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.
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.
In editors such as 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 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.
$ 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.
$ 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.