Link to home
Start Free TrialLog in
Avatar of mac_g
mac_gFlag for Saudi Arabia

asked on

Linux : when to add the entries in /etc/hosts file

when should we add the entries into the /etc/host file ?

1) when the DNS server failed resolve the IP's (or)
2) Despite having the DNS server working, add entries for just for quick resolution ?
3) which one will have priority in resolving IP's, DNS server/host entries defined in file ?


please advice ...
Avatar of Kent W
Kent W
Flag of United States of America image

Normally, the /etc/hosts file should have nothing in it save for possibly "127.0.0.1 localhost".

Normal operations, if you have some other DNS issues, using the hosts file is simply a work-around, but I would not rely on it, as DNS is there for a reason. IPs change, etc.

The day-to-day use I employee for the /etc/hosts file is testing. For instance, you have a dev box and you need to hit it with a FQDN for testing purposes, you can then use the hosts file to circumvent DNS (/etc/resolv.conf), and force a particular host or hosts to a certain IP.

Outside of that, or some very specialized uses, if you have to use the hosts file to get around other issues, you have a larger problem that needs to be addresses.

I would not practice using the hosts file just to speed up resolution. If resolution is slow, you have bigger fish to fry.

Your /etc/hosts file will override /etc/resolv.conf (system DNS servers)
I use the hosts file here to convert host names to IPs because there is no DNS server for my network.
Avatar of mac_g

ASKER

The configured DNS server not resolving certain server-ip's  properly.
Can I use the host file in such cases to resolve ?

please advice
Yes, you can. You will need to know the IPs, as the /etc/hosts file entry requires the IP in order to point it to the host.  Be aware that hosts with dynamic DNS may fail at times.

The best practice is to figure out what's happening with your DNS servers, though, unless they are supposed to act this way by design (for security purposes, etc.)
Avatar of mac_g

ASKER

recently our DNS server moved to new HOSTNAME  & IP, since then applications deployed on web-server is not working properly as it is using some references to other 2-network servers.

for quick resolution i would like to add those server details in host file.
is it advisable ?
Most likely those servers have fixed IP addresses and references to the old DNS in their settings... It might take the same amount of time to fix that as to edit hosts files...
Avatar of mac_g

ASKER

servers with fixed Ip's only !!
Can you not edit your /etc/resolv.conf file to point to the new, updated Name Servers Ip addresses?

That would be the preferred fix, rather than "faking it" with the hosts file.
Avatar of mac_g

ASKER

after updating resolve.conf, when I rebooted the server, again old DNS server details resurfaced in resolve.conf. As quick resolution I would like to go with /etc/host file.
please advice
THen you have something updating your resolv.conf.
YOu do not need to reboot to effect changes to resolv.conf.

If you can provide your OS and version, we may be able to help you fix this the correct way.

If you want to go with your hosts file, then you will manually have to enter every IP / Host that you want to resolve, in the format -

Ip.add.re.ss   hostname

such as
216.58.192.164  www.google.com

You will have to, most likely from another machine, manually look up each IP you want to enter.
Avatar of mac_g

ASKER

OS : Oracle Linux 5
Is this machine given an IP address via DHCP?
Avatar of mac_g

ASKER

this is server, obviously fix IP
Not necessarily. I extensively use DHCP reservation via Mac address to hand out the same IP to a class of servers.

That being said, if you run system-config-network-tui or system-config-network, then select "DNS Configuration", you can permanently change your name servers for the system.

If you change the /etc/resolv.conf and don't reboot, does the system resolve correctly?
Avatar of mac_g

ASKER

-- /etc/resolv.conf and don't reboot, -  it is resolving properly.
--system-config-network-tui or system-config-network,  - also not able to male the changes permanently.
Can you dump the contents of your /etc/sysconfig/network-scripts/ifcfg-eth0 file here? (if the ifcfg-ethX file that you are currently setting your network params with)

Think I found a solution, but don't want to send you down a rabbit hole.
Run setup and edit your DNS in the network settings.
ASKER CERTIFIED SOLUTION
Avatar of Kent W
Kent W
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mac_g

ASKER

thanks it is working as you said.
please let me know,  command to restart networking service ?
Avatar of mac_g

ASKER

thanks it is working as you said.
- please let me know,  command to restart networking service ?
- is there any additional steps are required like , flush/update dns info, remove cache details of Old dns details from server ......etc, which can impact the performance of the applications running on the server. ?
you should be able to do a
service network restart
or
/etc/init.d/network restart

There is nothing else you would really need to do as far as flushing cache, etc.
Avatar of mac_g

ASKER

after OS, and WebServers installed on it , configured with new DNS details as you said above,  application login process taking little time.

for testing purpose,  I asked AD admin to UP the old DNS server for 5 min for testing.
again, I can see performance of the application very fast as before.

Not sure where to look for this issue, whether any network related work also be required from our Network Admins, like routing ..etc.    please advice  ?