Join the community today
Register Now

Install CentOS 7 - Hostname Changes after Reboot

Discussion in 'Install & Upgrades or Pre-Install Questions' started by nqservices, Jun 17, 2016.

  1. nqservices

    nqservices Member

    70
    14
    8
    Jun 17, 2016
    Ratings:
    +28
    Local Time:
    9:02 AM
    Hi eva2000,

    Thanks again for your help!

    Just a situation status point: at this moment my hostname is correct showing the subdomain.domain.com but after reboot the hostname revert's back to just subdomain. I'm still waiting for a response from my hosting provider about this.


    After replacing the "persistent_host_name" to be the first on the line and restart network i run the 3 commands you told me. The results are bellow:

    ---
    [root@subdomain ~]# cat /etc/sysconfig/network-scripts/ifcfg-venet0
    DEVICE=venet0
    BOOTPROTO=static
    ONBOOT=yes
    ARPCHECK="no"
    IPADDR=127.0.0.1
    NETMASK=255.255.255.255
    BROADCAST=0.0.0.0
    ARPCHECK="no"

    [root@subdomain ~]# cat /etc/sysconfig/network-scripts/ifcfg-venet0:0
    DEVICE=venet0:0
    ONBOOT=yes
    ARPCHECK="no"
    IPADDR=XXX.XXX.XX.XX
    NETMASK=255.255.255.255

    [root@subdomain ~]# cat /etc/sysconfig/network-scripts/ifcfg-venet0:1
    cat: /etc/sysconfig/network-scripts/ifcfg-venet0:1: No such file or directory
    ---

    Update after Reboot:
    I have now reboot the VPS server and problem still exists. I noticed that to resolve the problem the only placed i need to change was on the "/etc/hostname" file. So is just this file that is not saving after reboot. Also in terms of the "/etc/hosts" "persistent_host_name" order to be in first, the order has automatic changed again after reboot. So now "persistent_host_name" is again before the "localhost4". All the other settings / files saved the settings properly.


    Thanks
     
    Last edited: Jun 18, 2016
  2. nqservices

    nqservices Member

    70
    14
    8
    Jun 17, 2016
    Ratings:
    +28
    Local Time:
    9:02 AM
    Hi,

    I have already received the response from my hosting provider about this hostname issue and their response was that i related witn CentOS 7. I quote their response bellow:

    "This is normal CentOS 7 behavior. If you want to get full not shorten hostname use command "hostname -f" instead of just "hostname. Could you clarify why do you need to to show extended hostname with for example "uname -n", when you can use "hostname -f" instead? This is made by CentOS creators, if you want to change it you should contact them."

    So i have reboot my server now and after reboot this is what it shows:

    ------
    [root@subdomain ~]# uname -n
    subdomain

    [root@subdomain ~]# hostname -f
    subdomain.domain.com

    [root@subdomain ~]# hostnamectl
    Static hostname: subdomain
    Icon name: computer-container
    Chassis: container
    Machine ID: 22ca1661401845c8935f2d08d130acd0
    Boot ID: 53040c02424a4164aefce8aa112b0065
    Virtualization: openvz
    Operating System: CentOS Linux 7 (Core)
    CPE OS Name: cpe:/o:centos:centos:7
    Kernel: Linux 2.6.32-042stab113.12
    Architecture: x86-64
    ------

    Any ideas?
     
  3. eva2000

    eva2000 Administrator Staff Member

    58,894
    12,490
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +19,122
    Local Time:
    6:02 PM
    Nginx 1.31.x
    MariaDB 10.x/11.4+/12.3+
    Well it definitely ain't that from my experience with CentOS 7 on either openvz, xen or kvm on non-virtualised servers. Setting the hostname via various outlined and discussed methods in this thread should persistent on server reboot !

    maybe try a different web host's centos 7 OS template ?

    however, it should work still technically if /etc/hosts is setup correctly as yours is

    Code (Text):
    XXX.XXX.XX.XX subdomain.domain.com subdomain


    just centmin mod looks for full hostname via hostname command without -f which I guess i need to change
     
    Last edited: Jun 21, 2016
  4. eva2000

    eva2000 Administrator Staff Member

    58,894
    12,490
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +19,122
    Local Time:
    6:02 PM
    Nginx 1.31.x
    MariaDB 10.x/11.4+/12.3+
    this is what i see - as you can see centminmod relying on hostname without -f gives the full hostname
    Code (Text):
    [root@centos7 ~]# hostname
    centos7.localdomain
    

    hostname shortname
    Code (Text):
    hostname -s
    centos7
    

    while with FQDN -f gives localhost matching first entry in /etc/hosts
    Code (Text):
    [root@centos7 ~]# hostname -f
    localhost
    

    Code (Text):
    cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    

    Code (Text):
    [root@centos7 ~]# uname -a
    Linux centos7.localdomain 3.10.0-327.13.1.el7.x86_64 #1 SMP Thu Mar 31 16:04:38 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
    

    Code (Text):
    [root@centos7 ~]# hostnamectl
       Static hostname: centos7.localdomain
             Icon name: computer-vm
               Chassis: vm
            Machine ID: d1f656550e22485ba59f516e548d99df
               Boot ID: 3d083dcc460b4187a3b026fa235c3dd6
        Virtualization: oracle
      Operating System: CentOS Linux 7 (Core)
           CPE OS Name: cpe:/o:centos:centos:7
                Kernel: Linux 3.10.0-327.13.1.el7.x86_64
          Architecture: x86-64

    Code (Text):
    cat /proc/sys/kernel/hostname
    centos7.localdomain
    
     
    Last edited: Jun 21, 2016
  5. apidevlab

    apidevlab Member

    91
    33
    18
    Mar 22, 2016
    /dev/null
    Ratings:
    +58
    Local Time:
    9:02 AM
    1.11.1
    5.2.14-122

    Baffled, I don't use Time4VPS with Centos 6/7 I use their 'Filer' OS and the hostname is set as xxx.xx.time4vps.eu which I just point to (IP) an A record. I know major changes were made to Centos 7 which is 1 reason I have avoided it TBH

    This may be of help:

    To change all three hostnames: static, transient, and pretty, simultaneously:
    Code (Text):
    $ hostnamectl set-hostname <host-name>

    How to change hostname on CentOS or RHEL 7 - Ask Xmodulo
     
  6. eva2000

    eva2000 Administrator Staff Member

    58,894
    12,490
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +19,122
    Local Time:
    6:02 PM
    Nginx 1.31.x
    MariaDB 10.x/11.4+/12.3+
    @apidevlab i am baffled too as that is what @nqservices did try too using the recommended CentOS 7 method
    Code (Text):
    hostnamectl set-hostname <host-name>
    

    but still on reboot it reverts for him while for on all my CentOS 7 servers and testing reboots persist using hostnamectl method
     
  7. apidevlab

    apidevlab Member

    91
    33
    18
    Mar 22, 2016
    /dev/null
    Ratings:
    +58
    Local Time:
    9:02 AM
    1.11.1
    5.2.14-122
    I agree with your hunch that Time4VPS are possibly using a custom template (they use Hostbill to setup/manage instances) the fact they state After hostname is changed we will not be able to change it back to default xxxx.s.time4vps.eu seems to my mind imply they may have a weird setup I don't know? The 'Filer' OS runs a customised Centos6 which as I said is what I use, like you every other Centos 7 hostname change has never been an issue.

    Suggest OP should try to contact T4VPS support again (maybe link to this thread) they could ask T4VPS to login to the instance and see if they can change the hostname or as an alternative which is poor I know but try re-install or Centos 6
     
  8. eva2000

    eva2000 Administrator Staff Member

    58,894
    12,490
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +19,122
    Local Time:
    6:02 PM
    Nginx 1.31.x
    MariaDB 10.x/11.4+/12.3+
    I agree though techinically @nqservices's setup is technically working properly server wise meaning /etc/hosts is setup properly so that shortname and FQDN are setup so either works and will resolve is DNS setup is correct

    The issue is Centmin Mod automated install routine uses the value from hostname without -f to setup nginx main hostname's nginx vhost and server_name within vhost file at /usr/local/nginx/conf/conf.d/virtual.conf . And usually out of box OS don't have a specific main hostname to ip setup in /etc/hosts so hostname -f won't give a proper full hostaname for nginx vhost setup. Of course you can manually edit /usr/local/nginx/conf/conf.d/virtual.conf and server_name to proper full hostname if it's incorrect.

    FYI, to edit /usr/local/nginx/conf/conf.d/virtual.conf you can also use centmin mod command shortcuts = vhostconf

    when typed vhostconf involves nano text editor edit of /usr/local/nginx/conf/conf.d/virtual.conf

    see FAQ item 16 for more details on cmd shortcuts
     
  9. nqservices

    nqservices Member

    70
    14
    8
    Jun 17, 2016
    Ratings:
    +28
    Local Time:
    9:02 AM
    Hi eva2000,

    I have follow your advice and bought a CentOS 7 test server with Vultr.com and there the hostname works properly. So i think the problem may be related with some specific configuration or software that Time4VPS uses.

    Also another strange thing i noticed, was when i made another test in Time4VPS by creating a new CentOS 7 server and the first time i login the server and type "uname -n" it showed the correct full hostname: "server.mydomain.com". I did not made any change, i just reboot the server using the option on Time4VPS client area and on the second time i login i type "uname -n" and the hostname had changed to just "server".

    So it seems that when server is created the hostname is correct and then after a reboot it always changes.

    I have already contacted Time4VPS support team and i'm now waiting for their response.

    Does anyone here on the forum uses Time4VPS? If yes can you share / tell if your hostname works properly with CentOS 7?

    I try that. It did not save the changes after reboot.
     
  10. eva2000

    eva2000 Administrator Staff Member

    58,894
    12,490
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +19,122
    Local Time:
    6:02 PM
    Nginx 1.31.x
    MariaDB 10.x/11.4+/12.3+
    yeah definitely something on their end !
     
  11. Steve Tozer

    Steve Tozer Member

    70
    42
    18
    Jul 28, 2014
    South Wales, UK
    Ratings:
    +49
    Local Time:
    9:02 AM
    1.91
    10.0.19
    Had this a couple of times pretty sure its to do with SolusVM / Templates

    If you

    Code:
    nano /etc/hostname
    Change it to the correct hostname

    Then run the below command

    Code:
    chattr +i /etc/hostname
    Reboot should keep the correct hostname. Let me know how you get off.
     
  12. nqservices

    nqservices Member

    70
    14
    8
    Jun 17, 2016
    Ratings:
    +28
    Local Time:
    9:02 AM
    Hi,

    Thanks for your advice. I will try. But before i try i have 2 questions:

    1- On the /etc/hostname file should i type just the beginning of the hostname (subdomain) or should i set the full hostname like;: subdomain.mydomain.com ?

    2- Also you said for me to reboot after the change. I can do the reboot using my hosting company control panel. But i wanted to know what is the best way to gracefully restart a CentOS 7 server. Can you please tell me what command you normally use?

    Thanks
     
  13. Steve Tozer

    Steve Tozer Member

    70
    42
    18
    Jul 28, 2014
    South Wales, UK
    Ratings:
    +49
    Local Time:
    9:02 AM
    1.91
    10.0.19
    Hello,

    In the /etc/hostname file you should put the full hostname eg, subdomain.mydomain.com

    Rebooting would normally just use the "reboot" command when logged in via SSH
     
  14. nqservices

    nqservices Member

    70
    14
    8
    Jun 17, 2016
    Ratings:
    +28
    Local Time:
    9:02 AM
    Hi @Steve Tozer ,

    You solution worked out perfect. Problem is finally resolved! Thanks so much!

    But anyway, i also receive today a response from my hosting provider about this and their solution is similar to your's. They said for me to do the following:

    Code:
    hostnamectl set-hostname subdomain.domain.com
    chattr +i /etc/hostname
    reboot
    ---

    I have try both ways and both ways work perfect! So i just wanted to know if from this 2 ways, is there one best than another or if it's exactly the same.

    So to change the hostname should i edit /etc/hostname directly as @Steve Tozer said, or should i use hostnamectl as my hosting provider said? As said before both ways work.

    Thanks
     
    Last edited: Jul 3, 2016
  15. Steve Tozer

    Steve Tozer Member

    70
    42
    18
    Jul 28, 2014
    South Wales, UK
    Ratings:
    +49
    Local Time:
    9:02 AM
    1.91
    10.0.19
    I'm sure
    hostnamectl set-hostname sets the /etc/hostname file which makes it the same

    then

    chattr +i /etc/hostname sets the file as not writeable so when the server reboots the network config on the host node doesn't overwrite the /etc/hostname file. Either way is likely to be correct ;)

    Glad you got it fixed at last :)
     
  16. nqservices

    nqservices Member

    70
    14
    8
    Jun 17, 2016
    Ratings:
    +28
    Local Time:
    9:02 AM
    Yes, at last. I was having this problem for a week and i was unable to setup new servers and spend many hours trying to fix it.

    Once again many thanks!
     
  17. eva2000

    eva2000 Administrator Staff Member

    58,894
    12,490
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +19,122
    Local Time:
    6:02 PM
    Nginx 1.31.x
    MariaDB 10.x/11.4+/12.3+
  18. nqservices

    nqservices Member

    70
    14
    8
    Jun 17, 2016
    Ratings:
    +28
    Local Time:
    9:02 AM
    Hi @eva2000 ,

    Yes, it seems to a SolusVM problem. I have also try this on a different hosting company on a VPS server running Debian 8 Minimal and the Hostname problem is the same.

    The solution gived by @Steve Tozer resolves the problem both in CentOS and also in Debian.
     
  19. eva2000

    eva2000 Administrator Staff Member

    58,894
    12,490
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +19,122
    Local Time:
    6:02 PM
    Nginx 1.31.x
    MariaDB 10.x/11.4+/12.3+
    who was the different hosting company ?

    i'ved tried and use over 10+ solusvm hosts and not one has this hostname problem on centos 6/7 for myself :)
     
  20. nqservices

    nqservices Member

    70
    14
    8
    Jun 17, 2016
    Ratings:
    +28
    Local Time:
    9:02 AM
    Hi @eva2000 ,

    The hosting company is Dacentec.com but there i did not try CentOS. Instead i used "Debian 8 Minimal".

    Also searching on Google for this issue i found a lot of people talking about this and how on reboot, the system goes back to the "old / default" hostname. It seems for a problem related with SolusVM + OpenVZ as you can see on the following 2 example links:

    Setting your hostname???? - LowEndTalk
    Hostname for OpenVZ Containers - Question for Falko | Howtoforge - Linux Howtos and Tutorials

    Once again thanks for all the help.