LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-30-2016, 12:48 AM   #1
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Rep: Reputation: Disabled
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.

Last edited by fanoflq; 10-30-2016 at 01:14 AM.
 
Old 10-30-2016, 10:31 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,908

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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).

Last edited by jpollard; 10-30-2016 at 10:34 AM.
 
Old 10-31-2016, 06:37 PM   #3
jefro
Moderator
 
Registered: Mar 2008
Posts: 22,247

Rep: Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654
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.
 
Old 11-01-2016, 10:00 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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.
 
1 members found this post helpful.
Old 11-01-2016, 10:47 AM   #5
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
@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.
 
Old 11-01-2016, 10:54 AM   #6
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-01-2016, 11:21 AM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143
Quote:
Originally Posted by fanoflq View Post
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.
 
Old 11-01-2016, 11:24 AM   #8
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Quote:
Originally Posted by fanoflq View Post
@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.
 
Old 11-01-2016, 12:54 PM   #9
lazydog
Senior Member
 
Registered: Dec 2003
Location: The Key Stone State
Distribution: CentOS Sabayon and now Gentoo
Posts: 1,249
Blog Entries: 3

Rep: Reputation: 194Reputation: 194
You got me interested.

Quote:
Originally Posted by tronayne View Post
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?
 
Old 11-01-2016, 01:01 PM   #10
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
L3Comm = 4.2.2.2
is reliable.
 
Old 11-01-2016, 01:15 PM   #11
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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.
 
Old 11-01-2016, 01:25 PM   #12
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
@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.
 
Old 11-01-2016, 01:28 PM   #13
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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
 
Old 11-01-2016, 03:46 PM   #14
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Quote:
Originally Posted by lazydog View Post
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 View Post
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.
 
Old 11-01-2016, 04:32 PM   #15
jefro
Moderator
 
Registered: Mar 2008
Posts: 22,247

Rep: Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654Reputation: 3654
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hostname to Hostname mapping within /etc/hosts possible? helptonewbie Linux - Newbie 2 09-09-2010 07:43 AM
Could someone explain to me the process of adding a hostname? (hostname.domain.com) brynjarh Linux - Server 2 10-18-2009 08:58 PM
Problem with hostname and /etc/hosts file toxikco2 Debian 6 03-07-2007 09:55 PM
postfix with /etc/hosts, /etc/hostname file setting taiwf Linux - Newbie 1 05-24-2006 11:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration