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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
10-30-2016, 12:48 AM
|
#1
|
Member
Registered: Nov 2015
Posts: 397
Rep:
|
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.
|
|
|
10-30-2016, 10:31 AM
|
#2
|
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,908
|
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.
|
|
|
10-31-2016, 06:37 PM
|
#3
|
Moderator
Registered: Mar 2008
Posts: 22,247
|
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.
|
|
|
11-01-2016, 10:00 AM
|
#4
|
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
|
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.
|
11-01-2016, 10:47 AM
|
#5
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep:
|
@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.
|
|
|
11-01-2016, 10:54 AM
|
#6
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep:
|
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.
|
|
|
11-01-2016, 11:21 AM
|
#7
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
Quote:
Originally Posted by fanoflq
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.
|
|
|
11-01-2016, 11:24 AM
|
#8
|
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
|
Quote:
Originally Posted by fanoflq
@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.
|
|
|
11-01-2016, 12:54 PM
|
#9
|
Senior Member
Registered: Dec 2003
Location: The Key Stone State
Distribution: CentOS Sabayon and now Gentoo
Posts: 1,249
Rep:
|
You got me interested.
Quote:
Originally Posted by tronayne
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?
|
|
|
11-01-2016, 01:01 PM
|
#10
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep:
|
L3Comm = 4.2.2.2
is reliable.
|
|
|
11-01-2016, 01:15 PM
|
#11
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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.
|
|
|
11-01-2016, 01:25 PM
|
#12
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep:
|
@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.
|
|
|
11-01-2016, 01:28 PM
|
#13
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
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
|
|
|
11-01-2016, 03:46 PM
|
#14
|
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
|
Quote:
Originally Posted by lazydog
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
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.
|
|
|
11-01-2016, 04:32 PM
|
#15
|
Moderator
Registered: Mar 2008
Posts: 22,247
|
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.
|
All times are GMT -5. The time now is 08:45 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|