3

I have two CentOS 7 VM's running in virtualbox. On each of the machines I want to set the hostname and a static IP address. VM1 works just fine. VM2 does not. I did the same thing on both servers so I'm not sure why VM2 is having issues. It shows as localhost.localdomain and I can't get it to read the new correct hostname. Here is what I've done:

Modified the /etc/sysconfic/network file as follows:

NETWORKING=yes
HOSTNAME=newhost.newdomain

Modified the /etc/resolv.conf file as follows:

nameserver 8.8.8.8

Modified the /etc/sysconfig/network-scripts/ifcfg-enp0s3 file as follows:

HWADDR=#
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=enp0s3
UUID=#
ONBOOT=yes
IPADDR=192.168.10.1
NETMASK=255.255.255.0
NM_CONTROLLER=no
GATEWAY=192.168.10.100

The interface works and the IP is assigned as specified. The only thing that does not work is the hostname. I can change it temporarily by using the 'hostname {newname}' command but that is only a temp fix as it reverts back on reboot. All of this is the same as on VM1 (except for the IP address assigned) and VM1 works fine. I'm not concerned with the hosts file at the moment since I'm not worried about name resolution; I'm just worried with the hostname.

Any thoughts or suggestions?

  • What does your /etc/hosts file contain? Also what does sysctl kernel.hostname say ? – Hunter McMillen Nov 20 '14 at 16:19
  • The hosts file is the default (localhost) and I'm ok with that as I'm not worried about name resolution at the moment. I can't run the other command right now and get the results you need since I ran hostnamectl and set the host name that way and it appears to have stuck. I checked all my config files again and the only change I see is that it removed my name server entry from the resolv.conf file and added a line for 'search newdomain'. I removed that line and added back in my nameserver and it's still working. Not sure what else that command changed that actually fixed the issue. – Michael Haynes Nov 20 '14 at 16:30
  • After changing /etc/hostname , you have to change with host commmand for showing in prompt. – PersianGulf Nov 20 '14 at 17:47
7

Try setting the host name in /etc/hostname

From the hostname man page on my CentOS 7 machine:

The host name is usually set once at system startup (normally by read‐ing the >contents of a file which contains the host name, e.g. /etc/hostname).

  • I'm going to accept this as the answer. See my other answer for what I did on the current setup to get it working. After I had it working I wiped the VM and started from scratch and got the same error. Instead of running 'hostnamectl' I just updated this file and it works. Still left with the concern of why VM1 didn't require this step but that's a different question. Thanks ChrisV. – Michael Haynes Nov 20 '14 at 19:18
  • I believe I figured out the root of the issue. I think that on VM1 I started to set up the network during installation but decided not to and turned it off. However, I think I may have left the hostname populated with the hostname I wanted. I do not remember if it booted with the proper hostname or not as I'm so used to it defaulting to localhost and having to change it that I didn't notice. However, I setup a 3rd VM and set the hostname on the network setup screen (I left networking turned off) and it booted with the correct hostname. That's the only thing I can think of. – Michael Haynes Nov 20 '14 at 19:47
12

Set the hostname with:

hostnamectl set-hostname host.domain
  • 1
    hostnamectl set-hostname host.domain should set the static, transient, and the pretty version of the hostname, IIRC. <br /> EDIT: yup, in the manpage for hostnamectl (1): set-hostname NAME Set the system hostname to NAME. By default, this will alter the pretty, the static, and the transient hostname alike; however, if one or more of --static, --transient, --pretty are used, only the selected hostnames are changed. – ILMostro_7 Oct 24 '15 at 4:29
  • 1
    updated according to @ILMostro_7 comment – Pavel Tankov Sep 29 '16 at 14:44
  • Tried the selected answer, didn't work. This did. Thanks. – a coder Feb 8 '17 at 19:13
  • This works even after reboot when the [hostname] command did not. – Lionet Chen Dec 22 '17 at 4:32
1

Ok, so I found a way to get this working although I don't know what actually happened to fix it. What I did was run 'hostnamectl {newname}'. This set the hostname to the new value and I am able to reboot and retain the settings. This made me curious as to what changed so I looked back over the 3 config files listed previously and the only change was to the 'resolv.conf' file. My nameserver value had been removed an a new line reading 'search {localdomain}' was put in.

Out of curiosity I decided to remove the new line and put my nameserver value back in and rebooted. After reboot everything was still looking good. So it appears that that command fixed the issue but I have no idea what it actually did to fix it.

At this point I'm going to wipe out the VM and start from scratch to see if I can reproduce the issue again and will look at the hostname file ChrisV mentioned. Perhaps that was changed. But that still leaves me with the one big burning question: why did VM1 work just fine while VM2 failed and they were setup at the exact same time in the exact same way (the only way to have more exact would be to clone VM1). Maybe after a few more wipes and reinstalls I'll stumble upon something that indicates what the root of the issue is.

  • I think I know what happened. When you update your system with a new kernel, the rpm installation automatically triggers the re-generation of a new initrd/initramfs file. The program it uses in RHEL/CENTOS7 is dracut. Apparently, this will somehow use your current host's hostname and build it in as a 'fallback' hostname. When systemd starts up, if it can find no other hostname, it will use this fallback as the "transient" hostname. – Otheus May 23 '16 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.