Configuring host in linux

How to Setup Local DNS Using /etc/hosts File in Linux

DNS (Domain Name System or Service) is a hierarchical decentralized naming system/service that translates domain names into IP addresses on the Internet or a private network and a server that provides such a service is called a DNS server.

This article explains, how to setup a local DNS using the hosts file (/etc/hosts) in Linux systems for local domain resolution or testing the website before taking live.

For example, you may want to test a website locally with a custom domain name before going live publicly by modifying the /etc/hosts file on your local system to point the domain name to the IP address of the local DNS server you configured.

The /etc/hosts is an operating system file that translate hostnames or domain names to IP addresses. This is useful for testing websites changes or the SSL setup before taking a website publicly live.

Attention: This method will only work if the hosts have a static IP address. Therefore ensure that you have set static IP addresses for your Linux hosts or nodes running other operating systems.

For the purpose of this article, we will be using the following domain, hostnames and IP addresses (use values that apply to your local setting).

Domain: tecmint.lan Host 1: ubuntu.tecmint.lan 192.168.56.1 Host 2: centos.tecmint.lan 192.168.56.10

Understanding Name Service Switch in Linux

Before moving any further, you should understand a few things about another important file that is /etc/nsswitch.conf. It provides Name Service Switch functionality which controls the order in which services are queried for name service lookups.

The configuration is based on order; if files is before dns it means the system will query the /etc/hosts file before checking DNS for name service requests. But if DNS is before files then the domain lookup process will consult DNS first before any other appropriate services or files.

In this scenario, we want to query the “files” service. To check the order, type.

$ cat /etc/nsswitch.conf OR $ grep hosts /etc/nsswitch.conf

Check Name Service Switch

Configure DNS Locally Using /etc/hosts File in Linux

Now open the /etc/hosts file using your editor of choice as follows

Then add the lines below to the end of the file as shown in the screen shot below.

192.168.56.1 ubuntu.tecmint.lan 192.168.56.10 centos.tecmint.lan

Next, test if everything is working well as expected, using the ping command from Host 1, you can ping Host 2 using it domain name like so.

$ ping -c 4 centos.tecmint.lan OR $ ping -c 4 centos

Ping Domain Locally

On the Host 2, we have setup Apache HTTP server. So we can also test if the name translation service is working as follows by going to URL http://centos.tecmint.lan.

Check Domain Locally

Important: To use the domain names on any host on the network, you must configure the above settings in its /etc/hosts file.

What does this mean, in the above example, we only configured the hosts file of Host 1 and we can only use the domain names on it. To use the same names on Host 2, we have to add the addresses and names to its hosts file as well.

Читайте также:  What is memory leak in linux

Lastly, you should use host command or nslookup command to test if the name translation service is actually working, these commands only query DNS and overlook any configurations in /etc/hosts and /etc/nsswitch.conf files.

You may also like to read these following related articles.

That’s it! Do share any additional thoughts or questions about this topic with us, via the comment section below.

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Install Dnsmasq on CentOS

Install Bind DNS Server on RHEL 8

Setup Cache-Only DNS in RHEL and CentOS 7

Install PowerDNS and PowerAdmin in CentOS

Setup Cahing DNS Server in CentOS 7

Cache Only DNS Setup in CentOS

3 thoughts on “How to Setup Local DNS Using /etc/hosts File in Linux”

Linux does provide the bind RPM package to allow an administrative domain to configure and maintain its own DNS service, but frequently the size of the local network is only a few hosts and makes justifying the effort of maintaining a DNS service unwarranted. Reply

Please refer to https://unix.stackexchange.com/questions/421491/what-is-the-purpose-of-etc-hosts . Reply

Hello Aaron, I’m sad to say that in above article you did not describe DNS at all. You instead described how to configure local name resolution on a Linux host through /etc/hosts. In this scenario, DNS is not involved at all. Think of OS name resolution like an umbrella. In nsswitch.conf the sysadmin configures what name resolution services for this system should be. Files usually come first (means /etc/hosts), DNS is one of them – it usually follows files but there are a lot more different name resolution services and strategies available. “getent hosts ” query the OS to resolve according to configured name resolution services in that order (and usually checks /etc/hosts firsts and then maybe DNS, but always does what’s written in nsswitch.conf behind “hosts:”) “host” explicitly asks DNS service to resolve. This won’t resolve hostnames that only /etc/hosts can resolve – to demonstrate the difference. Please read the man pages of “getent” and “hosts“. I suggest completely overhaul this article or take it off-line. Greetings,
Joerg Reply

Источник

How to Edit Hosts File on Linux?

A Hosts file is supported in Linux, Windows, and Mac; they are plain-text files that work to map hostnames to various IP addresses. It is great to edit the Hosts file when you run the test on the specific network. You can also use the mapping of an IP address to skip the process in which the web browser uses the DNS (Domain Name Server) lookup for translating a domain name to a particular IP address.

When a user types a website’s domain name, the domain name needs to translate into a specific IP address. A Hosts file has a top priority over DNS since an operating system checks its Hosts file for a domain and in case there is no entry for that domain. It starts to query the configured DNS servers for resolving the particular domain name. It was the little information about the Hosts file, and we will consider every single aspect on how to edit Hosts files on Linux easily.

