How to correctly set hostname and domain name?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
21
down vote

favorite
9












I am renting a server, running Ubuntu 16.04 at a company, let's name it company.org.



Currently, my server is configured like this:



  • hostname: server737263

  • domain name: company.org

Here's my FQDN:



user@server737263:~ $ hostname --fqdn
server737263.company.org


This is not surprising.



I am also renting a domain name, let's name it domain.org. What I would like to do would be to rename my server as server1.domain.org.



This means configuring my hostname as server1 and my domain name as domain.org.



How can I do it correctly?



Indeed, the manpage for hostname is not clear. To me at least:




HOSTNAME(1)



[...]



SET NAME



  • When called with one argument or with the --file option, the commands set the host name or the NIS/YP domain name. hostname uses
    the sethostname(2) function, while all of the three domainname,
    ypdomainname and nisdomainname use setdomainname(2). Note, that this
    is effective only until the next reboot. Edit /etc/hostname for
    permanent change.


[...]



THE FQDN



  • You cannot change the FQDN with hostname or dnsdomainname.

[...]




So it seems that editing /etc/hostname is not enough? Because if it really changed the hostname, it would have changed the FQDN. There's also a trick I read to change the hostname with the command sysctl kernel.hostname=server1, but nothing says whether this is the correct way or an ugly trick.



So:



  1. What is the correct way to set the hostname?


  2. What is the correct way to set the domain name?










