Frequently Asked Question
How to add a Hosts entry (etchosts) to fix a domain to an IP
Last Updated about an hour ago
To add a hosts entry for local.example.gen pointing to 192.168.128.10 on a Linux system, follow these steps:
- Open the hosts file with root privileges
Use a text editor such as nano or vim with sudo to modify /etc/hosts:
sudo nano /etc/hosts
- Add the entry
Append the following line to the end of the file, ensuring there is a single space between the IP address and the hostname:
192.168.128.10 local.example.gen
- Save and exit
- In
nano, pressCtrl+Oto write the changes, thenEnterto confirm, followed byCtrl+Xto exit. - In
vim, pressEsc, type:wq, and pressEnter.
- Verify the entry
Check that the entry was added correctly by viewing the file:
cat /etc/hosts
Ensure the line appears exactly as added.
- Test the mapping
Use ping or nslookup to confirm that local.example.gen resolves to the specified IP:
ping local.example.gen
or
nslookup local.example.gen
- Troubleshoot if needed
- If the entry does not take effect, ensure there are no leading/trailing spaces or duplicate entries.
- Confirm the hostname is spelled correctly (e.g.,
local.example.gen, notlocal.example.gen.). - Check for syntax errors in
/etc/hosts(e.g., missing tabs or incorrect formatting). - If changes are not reflected, flush any caching (e.g., restart networking services or flush DNS cache if applicable).
Note: The hosts file is case-sensitive for hostnames, and entries must use a single space (or tab) to separate the IP address from the hostname. Avoid adding extra spaces or characters.
