Pages

Monday 13 April 2020

Change linux hostname Cent OS/RHEL/Oracle Linux (proper way)

While requesting linux virtual machine at a vps provider I forgot to set the hostname I wanted it to have. As a result it got a default hostname which was a combination of date and time. Following google findings I changed the hostname in /etc/hosts and by executing hostname NEW_HOSTNAME. I tried hostnamectl set-hostname NEW_HOSTNAME too. That didn't work! I didn't have any other choice as going to the documentation. Then I realised I should have set the type of hostname I am setting up. Quick note from docs:

The static (configured) host name is the one configured in /etc/hostname or a similar file. It is chosen by the local user. It is not always in sync with the current host name as returned by the gethostname() system call. If no host name is configured this property will be the empty string. Setting this property to the empty string will remove /etc/hostname. This hostname should be an internet-style hostname, 7bit ASCII, no special chars/spaces, lower case.

The transient (dynamic) host name is the one configured via the kernel's sethostbyname(). It can be different from the static hostname in case DHCP or mDNS have been configured to change the name based on network information. This property is never empty. If no host name is set this will default to "localhost". Setting this property to the empty string will reset the dynamic hostname to the static host name. If no static host name is configured the dynamic host name will be reset to "localhost". This hostname should be an internet-style hostname, 7bit ASCII, no special chars/spaces, lower case.

The pretty host name is a free-form UTF8 host name for presentation to the user. UIs should ensure that the pretty hostname and the static hostname stay in sync. I.e. when the former is "Lennart's Computer" the latter should be "lennarts-computer". If no pretty host name is set this setting will be the empty string. Applications should then find a suitable fallback, such as the dynamic hostname.



Here I used static type: hostnamectl set-hostname NEW_HOSTNAME --static

No comments:

Post a Comment