share|improve this question



























    up vote
    21
    down vote

    favorite
    9












    I am renting a server, running Ubuntu 16.04 at a company, let's name it company.org.



    Currently, my server is configured like this:



    • hostname: server737263

    • domain name: company.org

    Here's my FQDN:



    user@server737263:~ $ hostname --fqdn
    server737263.company.org


    This is not surprising.



    I am also renting a domain name, let's name it domain.org. What I would like to do would be to rename my server as server1.domain.org.



    This means configuring my hostname as server1 and my domain name as domain.org.



    How can I do it correctly?



    Indeed, the manpage for hostname is not clear. To me at least:




    HOSTNAME(1)



    [...]



    SET NAME



    • When called with one argument or with the --file option, the commands set the host name or the NIS/YP domain name. hostname uses
      the sethostname(2) function, while all of the three domainname,
      ypdomainname and nisdomainname use setdomainname(2). Note, that this
      is effective only until the next reboot. Edit /etc/hostname for
      permanent change.


    [...]



    THE FQDN



    • You cannot change the FQDN with hostname or dnsdomainname.

    [...]




    So it seems that editing /etc/hostname is not enough? Because if it really changed the hostname, it would have changed the FQDN. There's also a trick I read to change the hostname with the command sysctl kernel.hostname=server1, but nothing says whether this is the correct way or an ugly trick.



    So:



    1. What is the correct way to set the hostname?


    2. What is the correct way to set the domain name?










    share|improve this question

























      up vote
      21
      down vote

      favorite
      9









      up vote
      21
      down vote

      favorite
      9






      9





      I am renting a server, running Ubuntu 16.04 at a company, let's name it company.org.



      Currently, my server is configured like this:



      • hostname: server737263

      • domain name: company.org

      Here's my FQDN:



      user@server737263:~ $ hostname --fqdn
      server737263.company.org


      This is not surprising.



      I am also renting a domain name, let's name it domain.org. What I would like to do would be to rename my server as server1.domain.org.



      This means configuring my hostname as server1 and my domain name as domain.org.



      How can I do it correctly?



      Indeed, the manpage for hostname is not clear. To me at least:




      HOSTNAME(1)



      [...]



      SET NAME



      • When called with one argument or with the --file option, the commands set the host name or the NIS/YP domain name. hostname uses
        the sethostname(2) function, while all of the three domainname,
        ypdomainname and nisdomainname use setdomainname(2). Note, that this
        is effective only until the next reboot. Edit /etc/hostname for
        permanent change.


      [...]



      THE FQDN



      • You cannot change the FQDN with hostname or dnsdomainname.

      [...]




      So it seems that editing /etc/hostname is not enough? Because if it really changed the hostname, it would have changed the FQDN. There's also a trick I read to change the hostname with the command sysctl kernel.hostname=server1, but nothing says whether this is the correct way or an ugly trick.



      So:



      1. What is the correct way to set the hostname?


      2. What is the correct way to set the domain name?










      share|improve this question















      I am renting a server, running Ubuntu 16.04 at a company, let's name it company.org.



      Currently, my server is configured like this:



      • hostname: server737263

      • domain name: company.org

      Here's my FQDN:



      user@server737263:~ $ hostname --fqdn
      server737263.company.org


      This is not surprising.



      I am also renting a domain name, let's name it domain.org. What I would like to do would be to rename my server as server1.domain.org.



      This means configuring my hostname as server1 and my domain name as domain.org.



      How can I do it correctly?



      Indeed, the manpage for hostname is not clear. To me at least:




      HOSTNAME(1)



      [...]



      SET NAME



      • When called with one argument or with the --file option, the commands set the host name or the NIS/YP domain name. hostname uses
        the sethostname(2) function, while all of the three domainname,
        ypdomainname and nisdomainname use setdomainname(2). Note, that this
        is effective only until the next reboot. Edit /etc/hostname for
        permanent change.


      [...]



      THE FQDN



      • You cannot change the FQDN with hostname or dnsdomainname.

      [...]




      So it seems that editing /etc/hostname is not enough? Because if it really changed the hostname, it would have changed the FQDN. There's also a trick I read to change the hostname with the command sysctl kernel.hostname=server1, but nothing says whether this is the correct way or an ugly trick.



      So:



      1. What is the correct way to set the hostname?


      2. What is the correct way to set the domain name?







      ubuntu hostname domain






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '16 at 23:56









      Jeff Schaller

      35.2k952115




      35.2k952115










      asked Nov 12 '16 at 23:38









      titsou

      108114




      108114




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          21
          down vote



          accepted










          Setting your hostname:



          • You'll want to edit /etc/hostname with your new hostname.


          • Then, run sudo hostname $(cat /etc/hostname).


          Setting your domain:



          • Then, in /etc/resolvconf/resolv.conf.d/head, you'll add then line domain your.domain.name (not your FQDN, just the domainname).


          • Then, run sudo resolvconf -u to update your /etc/resolv.conf (alternatively, just reproduce the previous change into your /etc/resolv.conf).


          Both:



          Finally, update your /etc/hosts file. There should be at least one line starting with one of your IP (loopback or not), your FQDN and your hostname. grepping out ipv6 addresses, your hosts file could look like this:



          127.0.0.1 localhost
          1.2.3.4 service.domain.com service





          share|improve this answer






















          • does this work for AWS or other VPS?
            – Thufir
            Jan 3 '17 at 7:52










          • Running on EC2, note that you may want to set some DHCP options to your VPC, pushing the proper domain name - or disable DHCP client from your AMIs - to prevent /etc/resolv.conf from being rewritten.
            – SYN
            Jan 4 '17 at 1:04







          • 6




            Both those conf files say DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN on my server (same version).
            – Walf
            May 1 '17 at 1:26






          • 4




            Don't be fooled by the fact that the "DO NOT EDIT" warning appears in /etc/resolvconf/resolv.conf.d/head. Everything in the head file is prepended to the resulting /etc/resolv.conf output file, so that's why the warning is in there, so that it shows up in the final result. Threw me for a loop at first.
            – njbair
            Jan 6 at 23:24






          • 1




            This doesn't seem to apply to 18.04 LTS - there's no such file /etc/resolvconf/resolv.conf.d/head and there's no such utility resolvconf.
            – inopinatus
            Jun 4 at 3:42

















          up vote
          0
          down vote













          sudo nano /etc/hostname



          hostname.domain.com


          sudo nano /etc/hosts



          127.0.0.1 hostname.domain.com hostname localhost


          REBOOT!



          MUST HAVE SINGLE HOSTNAME after FQDN in /etc/hosts file. Works fine on Ubuntu 18.04.1 and all other versions. On EC2 and elsewhere.



          Didn't mess with resolve file or anything else.



          That shows hostname in shell and then has the FQDN when you need it.





          share








          New contributor




          Richard Westby-Nunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

















            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',
            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%2f322883%2fhow-to-correctly-set-hostname-and-domain-name%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            21
            down vote



            accepted










            Setting your hostname:



            • You'll want to edit /etc/hostname with your new hostname.


            • Then, run sudo hostname $(cat /etc/hostname).


            Setting your domain:



            • Then, in /etc/resolvconf/resolv.conf.d/head, you'll add then line domain your.domain.name (not your FQDN, just the domainname).


            • Then, run sudo resolvconf -u to update your /etc/resolv.conf (alternatively, just reproduce the previous change into your /etc/resolv.conf).


            Both:



            Finally, update your /etc/hosts file. There should be at least one line starting with one of your IP (loopback or not), your FQDN and your hostname. grepping out ipv6 addresses, your hosts file could look like this:



            127.0.0.1 localhost
            1.2.3.4 service.domain.com service





            share|improve this answer






















            • does this work for AWS or other VPS?
              – Thufir
              Jan 3 '17 at 7:52










            • Running on EC2, note that you may want to set some DHCP options to your VPC, pushing the proper domain name - or disable DHCP client from your AMIs - to prevent /etc/resolv.conf from being rewritten.
              – SYN
              Jan 4 '17 at 1:04







            • 6




              Both those conf files say DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN on my server (same version).
              – Walf
              May 1 '17 at 1:26






            • 4




              Don't be fooled by the fact that the "DO NOT EDIT" warning appears in /etc/resolvconf/resolv.conf.d/head. Everything in the head file is prepended to the resulting /etc/resolv.conf output file, so that's why the warning is in there, so that it shows up in the final result. Threw me for a loop at first.
              – njbair
              Jan 6 at 23:24






            • 1




              This doesn't seem to apply to 18.04 LTS - there's no such file /etc/resolvconf/resolv.conf.d/head and there's no such utility resolvconf.
              – inopinatus
              Jun 4 at 3:42














            up vote
            21
            down vote



            accepted










            Setting your hostname:



            • You'll want to edit /etc/hostname with your new hostname.


            • Then, run sudo hostname $(cat /etc/hostname).


            Setting your domain:



            • Then, in /etc/resolvconf/resolv.conf.d/head, you'll add then line domain your.domain.name (not your FQDN, just the domainname).


            • Then, run sudo resolvconf -u to update your /etc/resolv.conf (alternatively, just reproduce the previous change into your /etc/resolv.conf).


            Both:



            Finally, update your /etc/hosts file. There should be at least one line starting with one of your IP (loopback or not), your FQDN and your hostname. grepping out ipv6 addresses, your hosts file could look like this:



            127.0.0.1 localhost
            1.2.3.4 service.domain.com service





            share|improve this answer






















            • does this work for AWS or other VPS?
              – Thufir
              Jan 3 '17 at 7:52










            • Running on EC2, note that you may want to set some DHCP options to your VPC, pushing the proper domain name - or disable DHCP client from your AMIs - to prevent /etc/resolv.conf from being rewritten.
              – SYN
              Jan 4 '17 at 1:04







            • 6




              Both those conf files say DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN on my server (same version).
              – Walf
              May 1 '17 at 1:26






            • 4




              Don't be fooled by the fact that the "DO NOT EDIT" warning appears in /etc/resolvconf/resolv.conf.d/head. Everything in the head file is prepended to the resulting /etc/resolv.conf output file, so that's why the warning is in there, so that it shows up in the final result. Threw me for a loop at first.
              – njbair
              Jan 6 at 23:24






            • 1




              This doesn't seem to apply to 18.04 LTS - there's no such file /etc/resolvconf/resolv.conf.d/head and there's no such utility resolvconf.
              – inopinatus
              Jun 4 at 3:42












            up vote
            21
            down vote



            accepted







            up vote
            21
            down vote



            accepted






            Setting your hostname:



            • You'll want to edit /etc/hostname with your new hostname.


            • Then, run sudo hostname $(cat /etc/hostname).


            Setting your domain:



            • Then, in /etc/resolvconf/resolv.conf.d/head, you'll add then line domain your.domain.name (not your FQDN, just the domainname).


            • Then, run sudo resolvconf -u to update your /etc/resolv.conf (alternatively, just reproduce the previous change into your /etc/resolv.conf).


            Both:



            Finally, update your /etc/hosts file. There should be at least one line starting with one of your IP (loopback or not), your FQDN and your hostname. grepping out ipv6 addresses, your hosts file could look like this:



            127.0.0.1 localhost
            1.2.3.4 service.domain.com service





            share|improve this answer














            Setting your hostname:



            • You'll want to edit /etc/hostname with your new hostname.


            • Then, run sudo hostname $(cat /etc/hostname).


            Setting your domain:



            • Then, in /etc/resolvconf/resolv.conf.d/head, you'll add then line domain your.domain.name (not your FQDN, just the domainname).


            • Then, run sudo resolvconf -u to update your /etc/resolv.conf (alternatively, just reproduce the previous change into your /etc/resolv.conf).


            Both:



            Finally, update your /etc/hosts file. There should be at least one line starting with one of your IP (loopback or not), your FQDN and your hostname. grepping out ipv6 addresses, your hosts file could look like this:



            127.0.0.1 localhost
            1.2.3.4 service.domain.com service






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 25 at 17:11









            Benjamin Moore

            32




            32










            answered Nov 12 '16 at 23:51









            SYN

            1,824415




            1,824415











            • does this work for AWS or other VPS?
              – Thufir
              Jan 3 '17 at 7:52










            • Running on EC2, note that you may want to set some DHCP options to your VPC, pushing the proper domain name - or disable DHCP client from your AMIs - to prevent /etc/resolv.conf from being rewritten.
              – SYN
              Jan 4 '17 at 1:04







            • 6




              Both those conf files say DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN on my server (same version).
              – Walf
              May 1 '17 at 1:26






            • 4




              Don't be fooled by the fact that the "DO NOT EDIT" warning appears in /etc/resolvconf/resolv.conf.d/head. Everything in the head file is prepended to the resulting /etc/resolv.conf output file, so that's why the warning is in there, so that it shows up in the final result. Threw me for a loop at first.
              – njbair
              Jan 6 at 23:24






            • 1




              This doesn't seem to apply to 18.04 LTS - there's no such file /etc/resolvconf/resolv.conf.d/head and there's no such utility resolvconf.
              – inopinatus
              Jun 4 at 3:42
















            • does this work for AWS or other VPS?
              – Thufir
              Jan 3 '17 at 7:52










            • Running on EC2, note that you may want to set some DHCP options to your VPC, pushing the proper domain name - or disable DHCP client from your AMIs - to prevent /etc/resolv.conf from being rewritten.
              – SYN
              Jan 4 '17 at 1:04







            • 6




              Both those conf files say DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN on my server (same version).
              – Walf
              May 1 '17 at 1:26






            • 4




              Don't be fooled by the fact that the "DO NOT EDIT" warning appears in /etc/resolvconf/resolv.conf.d/head. Everything in the head file is prepended to the resulting /etc/resolv.conf output file, so that's why the warning is in there, so that it shows up in the final result. Threw me for a loop at first.
              – njbair
              Jan 6 at 23:24






            • 1




              This doesn't seem to apply to 18.04 LTS - there's no such file /etc/resolvconf/resolv.conf.d/head and there's no such utility resolvconf.
              – inopinatus
              Jun 4 at 3:42















            does this work for AWS or other VPS?
            – Thufir
            Jan 3 '17 at 7:52




            does this work for AWS or other VPS?
            – Thufir
            Jan 3 '17 at 7:52












            Running on EC2, note that you may want to set some DHCP options to your VPC, pushing the proper domain name - or disable DHCP client from your AMIs - to prevent /etc/resolv.conf from being rewritten.
            – SYN
            Jan 4 '17 at 1:04





            Running on EC2, note that you may want to set some DHCP options to your VPC, pushing the proper domain name - or disable DHCP client from your AMIs - to prevent /etc/resolv.conf from being rewritten.
            – SYN
            Jan 4 '17 at 1:04





            6




            6




            Both those conf files say DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN on my server (same version).
            – Walf
            May 1 '17 at 1:26




            Both those conf files say DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN on my server (same version).
            – Walf
            May 1 '17 at 1:26




            4




            4




            Don't be fooled by the fact that the "DO NOT EDIT" warning appears in /etc/resolvconf/resolv.conf.d/head. Everything in the head file is prepended to the resulting /etc/resolv.conf output file, so that's why the warning is in there, so that it shows up in the final result. Threw me for a loop at first.
            – njbair
            Jan 6 at 23:24




            Don't be fooled by the fact that the "DO NOT EDIT" warning appears in /etc/resolvconf/resolv.conf.d/head. Everything in the head file is prepended to the resulting /etc/resolv.conf output file, so that's why the warning is in there, so that it shows up in the final result. Threw me for a loop at first.
            – njbair
            Jan 6 at 23:24




            1




            1




            This doesn't seem to apply to 18.04 LTS - there's no such file /etc/resolvconf/resolv.conf.d/head and there's no such utility resolvconf.
            – inopinatus
            Jun 4 at 3:42




            This doesn't seem to apply to 18.04 LTS - there's no such file /etc/resolvconf/resolv.conf.d/head and there's no such utility resolvconf.
            – inopinatus
            Jun 4 at 3:42












            up vote
            0
            down vote













            sudo nano /etc/hostname



            hostname.domain.com


            sudo nano /etc/hosts



            127.0.0.1 hostname.domain.com hostname localhost


            REBOOT!



            MUST HAVE SINGLE HOSTNAME after FQDN in /etc/hosts file. Works fine on Ubuntu 18.04.1 and all other versions. On EC2 and elsewhere.



            Didn't mess with resolve file or anything else.



            That shows hostname in shell and then has the FQDN when you need it.





            share








            New contributor




            Richard Westby-Nunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





















              up vote
              0
              down vote













              sudo nano /etc/hostname



              hostname.domain.com


              sudo nano /etc/hosts



              127.0.0.1 hostname.domain.com hostname localhost


              REBOOT!



              MUST HAVE SINGLE HOSTNAME after FQDN in /etc/hosts file. Works fine on Ubuntu 18.04.1 and all other versions. On EC2 and elsewhere.



              Didn't mess with resolve file or anything else.



              That shows hostname in shell and then has the FQDN when you need it.





              share








              New contributor




              Richard Westby-Nunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.



















                up vote
                0
                down vote










                up vote
                0
                down vote









                sudo nano /etc/hostname



                hostname.domain.com


                sudo nano /etc/hosts



                127.0.0.1 hostname.domain.com hostname localhost


                REBOOT!



                MUST HAVE SINGLE HOSTNAME after FQDN in /etc/hosts file. Works fine on Ubuntu 18.04.1 and all other versions. On EC2 and elsewhere.



                Didn't mess with resolve file or anything else.



                That shows hostname in shell and then has the FQDN when you need it.





                share








                New contributor




                Richard Westby-Nunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                sudo nano /etc/hostname



                hostname.domain.com


                sudo nano /etc/hosts



                127.0.0.1 hostname.domain.com hostname localhost


                REBOOT!



                MUST HAVE SINGLE HOSTNAME after FQDN in /etc/hosts file. Works fine on Ubuntu 18.04.1 and all other versions. On EC2 and elsewhere.



                Didn't mess with resolve file or anything else.



                That shows hostname in shell and then has the FQDN when you need it.






                share








                New contributor




                Richard Westby-Nunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.








                share


                share






                New contributor




                Richard Westby-Nunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered 5 mins ago









                Richard Westby-Nunn

                1




                1




                New contributor




                Richard Westby-Nunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                Richard Westby-Nunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                Richard Westby-Nunn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f322883%2fhow-to-correctly-set-hostname-and-domain-name%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?