Using Your System's hosts File
Updated by Linode Written by Linode
The Linux hosts file lives at
/etc/hosts and it creates static associations between IP addresses and hostnames, domains, or machine aliases. Your Linode then gives those associations higher priority than hostnames or domains which must be resolved by DNS.
Example Host Entries
There are various ways to use the hosts file and the types of associations you set would depend on your specific use case. Below are some examples.
Map the alias
mywebsiteto a given IP address. This is often done when previewing a site during development before the domain is live.
Map the domain
example.comto the given IP address. This is useful when hosting a web or mail server.
Combine the two options above so both the domain and alias map to the IP address:
203.0.113.10 example.com mywebsite
Map the alias
backupserverto the given private IPv6 address:
Block all traffic to and from the domain
example.com. This is frequently used for content filtering or blocking advertisements via a hosts file.
Set a fully qualified domain name (FQDN). In the example below, replace example_hostname with your system’s hostname. The domain example.com can be a public internet domain (ex. a public website), or the domain of a private network (ex. your home LAN), or a subdomain (subdomain.example.com). It’s important to add the FQDN entry immediately after the localhost line, so it looks like below:
127.0.0.1 localhost 127.0.1.1 hostname.example.com example_hostname
A FQDN does not necessarily have any relationship to websites or other services hosted on the server (although it may if you wish). As an example, you might host
www.something.comon your server, but the system’s FQDN might be
The domain you assign as your system’s FQDN should have an “A” record in DNS pointing to your Linode’s IPv4 address. For IPv6, you should set up a “AAAA” record in DNS pointing to your Linode’s IPv6 address. For more information on configuring DNS, see our DNS records guide.
Debian and Ubuntu include a line in their host file for the loopback domain. However, when you change the system’s hostname, the loopback domain should be changed too. If you do not, then you’ll see the message sudo: unable to resolve host when running sudo commands. If you are not using a FQDN like shown above, then all you need to eliminate the sudo message is:
Name Service Switch
etc/nsswitch.conf is important to keep in mind if you choose to rely on your hosts file for domain mapping. To ensure than the system prefers resolving domains listed in your hosts file over DNS resolution, the word
files must appear in the line before
dns. So the line should look as shown below:
[email protected]:~# grep hosts /etc/nsswitch.conf hosts: files dns
man nsswitch.conf from your system’s terminal for more information.
Join our Community
This guide is published under a CC BY-ND 4.0 license.