Scenario to Use This Technique ?
When you happen to know the IP address of a domain, but have not assigned a domain for it, AND/OR the website is down due to unknown reason, you are suspecting the issue is domain/DNS resolution, but want to check if the website hosting is working.
Steps to Setup local DNS name resolution ?
(via editing /etc/hosts
file directly)
open terminal, and navigate to the user root folder (
~
directory)type in
sudo nano /etc/hosts
and hit enter (ORsudo vim /etc/hosts
, use whichever editor that you find confortable)you will be prompted for your password, enter that then hit enter again
inside
/etc/hosts
file, you can add/modify/delete your local DNS rule, for instance:New Record
(syntax:
IP_address canonical_hostname [aliases...]
)if you want to resolve
example.com.au
to ip address123.456.789.0
, then create a empty new line and add123.456.789.0 example.com.au
if you want to resolve
hello.com.au
andgoodbye.com.au
both to ip address987.654.321.0
, then create a new empty line and add987.654.321.0 hello.com.au goodbye.com.au
(you may see a rule
::1 localhost
in the file, here::1
is the IPv6 equivalent of the IPv4 loopback address127.0.0.1
)
Remove Record
- if you want to remove the
123.456.789.0 example.com.au
rule, you can either remove the whole line completely, or add a hash symbol#
before the rule to comment it out.
- if you want to remove the
save the file, via
Ctrl+O
+enter
; and close the file viaCtrl+x
(or:wq
if you are usingvim
to open the file)check the local DNS resolver is working via
ping
ordig
command (e.g.ping example.com
)
Purpose of /etc/hosts file ?
The
/etc/hosts
file maps human-readable hostnames to IP addresses, acting as a local DNS for a computer. It allows for local name resolution, overriding external DNS servers. (The system checks the/etc/hosts
file before querying a DNS server. If a match is found, it uses that IP address and does not proceed to the DNS server.)This is useful for developers to test websites on a live server before the domain is made public or for mapping custom domain names to a specific server IP address.