Читайте также:  Виды установки в linux

Why Edit Hosts File?

There are various reasons for editing hosts file in the Linux machine:

  • You can easily block a website.
  • You can handle an attack.
  • You can create an alias for different locations on a local server.
  • You can override addresses that a DNS server delivers.
  • You can easily control access to network traffic.

How to Edit Hosts File on Linux?

Open the terminal from applications > utilities > terminal feature, or you can use CTRL, ALT, and T as shortcut keys.

After opening the terminal, open the Linux Hosts file in the system using the following command:

We have used Vim, but you can use different text editors like nano.

Once you execute the command, the system will prompt you to enter the password to open the Hosts file.

You can easily modify the Hosts file, so the file is formatted like the IP address is written first, and the server’s name is on second.

You can add entries at the end of the Hosts file as per your requirements. In case you want to ignore any line or specify the system to not read the line, then you can put the “#” sign at the starting of that particular line. Finally, you can save the file before and make sure you save it before exiting.

Block a Website

If you want to block a particular website from redirecting it to the localhost’s IP address, change the Hosts file’s domain name. For example, we want to block xxyyzz.com, then we can write:

In the above text, 134.1.0.1 is the IP of the localhost, but if you want to make changes in the default route, then you can write:

In the above text, 0.0.0.0 IP is a default route from this particular Hosts file.

At last, you can press CTRL and X keys simultaneously to save the file.

Name Service Switch

As mentioned, a Hosts file works to bypass a Domain Namer server lookup. Still, it has another file that has the information of the operating system about the order for finding the IP address translation.

The nsswitch.conf is a file completely configured for finding the DNS, then skips the Hosts file and goes in the DNS lookup. You can use the below command line for configuration checking easily:

xyz@xyz-virtualBox:~$ cat etc nsswttch.conf
# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the ‘glibc-doc-reference’ and ‘info’ packages installed, try:
# ‘info libc «Name Service Switch»‘ for information about this file.

passwd: compat systemd
group: compat systemd
shadow: compat
gshadow: files

hosts: files mdns4_minimal [ NOTFOUND = return ] dns myhostname files
networks: db files
protocols: db files
services: db files
ethers: db files
rpc: db files

netgroup: nis
xyz @ xyz-VirtualBox: ~$

There is an entry called “Hosts,” ensuring that the files should be listed in the right-hand column in the above results. In case it is not listed like this, then open your file editor using the below command:

This command will open the Vim text editor to make changes and set the files at the beginning for appropriate use.

Conclusion

The Hosts files are beneficial to perform multiple tasks for domain names and IP addresses. It was the complete information on the Hosts file and how you can easily edit the Hosts file on Linux. This procedure is tried and tested on multiple Linux machines, but you have to carefully follow every step.

Читайте также:  Linux mint оформление темы

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.

Источник

Configuring host in linux

NAME

hosts - static table lookup for hostnames

SYNOPSIS

DESCRIPTION

This manual page describes the format of the /etc/hosts file. This file is a simple text file that associates IP addresses with hostnames, one line per IP address. For each host a single line should be present with the following information: IP_address canonical_hostname [aliases. ] Fields of the entry are separated by any number of blanks and/or tab characters. Text from a "#" character until the end of the line is a comment, and is ignored. Host names may contain only alphanumeric characters, minus signs ("-"), and periods ("."). They must begin with an alphabetic character and end with an alphanumeric character. Optional aliases provide for name changes, alternate spellings, shorter hostnames, or generic hostnames (for example, localhost). The Berkeley Internet Name Domain (BIND) Server implements the Internet name server for UNIX systems. It augments or replaces the /etc/hosts file or hostname lookup, and frees a host from relying on /etc/hosts being up to date and complete. In modern systems, even though the host table has been superseded by DNS, it is still widely used for: bootstrapping Most systems have a small host table containing the name and address information for important hosts on the local network. This is useful when DNS is not running, for example during system bootup. NIS Sites that use NIS use the host table as input to the NIS host database. Even though NIS can be used with DNS, most NIS sites still use the host table with an entry for all local hosts as a backup. isolated nodes Very small sites that are isolated from the network use the host table instead of DNS. If the local information rarely changes, and the network is not connected to the Internet, DNS offers little advantage.

FILES

NOTES

Modifications to this file normally take effect immediately, except in cases where the file is cached by applications. Historical notes RFC 952 gave the original format for the host table, though it has since changed. Before the advent of DNS, the host table was the only way of resolving hostnames on the fledgling Internet. Indeed, this file could be created from the official host data base maintained at the Network Information Control Center (NIC), though local changes were often required to bring it up to date regarding unofficial aliases and/or unknown hosts. The NIC no longer maintains the hosts.txt files, though looking around at the time of writing (circa 2000), there are historical hosts.txt files on the WWW. I just found three, from 92, 94, and 95.

EXAMPLE

127.0.0.1 localhost 192.168.1.10 foo.mydomain.org foo 192.168.1.13 bar.mydomain.org bar 146.82.138.7 master.debian.org master 209.237.226.90 www.opensource.org

SEE ALSO

hostname(1), resolver(3), resolver(5), hostname(7), named(8) Internet RFC 952

Источник

Оцените статью
Adblock
detector