This post will guide you set up local DNS using /etc/hosts file in your Linux system. How do I modify your hosts file to resolve your hostname to ip address on Linux operating system.
- What is Host File
- Modify Host File to Configure DNS Locally
- Block Domain Name with Modifying Host File
What is Host File
The Hosts file is used to resolve domain name or Hostname to IP addresses. When you access a website using it domain name in your web browser, you system will check the resolution of domain name from your hosts file locally to get the corresponding IP address, and then download the web page. If there is no any entry for the domain name, and it will query the configured DNS servers to resolve the specified domain name.
All operating systems have a hosts file to translate hostnames to IP addresses, such as: windows, Mac and Linux.
The hosts file also can be used to test your website, and if you do not want to change your domain DNS settings.
Modify Host File to Configure DNS Locally
If you want to add one Domain name resolution into hosts file, you just edit the hosts text file located at /etc/hosts with a sudo user using vi or vim text editor.
You need to follow the following format to add DNS entries into your /etc/hosts configuration file:
IPADDRESS DomainName [ShortName]
Note: the IP address and the domain name should be separated by at least one space character. If you do not want ignore one hosts entry, just add # to comment it.
For example, add domain name osetc.com and ip address 192.168.3.22 to your hosts file, type:
$ vim /etc/hosts 192.168.3.22 osetc.com
Save and close the file. Let’s try to ping your server with domain name, type:
$ ping osetc.com
Outputs:
[devops@devops ~]$ ping osetc.com PING osetc.com (192.168.3.22) 56(84) bytes of data. 64 bytes from osetc.com (192.168.3.22): icmp_seq=1 ttl=64 time=0.034 ms 64 bytes from osetc.com (192.168.3.22): icmp_seq=2 ttl=64 time=0.038 ms 64 bytes from osetc.com (192.168.3.22): icmp_seq=3 ttl=64 time=0.038 ms 64 bytes from osetc.com (192.168.3.22): icmp_seq=4 ttl=64 time=0.040 ms ^C --- osetc.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 2999ms
Block Domain Name with Modifying Host File
You can use hosts file to block one domain name by redirecting it to the ip of your localhost.
For example, if you want to block domain name bing.com, you can add the following DNS entry into your hosts file with vim text editor:
127.0.0.1 bing.com
or
0.0.0.0 bing.com
Save and close the file. when you try to access bing.com site, it will translate bing.com domain to ip address of your localhost. And you will get error message as “Unable to connect to bing.com”
Conclusion
You should know that how to list shell variables and environmental variables in your shell session on your CentOS or RHEL Linux 6/7.