Understand hostname and /etc/hosts












6















In /etc/hostname, I have myname.



In /etc/hosts, I have :



127.0.0.1   localhost.localdomain   localhost myname
::1 localhost.localdomain localhost myname


I have a vague understanding of what really is a hostname and what it is used for.



These more precise questions could help me to understand it better:




  1. What really is my hostname in the example above? myname or something else? If I had instead myname.domain.com in /etc/hostname, what would be my real hostname then?


  2. In the example, the command hostname returns myname, whereas the command hostname -f returns localhost.localdomain. I had in mind that /etc/hosts was only a mapping between names and ip addresses. It seems to serve another function here. What is a fqdn and what is it used for? How is it retrieved? Why is it localhost.localdomain?


  3. If I had instead myname.domain.com in /etc/hostname, hostname -f would return myname.domain.com. Why?


  4. Why aren't fqdn ending with a dot in these files?


  5. Are hostname -d and dnsmydomain equivalent?


  6. In what context can I use my hostname (myname) and when do I have to use my fqdn (localhost.localdomain)?











share|improve this question



























    6















    In /etc/hostname, I have myname.



    In /etc/hosts, I have :



    127.0.0.1   localhost.localdomain   localhost myname
    ::1 localhost.localdomain localhost myname


    I have a vague understanding of what really is a hostname and what it is used for.



    These more precise questions could help me to understand it better:




    1. What really is my hostname in the example above? myname or something else? If I had instead myname.domain.com in /etc/hostname, what would be my real hostname then?


    2. In the example, the command hostname returns myname, whereas the command hostname -f returns localhost.localdomain. I had in mind that /etc/hosts was only a mapping between names and ip addresses. It seems to serve another function here. What is a fqdn and what is it used for? How is it retrieved? Why is it localhost.localdomain?


    3. If I had instead myname.domain.com in /etc/hostname, hostname -f would return myname.domain.com. Why?


    4. Why aren't fqdn ending with a dot in these files?


    5. Are hostname -d and dnsmydomain equivalent?


    6. In what context can I use my hostname (myname) and when do I have to use my fqdn (localhost.localdomain)?











    share|improve this question

























      6












      6








      6








      In /etc/hostname, I have myname.



      In /etc/hosts, I have :



      127.0.0.1   localhost.localdomain   localhost myname
      ::1 localhost.localdomain localhost myname


      I have a vague understanding of what really is a hostname and what it is used for.



      These more precise questions could help me to understand it better:




      1. What really is my hostname in the example above? myname or something else? If I had instead myname.domain.com in /etc/hostname, what would be my real hostname then?


      2. In the example, the command hostname returns myname, whereas the command hostname -f returns localhost.localdomain. I had in mind that /etc/hosts was only a mapping between names and ip addresses. It seems to serve another function here. What is a fqdn and what is it used for? How is it retrieved? Why is it localhost.localdomain?


      3. If I had instead myname.domain.com in /etc/hostname, hostname -f would return myname.domain.com. Why?


      4. Why aren't fqdn ending with a dot in these files?


      5. Are hostname -d and dnsmydomain equivalent?


      6. In what context can I use my hostname (myname) and when do I have to use my fqdn (localhost.localdomain)?











      share|improve this question














      In /etc/hostname, I have myname.



      In /etc/hosts, I have :



      127.0.0.1   localhost.localdomain   localhost myname
      ::1 localhost.localdomain localhost myname


      I have a vague understanding of what really is a hostname and what it is used for.



      These more precise questions could help me to understand it better:




      1. What really is my hostname in the example above? myname or something else? If I had instead myname.domain.com in /etc/hostname, what would be my real hostname then?


      2. In the example, the command hostname returns myname, whereas the command hostname -f returns localhost.localdomain. I had in mind that /etc/hosts was only a mapping between names and ip addresses. It seems to serve another function here. What is a fqdn and what is it used for? How is it retrieved? Why is it localhost.localdomain?


      3. If I had instead myname.domain.com in /etc/hostname, hostname -f would return myname.domain.com. Why?


      4. Why aren't fqdn ending with a dot in these files?


      5. Are hostname -d and dnsmydomain equivalent?


      6. In what context can I use my hostname (myname) and when do I have to use my fqdn (localhost.localdomain)?








      hostname hosts






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 25 '15 at 19:53









      GradientGradient

      1,00151930




      1,00151930






















          4 Answers
          4






          active

          oldest

          votes


















          6
















          1. /etc/hostname contains name of the machine, as known to applications that run locally. /etc/hosts and DNS associate names with IP addresses. myname may be mapped to whichever IP address the machine can access itself, but mapping it to 127.0.0.1 is unæsthetic.

          2. Not /etc/hosts, but /bin/hostname serves another function with -f.

          3. Because /etc/hosts can override the common sense. Edit it with caution and don’t leave garbage there after temporary patches and experiments.

          4. Both styles (⋯.TLD. and ⋯.TLD) are acceptable. The former is unambiguous, whereas the latter is ubiquitous.

          5. Don’t know what dnsmydomain is, but unlikely. Seeking something in /etc/hosts isn’t technically “DNS”.

          6. Remember the point 1. Hostname – for applications that run locally; also for such peculiar network protocols as SMB (samba). FQDN (if not a fakery or placeholder like localhost.localdomain) – for accessing the machine from outside. FQDN must be resolvable where it is used.






          share|improve this answer





















          • 1





            What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?

            – user156516
            Apr 13 '18 at 18:09





















          3














          I absolutely hate when someone tells me to read the manual but in this case please do take a look at the man page for hostname and most if not all your questions should be answered. It is written much better than I could possibly explain.



          To provide some assistance, some of the excerpts that hit on your questions are:
          - Your hostname is what you see in /etc/hosts.
          - The FQDN returned is based on what resolver returns, hence why you see the value in /etc/hosts being returned as resolver checks the hosts file first, it then does any necessary DNS queries.



          The one that I do not believe is touched on in the hostname man page (based on my re-reading of the man page myself) is why the entries in the files do not end with a dot.



          That convention is used when dealing with DNS records. The hostname and hosts files are not actually DNS records. If you were configuring these entries in bind to be resolved for DNS lookups then the use of a dot can come into play (check out this good resource: http://www.zytrax.com/books/dns/apa/dot.html).



          Also, keep in mind that all these values are not broadcast from the system. So if you want another system to access based on the FQDN, it is based on the hosts file on that other system, or lacking hosts entries based on the DNS lookups that the external system does on the FQDN to allow it to resolve back to the system.






          share|improve this answer


























          • Related, about the final dot in /etc/hosts: serverfault.com/questions/336056/setting-fqdn-hostname-and-dot

            – John WH Smith
            Feb 25 '15 at 20:27



















          3














          In answer to




          What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?




          Let me point out, first, that /etc/hosts is a static definition of name to address mapping for the host on which it is installed, and is ONLY used by that host to convert name to IP address.



          So, there are 2 points of view to consider - the host 'myname' and everybody else.



          For that 'myhost' host, either reference the current host using 'localhost' (which is defined in /etc/hosts as 127.0.0.1), or add 'myname' to the /etc/hosts line which defines localhost so that 'myname' always maps to, well, localhost. Like so:




          127.0.0.1 localhost myname




          Then it doesn't matter what IP address the INTERFACE gets given, you'll always have an address that works (an added bonus is that you don't need to hit the hardware to talk from 'myname' to 'myname').



          Now, to allow OTHER hosts to find 'myhost' when 'myhost' has a dynamic IP address, you must use DNS, and said DNS server must handle DHCP-assigned mappings. (Well, ok, there ARE other solutions I can think of, but everything that comes to mind fits in the 'beyond silly' category, IMHO. Well, ok, there are things you can do to the host to have IT tell the DNS where it is, but that's a question worthy of a completely different question/answer stream here, IMHO)






          share|improve this answer































            0














            /etc/hostname and /etc/hosts are simple, but also not so simple...



            How to quickly setup Debian /etc/hostname and /etc/hosts



            1) Edit /etc/hostname.



            There are 2 valid ways to set this up.



            Either make it:





            • machine, i.e. a simple, one word name for this processor.

              For example, 'joe' might be your self-chosen machine name.

              Note this will require an alias in /etc/hosts, further described below.


            OR





            • machine.domain.net, i.e. a Fully Qualified Domain Name (FQDN).

              For example, 'joe.xyz.com'.

              (This is assuming you have a DNS Host 'A' record pointing from joe.xyz.com to your joe's IP address, i.e. at your machine named joe. Normally you set this record up where you purchased your domain name. More on that in a moment.)




            Once /etc/hostname is set, then the next step is to get the commands hostname --all-fqdn and dnsdomainname to work properly, in that order, as follows:



            2) Edit /etc/hosts.



            In it have a line for this host's FQDN, e.g. 'machine.domain.net', prefixed with its this machine's IP address (possibly from a dynamic IP address server) like this:



            #   IP           FQDN (CANONICAL_HOSTNAME)   ALIASES
            #-------------- --------------------------- ------------------------
            192.168.1.14 Joe.xyz.com joe



            • First, note that upper or lower case don't matter here. Tip: Having some caps on, however, allows us to notice where things come from in the next steps. So I have capitalized Joe.


            • Also note that for the simple hostname (i.e. /etc/hostname = 'machine') to correctly generate a FQDN, an alias named 'machine' must be present in /etc/hosts. That's why the alias joe is there. (BTW, it's ok to have that alias even if you're not using it.) Otherwise this alias isn't needed, nor used.


            • Also note that 'domain.net' must be resolvable, i.e. there must exist an A record for it in DNS. This is what is called 'resolvable' (i.e. by the DNS resolver).



            3) Now install these new setting from /etc/hostname and /etc/hosts into the kernel, by running:



                $ /etc/init.d/hostname.sh       # Or by rebooting




            4) Test hostname



            $ hostname      # Reports the hostname previously read from in /etc/hostname.
            joe


            OR



            $ hostname
            joe.xyz.com


            Tips:




            • Notice that this is not what /etc/hostname currently is, like if you edit it again right now, but rather what was read previously by the /etc/init.d/hostname.sh script, possibly as a result of a reboot.


            • Also notice that the 'j' is lower case, so this suggests it is coming from /etc/hostname, not /etc/hosts.



            5) Test FQDN



            $ hostname --all-fqdn   #reports the FQDN(s) matched in /etc/hosts.
            Joe.xyz.com # It searches /etc/hosts for the joe in /etc/hostname.
            # Notice that the 'J' is uppercase.
            # This suggest it came from /etc/hosts.


            6) Test DNS domain name



            $ dnsdomainname         #reports the computed DNS doman name    
            xyz.com # (i.e. the right most part of the FQDN;
            # the part to the right of the first, left most, dot)
            # This is coming from Joe.xyz.com in /hosts,
            # after 'Joe.' is removed.




            Now this should make setting up a web and email server easier.





            share























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "106"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f186859%2funderstand-hostname-and-etc-hosts%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              6
















              1. /etc/hostname contains name of the machine, as known to applications that run locally. /etc/hosts and DNS associate names with IP addresses. myname may be mapped to whichever IP address the machine can access itself, but mapping it to 127.0.0.1 is unæsthetic.

              2. Not /etc/hosts, but /bin/hostname serves another function with -f.

              3. Because /etc/hosts can override the common sense. Edit it with caution and don’t leave garbage there after temporary patches and experiments.

              4. Both styles (⋯.TLD. and ⋯.TLD) are acceptable. The former is unambiguous, whereas the latter is ubiquitous.

              5. Don’t know what dnsmydomain is, but unlikely. Seeking something in /etc/hosts isn’t technically “DNS”.

              6. Remember the point 1. Hostname – for applications that run locally; also for such peculiar network protocols as SMB (samba). FQDN (if not a fakery or placeholder like localhost.localdomain) – for accessing the machine from outside. FQDN must be resolvable where it is used.






              share|improve this answer





















              • 1





                What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?

                – user156516
                Apr 13 '18 at 18:09


















              6
















              1. /etc/hostname contains name of the machine, as known to applications that run locally. /etc/hosts and DNS associate names with IP addresses. myname may be mapped to whichever IP address the machine can access itself, but mapping it to 127.0.0.1 is unæsthetic.

              2. Not /etc/hosts, but /bin/hostname serves another function with -f.

              3. Because /etc/hosts can override the common sense. Edit it with caution and don’t leave garbage there after temporary patches and experiments.

              4. Both styles (⋯.TLD. and ⋯.TLD) are acceptable. The former is unambiguous, whereas the latter is ubiquitous.

              5. Don’t know what dnsmydomain is, but unlikely. Seeking something in /etc/hosts isn’t technically “DNS”.

              6. Remember the point 1. Hostname – for applications that run locally; also for such peculiar network protocols as SMB (samba). FQDN (if not a fakery or placeholder like localhost.localdomain) – for accessing the machine from outside. FQDN must be resolvable where it is used.






              share|improve this answer





















              • 1





                What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?

                – user156516
                Apr 13 '18 at 18:09
















              6












              6








              6









              1. /etc/hostname contains name of the machine, as known to applications that run locally. /etc/hosts and DNS associate names with IP addresses. myname may be mapped to whichever IP address the machine can access itself, but mapping it to 127.0.0.1 is unæsthetic.

              2. Not /etc/hosts, but /bin/hostname serves another function with -f.

              3. Because /etc/hosts can override the common sense. Edit it with caution and don’t leave garbage there after temporary patches and experiments.

              4. Both styles (⋯.TLD. and ⋯.TLD) are acceptable. The former is unambiguous, whereas the latter is ubiquitous.

              5. Don’t know what dnsmydomain is, but unlikely. Seeking something in /etc/hosts isn’t technically “DNS”.

              6. Remember the point 1. Hostname – for applications that run locally; also for such peculiar network protocols as SMB (samba). FQDN (if not a fakery or placeholder like localhost.localdomain) – for accessing the machine from outside. FQDN must be resolvable where it is used.






              share|improve this answer

















              1. /etc/hostname contains name of the machine, as known to applications that run locally. /etc/hosts and DNS associate names with IP addresses. myname may be mapped to whichever IP address the machine can access itself, but mapping it to 127.0.0.1 is unæsthetic.

              2. Not /etc/hosts, but /bin/hostname serves another function with -f.

              3. Because /etc/hosts can override the common sense. Edit it with caution and don’t leave garbage there after temporary patches and experiments.

              4. Both styles (⋯.TLD. and ⋯.TLD) are acceptable. The former is unambiguous, whereas the latter is ubiquitous.

              5. Don’t know what dnsmydomain is, but unlikely. Seeking something in /etc/hosts isn’t technically “DNS”.

              6. Remember the point 1. Hostname – for applications that run locally; also for such peculiar network protocols as SMB (samba). FQDN (if not a fakery or placeholder like localhost.localdomain) – for accessing the machine from outside. FQDN must be resolvable where it is used.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 13 '17 at 12:22









              Community

              1




              1










              answered Sep 12 '15 at 21:45









              Incnis MrsiIncnis Mrsi

              1,356923




              1,356923








              • 1





                What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?

                – user156516
                Apr 13 '18 at 18:09
















              • 1





                What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?

                – user156516
                Apr 13 '18 at 18:09










              1




              1





              What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?

              – user156516
              Apr 13 '18 at 18:09







              What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?

              – user156516
              Apr 13 '18 at 18:09















              3














              I absolutely hate when someone tells me to read the manual but in this case please do take a look at the man page for hostname and most if not all your questions should be answered. It is written much better than I could possibly explain.



              To provide some assistance, some of the excerpts that hit on your questions are:
              - Your hostname is what you see in /etc/hosts.
              - The FQDN returned is based on what resolver returns, hence why you see the value in /etc/hosts being returned as resolver checks the hosts file first, it then does any necessary DNS queries.



              The one that I do not believe is touched on in the hostname man page (based on my re-reading of the man page myself) is why the entries in the files do not end with a dot.



              That convention is used when dealing with DNS records. The hostname and hosts files are not actually DNS records. If you were configuring these entries in bind to be resolved for DNS lookups then the use of a dot can come into play (check out this good resource: http://www.zytrax.com/books/dns/apa/dot.html).



              Also, keep in mind that all these values are not broadcast from the system. So if you want another system to access based on the FQDN, it is based on the hosts file on that other system, or lacking hosts entries based on the DNS lookups that the external system does on the FQDN to allow it to resolve back to the system.






              share|improve this answer


























              • Related, about the final dot in /etc/hosts: serverfault.com/questions/336056/setting-fqdn-hostname-and-dot

                – John WH Smith
                Feb 25 '15 at 20:27
















              3














              I absolutely hate when someone tells me to read the manual but in this case please do take a look at the man page for hostname and most if not all your questions should be answered. It is written much better than I could possibly explain.



              To provide some assistance, some of the excerpts that hit on your questions are:
              - Your hostname is what you see in /etc/hosts.
              - The FQDN returned is based on what resolver returns, hence why you see the value in /etc/hosts being returned as resolver checks the hosts file first, it then does any necessary DNS queries.



              The one that I do not believe is touched on in the hostname man page (based on my re-reading of the man page myself) is why the entries in the files do not end with a dot.



              That convention is used when dealing with DNS records. The hostname and hosts files are not actually DNS records. If you were configuring these entries in bind to be resolved for DNS lookups then the use of a dot can come into play (check out this good resource: http://www.zytrax.com/books/dns/apa/dot.html).



              Also, keep in mind that all these values are not broadcast from the system. So if you want another system to access based on the FQDN, it is based on the hosts file on that other system, or lacking hosts entries based on the DNS lookups that the external system does on the FQDN to allow it to resolve back to the system.






              share|improve this answer


























              • Related, about the final dot in /etc/hosts: serverfault.com/questions/336056/setting-fqdn-hostname-and-dot

                – John WH Smith
                Feb 25 '15 at 20:27














              3












              3








              3







              I absolutely hate when someone tells me to read the manual but in this case please do take a look at the man page for hostname and most if not all your questions should be answered. It is written much better than I could possibly explain.



              To provide some assistance, some of the excerpts that hit on your questions are:
              - Your hostname is what you see in /etc/hosts.
              - The FQDN returned is based on what resolver returns, hence why you see the value in /etc/hosts being returned as resolver checks the hosts file first, it then does any necessary DNS queries.



              The one that I do not believe is touched on in the hostname man page (based on my re-reading of the man page myself) is why the entries in the files do not end with a dot.



              That convention is used when dealing with DNS records. The hostname and hosts files are not actually DNS records. If you were configuring these entries in bind to be resolved for DNS lookups then the use of a dot can come into play (check out this good resource: http://www.zytrax.com/books/dns/apa/dot.html).



              Also, keep in mind that all these values are not broadcast from the system. So if you want another system to access based on the FQDN, it is based on the hosts file on that other system, or lacking hosts entries based on the DNS lookups that the external system does on the FQDN to allow it to resolve back to the system.






              share|improve this answer















              I absolutely hate when someone tells me to read the manual but in this case please do take a look at the man page for hostname and most if not all your questions should be answered. It is written much better than I could possibly explain.



              To provide some assistance, some of the excerpts that hit on your questions are:
              - Your hostname is what you see in /etc/hosts.
              - The FQDN returned is based on what resolver returns, hence why you see the value in /etc/hosts being returned as resolver checks the hosts file first, it then does any necessary DNS queries.



              The one that I do not believe is touched on in the hostname man page (based on my re-reading of the man page myself) is why the entries in the files do not end with a dot.



              That convention is used when dealing with DNS records. The hostname and hosts files are not actually DNS records. If you were configuring these entries in bind to be resolved for DNS lookups then the use of a dot can come into play (check out this good resource: http://www.zytrax.com/books/dns/apa/dot.html).



              Also, keep in mind that all these values are not broadcast from the system. So if you want another system to access based on the FQDN, it is based on the hosts file on that other system, or lacking hosts entries based on the DNS lookups that the external system does on the FQDN to allow it to resolve back to the system.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 25 '15 at 21:47

























              answered Feb 25 '15 at 20:25









              User52458User52458

              563




              563













              • Related, about the final dot in /etc/hosts: serverfault.com/questions/336056/setting-fqdn-hostname-and-dot

                – John WH Smith
                Feb 25 '15 at 20:27



















              • Related, about the final dot in /etc/hosts: serverfault.com/questions/336056/setting-fqdn-hostname-and-dot

                – John WH Smith
                Feb 25 '15 at 20:27

















              Related, about the final dot in /etc/hosts: serverfault.com/questions/336056/setting-fqdn-hostname-and-dot

              – John WH Smith
              Feb 25 '15 at 20:27





              Related, about the final dot in /etc/hosts: serverfault.com/questions/336056/setting-fqdn-hostname-and-dot

              – John WH Smith
              Feb 25 '15 at 20:27











              3














              In answer to




              What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?




              Let me point out, first, that /etc/hosts is a static definition of name to address mapping for the host on which it is installed, and is ONLY used by that host to convert name to IP address.



              So, there are 2 points of view to consider - the host 'myname' and everybody else.



              For that 'myhost' host, either reference the current host using 'localhost' (which is defined in /etc/hosts as 127.0.0.1), or add 'myname' to the /etc/hosts line which defines localhost so that 'myname' always maps to, well, localhost. Like so:




              127.0.0.1 localhost myname




              Then it doesn't matter what IP address the INTERFACE gets given, you'll always have an address that works (an added bonus is that you don't need to hit the hardware to talk from 'myname' to 'myname').



              Now, to allow OTHER hosts to find 'myhost' when 'myhost' has a dynamic IP address, you must use DNS, and said DNS server must handle DHCP-assigned mappings. (Well, ok, there ARE other solutions I can think of, but everything that comes to mind fits in the 'beyond silly' category, IMHO. Well, ok, there are things you can do to the host to have IT tell the DNS where it is, but that's a question worthy of a completely different question/answer stream here, IMHO)






              share|improve this answer




























                3














                In answer to




                What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?




                Let me point out, first, that /etc/hosts is a static definition of name to address mapping for the host on which it is installed, and is ONLY used by that host to convert name to IP address.



                So, there are 2 points of view to consider - the host 'myname' and everybody else.



                For that 'myhost' host, either reference the current host using 'localhost' (which is defined in /etc/hosts as 127.0.0.1), or add 'myname' to the /etc/hosts line which defines localhost so that 'myname' always maps to, well, localhost. Like so:




                127.0.0.1 localhost myname




                Then it doesn't matter what IP address the INTERFACE gets given, you'll always have an address that works (an added bonus is that you don't need to hit the hardware to talk from 'myname' to 'myname').



                Now, to allow OTHER hosts to find 'myhost' when 'myhost' has a dynamic IP address, you must use DNS, and said DNS server must handle DHCP-assigned mappings. (Well, ok, there ARE other solutions I can think of, but everything that comes to mind fits in the 'beyond silly' category, IMHO. Well, ok, there are things you can do to the host to have IT tell the DNS where it is, but that's a question worthy of a completely different question/answer stream here, IMHO)






                share|improve this answer


























                  3












                  3








                  3







                  In answer to




                  What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?




                  Let me point out, first, that /etc/hosts is a static definition of name to address mapping for the host on which it is installed, and is ONLY used by that host to convert name to IP address.



                  So, there are 2 points of view to consider - the host 'myname' and everybody else.



                  For that 'myhost' host, either reference the current host using 'localhost' (which is defined in /etc/hosts as 127.0.0.1), or add 'myname' to the /etc/hosts line which defines localhost so that 'myname' always maps to, well, localhost. Like so:




                  127.0.0.1 localhost myname




                  Then it doesn't matter what IP address the INTERFACE gets given, you'll always have an address that works (an added bonus is that you don't need to hit the hardware to talk from 'myname' to 'myname').



                  Now, to allow OTHER hosts to find 'myhost' when 'myhost' has a dynamic IP address, you must use DNS, and said DNS server must handle DHCP-assigned mappings. (Well, ok, there ARE other solutions I can think of, but everything that comes to mind fits in the 'beyond silly' category, IMHO. Well, ok, there are things you can do to the host to have IT tell the DNS where it is, but that's a question worthy of a completely different question/answer stream here, IMHO)






                  share|improve this answer













                  In answer to




                  What if my IP is dynamic (DHCP) and changes from time to time? which address should I use as myname in /etc/hosts?




                  Let me point out, first, that /etc/hosts is a static definition of name to address mapping for the host on which it is installed, and is ONLY used by that host to convert name to IP address.



                  So, there are 2 points of view to consider - the host 'myname' and everybody else.



                  For that 'myhost' host, either reference the current host using 'localhost' (which is defined in /etc/hosts as 127.0.0.1), or add 'myname' to the /etc/hosts line which defines localhost so that 'myname' always maps to, well, localhost. Like so:




                  127.0.0.1 localhost myname




                  Then it doesn't matter what IP address the INTERFACE gets given, you'll always have an address that works (an added bonus is that you don't need to hit the hardware to talk from 'myname' to 'myname').



                  Now, to allow OTHER hosts to find 'myhost' when 'myhost' has a dynamic IP address, you must use DNS, and said DNS server must handle DHCP-assigned mappings. (Well, ok, there ARE other solutions I can think of, but everything that comes to mind fits in the 'beyond silly' category, IMHO. Well, ok, there are things you can do to the host to have IT tell the DNS where it is, but that's a question worthy of a completely different question/answer stream here, IMHO)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 29 '18 at 21:49









                  RustyCarRustyCar

                  343




                  343























                      0














                      /etc/hostname and /etc/hosts are simple, but also not so simple...



                      How to quickly setup Debian /etc/hostname and /etc/hosts



                      1) Edit /etc/hostname.



                      There are 2 valid ways to set this up.



                      Either make it:





                      • machine, i.e. a simple, one word name for this processor.

                        For example, 'joe' might be your self-chosen machine name.

                        Note this will require an alias in /etc/hosts, further described below.


                      OR





                      • machine.domain.net, i.e. a Fully Qualified Domain Name (FQDN).

                        For example, 'joe.xyz.com'.

                        (This is assuming you have a DNS Host 'A' record pointing from joe.xyz.com to your joe's IP address, i.e. at your machine named joe. Normally you set this record up where you purchased your domain name. More on that in a moment.)




                      Once /etc/hostname is set, then the next step is to get the commands hostname --all-fqdn and dnsdomainname to work properly, in that order, as follows:



                      2) Edit /etc/hosts.



                      In it have a line for this host's FQDN, e.g. 'machine.domain.net', prefixed with its this machine's IP address (possibly from a dynamic IP address server) like this:



                      #   IP           FQDN (CANONICAL_HOSTNAME)   ALIASES
                      #-------------- --------------------------- ------------------------
                      192.168.1.14 Joe.xyz.com joe



                      • First, note that upper or lower case don't matter here. Tip: Having some caps on, however, allows us to notice where things come from in the next steps. So I have capitalized Joe.


                      • Also note that for the simple hostname (i.e. /etc/hostname = 'machine') to correctly generate a FQDN, an alias named 'machine' must be present in /etc/hosts. That's why the alias joe is there. (BTW, it's ok to have that alias even if you're not using it.) Otherwise this alias isn't needed, nor used.


                      • Also note that 'domain.net' must be resolvable, i.e. there must exist an A record for it in DNS. This is what is called 'resolvable' (i.e. by the DNS resolver).



                      3) Now install these new setting from /etc/hostname and /etc/hosts into the kernel, by running:



                          $ /etc/init.d/hostname.sh       # Or by rebooting




                      4) Test hostname



                      $ hostname      # Reports the hostname previously read from in /etc/hostname.
                      joe


                      OR



                      $ hostname
                      joe.xyz.com


                      Tips:




                      • Notice that this is not what /etc/hostname currently is, like if you edit it again right now, but rather what was read previously by the /etc/init.d/hostname.sh script, possibly as a result of a reboot.


                      • Also notice that the 'j' is lower case, so this suggests it is coming from /etc/hostname, not /etc/hosts.



                      5) Test FQDN



                      $ hostname --all-fqdn   #reports the FQDN(s) matched in /etc/hosts.
                      Joe.xyz.com # It searches /etc/hosts for the joe in /etc/hostname.
                      # Notice that the 'J' is uppercase.
                      # This suggest it came from /etc/hosts.


                      6) Test DNS domain name



                      $ dnsdomainname         #reports the computed DNS doman name    
                      xyz.com # (i.e. the right most part of the FQDN;
                      # the part to the right of the first, left most, dot)
                      # This is coming from Joe.xyz.com in /hosts,
                      # after 'Joe.' is removed.




                      Now this should make setting up a web and email server easier.





                      share




























                        0














                        /etc/hostname and /etc/hosts are simple, but also not so simple...



                        How to quickly setup Debian /etc/hostname and /etc/hosts



                        1) Edit /etc/hostname.



                        There are 2 valid ways to set this up.



                        Either make it:





                        • machine, i.e. a simple, one word name for this processor.

                          For example, 'joe' might be your self-chosen machine name.

                          Note this will require an alias in /etc/hosts, further described below.


                        OR





                        • machine.domain.net, i.e. a Fully Qualified Domain Name (FQDN).

                          For example, 'joe.xyz.com'.

                          (This is assuming you have a DNS Host 'A' record pointing from joe.xyz.com to your joe's IP address, i.e. at your machine named joe. Normally you set this record up where you purchased your domain name. More on that in a moment.)




                        Once /etc/hostname is set, then the next step is to get the commands hostname --all-fqdn and dnsdomainname to work properly, in that order, as follows:



                        2) Edit /etc/hosts.



                        In it have a line for this host's FQDN, e.g. 'machine.domain.net', prefixed with its this machine's IP address (possibly from a dynamic IP address server) like this:



                        #   IP           FQDN (CANONICAL_HOSTNAME)   ALIASES
                        #-------------- --------------------------- ------------------------
                        192.168.1.14 Joe.xyz.com joe



                        • First, note that upper or lower case don't matter here. Tip: Having some caps on, however, allows us to notice where things come from in the next steps. So I have capitalized Joe.


                        • Also note that for the simple hostname (i.e. /etc/hostname = 'machine') to correctly generate a FQDN, an alias named 'machine' must be present in /etc/hosts. That's why the alias joe is there. (BTW, it's ok to have that alias even if you're not using it.) Otherwise this alias isn't needed, nor used.


                        • Also note that 'domain.net' must be resolvable, i.e. there must exist an A record for it in DNS. This is what is called 'resolvable' (i.e. by the DNS resolver).



                        3) Now install these new setting from /etc/hostname and /etc/hosts into the kernel, by running:



                            $ /etc/init.d/hostname.sh       # Or by rebooting




                        4) Test hostname



                        $ hostname      # Reports the hostname previously read from in /etc/hostname.
                        joe


                        OR



                        $ hostname
                        joe.xyz.com


                        Tips:




                        • Notice that this is not what /etc/hostname currently is, like if you edit it again right now, but rather what was read previously by the /etc/init.d/hostname.sh script, possibly as a result of a reboot.


                        • Also notice that the 'j' is lower case, so this suggests it is coming from /etc/hostname, not /etc/hosts.



                        5) Test FQDN



                        $ hostname --all-fqdn   #reports the FQDN(s) matched in /etc/hosts.
                        Joe.xyz.com # It searches /etc/hosts for the joe in /etc/hostname.
                        # Notice that the 'J' is uppercase.
                        # This suggest it came from /etc/hosts.


                        6) Test DNS domain name



                        $ dnsdomainname         #reports the computed DNS doman name    
                        xyz.com # (i.e. the right most part of the FQDN;
                        # the part to the right of the first, left most, dot)
                        # This is coming from Joe.xyz.com in /hosts,
                        # after 'Joe.' is removed.




                        Now this should make setting up a web and email server easier.





                        share


























                          0












                          0








                          0







                          /etc/hostname and /etc/hosts are simple, but also not so simple...



                          How to quickly setup Debian /etc/hostname and /etc/hosts



                          1) Edit /etc/hostname.



                          There are 2 valid ways to set this up.



                          Either make it:





                          • machine, i.e. a simple, one word name for this processor.

                            For example, 'joe' might be your self-chosen machine name.

                            Note this will require an alias in /etc/hosts, further described below.


                          OR





                          • machine.domain.net, i.e. a Fully Qualified Domain Name (FQDN).

                            For example, 'joe.xyz.com'.

                            (This is assuming you have a DNS Host 'A' record pointing from joe.xyz.com to your joe's IP address, i.e. at your machine named joe. Normally you set this record up where you purchased your domain name. More on that in a moment.)




                          Once /etc/hostname is set, then the next step is to get the commands hostname --all-fqdn and dnsdomainname to work properly, in that order, as follows:



                          2) Edit /etc/hosts.



                          In it have a line for this host's FQDN, e.g. 'machine.domain.net', prefixed with its this machine's IP address (possibly from a dynamic IP address server) like this:



                          #   IP           FQDN (CANONICAL_HOSTNAME)   ALIASES
                          #-------------- --------------------------- ------------------------
                          192.168.1.14 Joe.xyz.com joe



                          • First, note that upper or lower case don't matter here. Tip: Having some caps on, however, allows us to notice where things come from in the next steps. So I have capitalized Joe.


                          • Also note that for the simple hostname (i.e. /etc/hostname = 'machine') to correctly generate a FQDN, an alias named 'machine' must be present in /etc/hosts. That's why the alias joe is there. (BTW, it's ok to have that alias even if you're not using it.) Otherwise this alias isn't needed, nor used.


                          • Also note that 'domain.net' must be resolvable, i.e. there must exist an A record for it in DNS. This is what is called 'resolvable' (i.e. by the DNS resolver).



                          3) Now install these new setting from /etc/hostname and /etc/hosts into the kernel, by running:



                              $ /etc/init.d/hostname.sh       # Or by rebooting




                          4) Test hostname



                          $ hostname      # Reports the hostname previously read from in /etc/hostname.
                          joe


                          OR



                          $ hostname
                          joe.xyz.com


                          Tips:




                          • Notice that this is not what /etc/hostname currently is, like if you edit it again right now, but rather what was read previously by the /etc/init.d/hostname.sh script, possibly as a result of a reboot.


                          • Also notice that the 'j' is lower case, so this suggests it is coming from /etc/hostname, not /etc/hosts.



                          5) Test FQDN



                          $ hostname --all-fqdn   #reports the FQDN(s) matched in /etc/hosts.
                          Joe.xyz.com # It searches /etc/hosts for the joe in /etc/hostname.
                          # Notice that the 'J' is uppercase.
                          # This suggest it came from /etc/hosts.


                          6) Test DNS domain name



                          $ dnsdomainname         #reports the computed DNS doman name    
                          xyz.com # (i.e. the right most part of the FQDN;
                          # the part to the right of the first, left most, dot)
                          # This is coming from Joe.xyz.com in /hosts,
                          # after 'Joe.' is removed.




                          Now this should make setting up a web and email server easier.





                          share













                          /etc/hostname and /etc/hosts are simple, but also not so simple...



                          How to quickly setup Debian /etc/hostname and /etc/hosts



                          1) Edit /etc/hostname.



                          There are 2 valid ways to set this up.



                          Either make it:





                          • machine, i.e. a simple, one word name for this processor.

                            For example, 'joe' might be your self-chosen machine name.

                            Note this will require an alias in /etc/hosts, further described below.


                          OR





                          • machine.domain.net, i.e. a Fully Qualified Domain Name (FQDN).

                            For example, 'joe.xyz.com'.

                            (This is assuming you have a DNS Host 'A' record pointing from joe.xyz.com to your joe's IP address, i.e. at your machine named joe. Normally you set this record up where you purchased your domain name. More on that in a moment.)




                          Once /etc/hostname is set, then the next step is to get the commands hostname --all-fqdn and dnsdomainname to work properly, in that order, as follows:



                          2) Edit /etc/hosts.



                          In it have a line for this host's FQDN, e.g. 'machine.domain.net', prefixed with its this machine's IP address (possibly from a dynamic IP address server) like this:



                          #   IP           FQDN (CANONICAL_HOSTNAME)   ALIASES
                          #-------------- --------------------------- ------------------------
                          192.168.1.14 Joe.xyz.com joe



                          • First, note that upper or lower case don't matter here. Tip: Having some caps on, however, allows us to notice where things come from in the next steps. So I have capitalized Joe.


                          • Also note that for the simple hostname (i.e. /etc/hostname = 'machine') to correctly generate a FQDN, an alias named 'machine' must be present in /etc/hosts. That's why the alias joe is there. (BTW, it's ok to have that alias even if you're not using it.) Otherwise this alias isn't needed, nor used.


                          • Also note that 'domain.net' must be resolvable, i.e. there must exist an A record for it in DNS. This is what is called 'resolvable' (i.e. by the DNS resolver).



                          3) Now install these new setting from /etc/hostname and /etc/hosts into the kernel, by running:



                              $ /etc/init.d/hostname.sh       # Or by rebooting




                          4) Test hostname



                          $ hostname      # Reports the hostname previously read from in /etc/hostname.
                          joe


                          OR



                          $ hostname
                          joe.xyz.com


                          Tips:




                          • Notice that this is not what /etc/hostname currently is, like if you edit it again right now, but rather what was read previously by the /etc/init.d/hostname.sh script, possibly as a result of a reboot.


                          • Also notice that the 'j' is lower case, so this suggests it is coming from /etc/hostname, not /etc/hosts.



                          5) Test FQDN



                          $ hostname --all-fqdn   #reports the FQDN(s) matched in /etc/hosts.
                          Joe.xyz.com # It searches /etc/hosts for the joe in /etc/hostname.
                          # Notice that the 'J' is uppercase.
                          # This suggest it came from /etc/hosts.


                          6) Test DNS domain name



                          $ dnsdomainname         #reports the computed DNS doman name    
                          xyz.com # (i.e. the right most part of the FQDN;
                          # the part to the right of the first, left most, dot)
                          # This is coming from Joe.xyz.com in /hosts,
                          # after 'Joe.' is removed.




                          Now this should make setting up a web and email server easier.






                          share











                          share


                          share










                          answered 8 mins ago









                          Elliptical viewElliptical view

                          5931622




                          5931622






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f186859%2funderstand-hostname-and-etc-hosts%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Loup dans la culture

                              How to solve the problem of ntp “Unable to contact time server” from KDE?

                              ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux