LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Adding hostname and its IP into its own /etc/hosts file (https://www.linuxquestions.org/questions/linux-newbie-8/adding-hostname-and-its-ip-into-its-own-etc-hosts-file-4175592511/)

fanoflq 10-29-2016 11:48 PM

Adding hostname and its IP into its own /etc/hosts file
 
Adding hostname and its ip into its own /etc/hosts file

Say I created hostname:
>hostnamectl set-hostname server1.test.com

Let say it IP address is 192.168.200.100

Is there a valid reason to include into its own /etc/hosts file like so:

192.168.200.100 server1.test.com

What use case by server1.test.com would need server1.test.com's IP address to be in server1.test.com's /etc/hosts?

Addendum: Pinging itself does not count as a good use case.

jpollard 10-30-2016 09:31 AM

Yes, there are reasons.

There are utilities that may look up an IP number before a network is even initialized, and frequently what they will do is use the hostname under the assumption that it also has been assigned an IP number.

There is an assumption being made that the "hostname" must be the same as the "network name". This IS a convention, but it doesn't have to be true.

My host has many addresses- 192.168.0.8, 192.168.1.3 (currently), 192.168.122.1 (for communicating with virtual machines)

Yet none of these are given my hostname other than 192.168.0.8.

127.0.0.1 is given "localhost"... as is ::1 (IPv6 local host).

No names HAVE to be put in /etc/host file. I do it because I copy the /etc/host to other systems so that they can contact each other by a commonly understood name. But that "commonly understood name" does NOT have to be the same as the "hostname".

This COULD be done by a name server - but then the assumption is that the name server is always available... (not necessarily true). So I just copy the /etc/hosts file around. This works only because I have very few systems to do it with (3 physical, and sometimes 3 active virtual systems).

jefro 10-31-2016 05:37 PM

Hosts files for lan computers tend to have all the computers with the same hosts file. Makes it easier to maintain.

You'd put the local name in the local hosts file maybe for some lookup tasks that refer back to the host. Could be quite a few.

tronayne 11-01-2016 09:00 AM

Here's a example of a "real" /etc/hosts file:
Code:

cat /etc/hosts
#
# hosts                This file describes a number of hostname-to-address
#                mappings for the TCP/IP subsystem.  It is mostly
#                used at boot time, when no name servers are running.
#                On small systems, this file can be used instead of a
#                "named" name server.  Just add the names, addresses
#                and any aliases to this file...
#
# By the way, Arnt Gulbrandsen <agulbra@nvg.unit.no> says that 127.0.0.1
# should NEVER be named with the name of the machine.  It causes problems
# for some (stupid) programs, irc and reputedly talk. :^)
#

# For loopbacking.
127.0.0.1                localhost
# For public and private OpenSRF comains
127.0.1.2      public.localhost        public
127.0.1.3      private.localhost      private
# Local servers
192.168.1.10                fubar.lan fubar
192.168.1.15                OfficeJet
192.168.1.20                snafu.lan snafu
192.168.1.30                pita pita.lan
192.168.1.2                hicl01.lan hicl01
# Public servers
75.126.162.205                www.linuxquestions.org        linuxquestions.org
74.125.224.70                maps.google.com
74.125.224.111                maps.gstatic.com

# End of hosts.

The "local servers" are those on my LAN, all have fixed IP addresses. Note the server name; e.g., fubar.lan and fubar. Just "fubar" is an alias that allows you to simply use that name rather than "fubar.la" when connecting with SSH and the like.

Officejet is an Ethernet connected HP printer that serves everything on the LAN (note that it's fixed-IP too, you don't want a printer running DHCP if you don't have to).

The "public servers" are frequently-used addresses -- why? because you don't go though a DNS lookup to get there quickly (/etc/hosts is the first place looked at for and address).

Every machine on the LAN has and identical /etc/hosts file (yeah, the local machine address is there too -- doesn't matter and can be handy).

Works just fine (for... well, decades).

Hope this helps some.

fanoflq 11-01-2016 09:47 AM

@tronayne:

Thank you.
Quote:

# ... It is mostly
# used at boot time, when no name servers are running.
# On small systems, this file can be used instead of a
# "named" name server. ...
What is "no name server" versus "named" name server?
Please provide some examples.
Thanks.

fanoflq 11-01-2016 09:54 AM

We have /etc/hosts mapping of IP and its hostname.
But this hostname do not have to be the same as that created by hostnamectl.
Thus in this case you can have 2 different hostnames, if you
have host's IP in the host's /etc/hosts points mapped to some arbitary name other than those created via hostnamectl.

When is hostname (created by hostnamectl) ever used and by whom?

Thanks.

suicidaleggroll 11-01-2016 10:21 AM

Quote:

Originally Posted by fanoflq (Post 5625657)
When is hostname (created by hostnamectl) ever used and by whom?

In my experience it's mostly for logging. SSH around to a lot of computers on a regular basis? Look at your prompt and you'll see which one you're currently on. Same goes for consolidating log files, each one will contain the hostname of the computer it came from. Status emails will come from the hostname of the machine that sent it (unless you change it), etc.

There may also be a networking use, but I don't know what it is.

tronayne 11-01-2016 10:24 AM

Quote:

Originally Posted by fanoflq (Post 5625655)
@tronayne:

Thank you.

You're welcome.


What is "no name server" versus "named" name server?
Please provide some examples.
Thanks.

A named server would be a DNS server with the address listing in /etc/resolv.conf, for example, this is my list of external DNS servers:
Code:

cat /etc/resolv.conf
search com
#nameserver 68.94.156.1
#nameserver 68.94.157.1
# Google Free DNS Servers
#nameserver 8.8.8.8
#nameserver 8.8.4.4
# HughesNet DNS Servers
nameserver 66.82.4.8
nameserver 66.82.4.12

The only two active DNS servers are the HughesNet ones (because I have satellite service), the others are simply commented out and ignored except when I'm using a laptop remotely and need to connect to, most likely, the two Google DNS servers. I keep the /etc/resolv.conf file identical on all my systems, same as keeping /etc/hosts identical on all systems.

So, the named servers are those and the no named are those addresses in /etc/hosts.

The way things work is that, when you enter a name of an external device the first place that's looked at is /etc/hosts then, if your network daemon is active, the first nameserver in /etc/resolv.conf, if that's not available, the next nameserver is looked at (after a long delay from the first). You don't want to have a bunch of nameservers -- most DNS servers have two addresses and if one fails the other is tried; you sit for a long time waiting for a long list of nameservers to be examined if one or more are not available, so use reliable nameservers in your /etc/resolv.conf file.

I have that list pretty much for the laptop being used somewhere or other and I know the DNS server addresses of places I go with it. You would, most likely, need only two nameserver entries in your file (and you do not what LAN servers in /etc/resolv.conf.

Hope this helps some.

lazydog 11-01-2016 11:54 AM

You got me interested.

Quote:

Originally Posted by tronayne (Post 5625676)
The only two active DNS servers are the HughesNet ones (because I have satellite service), the others are simply commented out and ignored except when I'm using a laptop remotely and need to connect to, most likely, the two Google DNS servers.

You are aware that you don't have to use your ISP's DNS servers and can use what ever servers you like? I have my systems setup to use 3 DNS server from 3 different providers so that I'm not relying on any 1 provider.

Quote:

I keep the /etc/resolv.conf file identical on all my systems, same as keeping /etc/hosts identical on all systems.
I am also interested in how you keep this files from being over-written when you use DHCP while out and about?

Habitual 11-01-2016 12:01 PM

L3Comm = 4.2.2.2
is reliable.

szboardstretcher 11-01-2016 12:15 PM

The l3 resolvers are at 4.2.2.1-6

4.2.2.2 is used mostly because it is easy to type, but they probably appreciate when people spread out on the resolvers and aren't just using one of the 6 over and over.

fanoflq 11-01-2016 12:25 PM

@szboardstretcher:

Quote:

The l3 resolvers are at 4.2.2.1-6
What is l3 resolver?
Is it DNS?
"l3" is short name for what?

What is "-6" in "4.2.2.1-6" for?

Thanks.

szboardstretcher 11-01-2016 12:28 PM

The company "Level3 Communications" has DNS name resolvers that are located in the ip address range that starts at 4.2.2.1 and goes to 4.2.2.6.

Writing 4.2.2.1-6 is practical networking shorthand for saying 4.2.2.1 to 4.2.2.6

tronayne 11-01-2016 02:46 PM

Quote:

Originally Posted by lazydog (Post 5625714)
You got me interested.
You are aware that you don't have to use your ISP's DNS servers and can use what ever servers you like? I have my systems setup to use 3 DNS server from 3 different providers so that I'm not relying on any 1 provider.

Of course. However, I have HughesNet Gen4 (EchoStar XVII) satellite service (with light velocity delay built in) and HughessNet DNS servers are faster than any of the other ones in my circumstance. If there's a weather problem (there is every so often) it ain't going to be working in any event, you just wait, there is no alternative where I live.
Keep in mind that it's 22,236 miles up, 22K down, do something, 22K back up and 22K down to me.
Quote:

Originally Posted by lazydog (Post 5625714)
I am also interested in how you keep this files from being over-written when you use DHCP while out and about?

Well, I actually don't. The normal connection with the laptop is an Ethernet cable to the router but, if I have to use wi-fi, I have a /etc/resolv.con.bak that, if DHCP rears its ugly head, is a simple copy to /etc/resolv.conf. Also, I do not run DHCP on anything, everything (including the laptop is fixe-IP; that would include pita, the laptop). That simple copy does not get overwritten during use, so, all is well that ends (and I usually do not edit /etc/resolv.conf, I just leave the HghesNet DNS servers as the active ones if they're working.

Thanks for the interest.

Hope this helps some.

jefro 11-01-2016 03:32 PM

If I had sat connection, I'd make the 50 or so common sites in hosts file and be sure edit that into a good hosts blocking file. You don't need all those ad's tearing up your bandwidth.
http://winhelp2002.mvps.org/hosts.htm

You can speed up lookups as most systems go to hosts file first to resolve an IP. Local lookup is much faster than web searches. You can speed up good sites and block bad sites or trivial ad sites.


All times are GMT -5. The time now is 01:10 PM.