system configuration - Red Hat based systems
For many years Red Hat systems have used the same approach for configuration of the hostname. Starting with RHEL 7 and Fedora 18 there is a new procedure for changing the hostname that represents a significant break with this tradition. There are now multiple hostname classes and a new command to manage them. This new password scheme is a component of systemd.
The legacy and new configuration procedures required to make a permanent hostname change on current Red Hat and Fedora systems is covered below.
Traditional Red Hat Hostname Configuration
Applicable: RHEL6, RHEL5, Fedora 17 and olderIf possible you should reboot the system when changing the hostname, however, the steps for changing the hostname with and without a system reboot are covered.
Procedure Overview
How to Change Hostname with Reboot
- Edit /etc/sysconfig/network and update HOSTNAME entry
- Update hostname in /etc/hosts
- Reboot
- Verify hostname
How to Change Hostname without Reboot
- Edit /etc/sysconfig/network and update HOSTNAME entry
- Update hostname in /etc/hosts
- Change hostname with command: hostname new_hostname
- Verify hostname
- Restart system logging
- Restart system networking
- Consider rebooting when convenient
Procedure Detail
If possible plan to reboot the system when you change the hostname. There are a number of reasons, some of them discussed below, to strongly favour this approach. Before starting you may also wish to review the "Additional Considerations" section below to help plan for the change.
1. Edit /etc/sysconfig/network and update the HOSTNAME entry. A fully qualified domain name should be used.
- vi /etc/sysconfig/network
- HOSTNAME=newname.example.com
2. Update hostname in /etc/hosts if required.
- vi /etc/hosts
3. Reboot - reboot NOW if possible. If you cannot reboot skip to step 4.
- shutdown -r now
Continue with this section only if you cannot reboot
4. Update the hostname using the hostname command. You should use the fully qualified domain name.
- hostname newname.example.com
5. Restart system logging with the new hostname.
To restart logging on RHEL5:
- service syslog restart
To restart logging on RHEL6:
- service rsyslog restart
6. Restart networking
- service network restart
At this point you should hopefully be in pretty good shape, however, there may still be processes that have a reference to the old hostname. As an example, if you have not logged off and back on since the hostname change, look at your shell session. If you echo $HOSTNAME you will see the old hostname. If your command prompt incorporates the hostname it may still display the old hostname. You can address this for your current session by logging off and back on, however, the point is that long running processes may continue to hold a reference to the old hostname. This may or may not be of consequence.
Use your knowledge of your environment to examine running applications to determine if there could be a potential issue. If there are applications for which there is a concern you might consider restarting them if possible. You might also consider rebooting when possible.
New Red Hat Hostname Configuration
Applicable: RHEL7, Fedora-18 and newer [Fedora-20]Starting with RHEL7 and Fedora 18 the hostname is no longer set in /etc/sysconfig/network. There are now 3 classes of hostnames and a new command named hostnamectl that communicates with a service daemon named systemd-hostnamed for setting and querying hostnames.
The classes are named static, transient, and pretty. Static is the default hostname and is stored in a system configuration file (i.e., /etc/hostname). This represents the traditional user configured persistent hostname. On servers and other non-mobile devices this is all you will probably ever need to worry about. The transient hostname defaults to the static hostname but can be dynamically updated and presumably fills a role in the mobile computing arena. At the moment pretty just strikes me as a dubious idea but perhaps I just do not know enough to understand where it might be relevant. By default it is not set.
The command 'hostnamectl' or 'hostnamectl status' displays certain host information including the static hostname. The transient and pretty hostnames will be displayed only if they are set and are unique from the static hostname.
- hostnamectl status
- hostnamectl --static status
- hostnamectl --transient status
- hostnamectl --pretty status
The hostname and sysctl kernel.hostname commands can still be used for displaying the current hostname and are important for doing so since the hostnamectl output is not always accurate.
1. To permanently change the system password perform the following steps. We assume you are running a default configuration where transient is inherited from static and pretty is not set. For other scenarios you may wish to consult the man page for other options.
- hostnamectl set-hostname newhostname.example.com
Note: If the provided hostname is NOT a legal hostname the command will assume you want to use that as the pretty hostname and will automatically convert the supplied name into a simplified legal name to be used for the static and transient hostnames.
2. Update hostname in the /etc/hosts file if required.
- vi /etc/hosts
3. Reboot - Reboot now if possible.
- shutdown -r now
If you cannot reboot at this time you should presumably restart the network (systemctl restart network) and review all services, applications, and processes that have been running since before the hostname change to determine if they might have a reference to the old hostname and refresh if needed. This is discussed in greater detail in the "Traditional Red Hat Hostname Configuration" section above. The part about logging in that section might not be relevant in this case.
Notes on hostnamectl and systemd-hostnamed
Exploring the Strangeness
There are a few things that might be worth understanding that accompany this new hostname configuration scheme.
Verification
First, if you have not rebooted, you should verify your change not just with the hostnamectl command but also with the 'hostname' or 'sysctl kernel.hostname' commands as well. This is due to the potential for hostnamectl status to temporarily report inaccurate results. The following commands are useful for gathering hostname information:
- hostnamectl status
- hostnamectl --static status
- hostnamectl --transient status
- hostnamectl --pretty status
- hostname
- sysctl kernel.hostname
The hostname command
The hostname command still exists and is useful for displaying the current effective system hostname. It can also still set the hostname (non-persistant) as in the past. Mapped to the new hostname scheme it can be thought of as reporting and setting the transient hostname.
However, hostnamectl is now the appropriate command to use for changing hostnames. The hostnamectl command communicates with the systemd-hostnamed service deamon.
If you change the hostname without using hostnamectl the hostname information reported by hostnamectl may be inconsistent with the actual system state for a period of time. This can be confusing when trying to set and verify hostnames if you are unaware of this behavior. Any tools or scripts that use hostnamectl may also act on inaccurate information.
This is not as bad, however, as it first appears since the systemd-hostnamed service daemon will eventually terminate itself after a period of inactivity. The next time it is invoked it will have the then current information. You can also manually refresh it if desired (e.g., systemctl restart systemd-hostnamed) to ensure you see the current state.
The /etc/hostname file
The /etc/hostname file is where the permanent system static hostname configuration is stored. However, unlike /etc/sysconfig/network on old Red Hat based systems, it is probably best not to edit this file directly. If you do one thing to be aware of is that the current hostname will immediately be changed. Also, as with the hostname command discussed above, the output from hostnamctl status will be inconsistent with the actual system state for a period of time.
Update: It appears the NetworkManager is detecting the change in /etc/hostname and applying it. With NetworkManager down simply updating /etc/hostname does not change the current hostname. However, by default NetworkManager is likely running.
Other observations
The hostnamectl command can temporarily report inaccurate hostname information even when used exclusively for setting the hostname.
From a system perspective, you cannot really set just the static hostname without impacting the transient hostname even though hostnamectl has an option to do so. In addition, in this case, the hostnamectl command can report inaccurate transient hostname information for a period of time.
Consider the following as an example:
- hostnamectl --static set-hostname newhostname.example.com
I speculate this is because hostnamectl in fact did not change the transient hostname when it was run and as a result systemd-hostnamed was not informed. However, as expected, it did update /etc/hostname and as we saw during testing the system is immediately aware of changes made to /etc/hostname and sets the current hostname when updated.
Update: This behavior appears to be the result of NetworkManager updating the current hostname based on contents of /etc/hostname. If NetworkManager is not running this behavior is not observed.
Note: All testing with hostnamectl was done on Fedora 20.
For more information on hostnamectl and the changes to system hostnames see the Fedora 18 release notes [section 2.3.2.4] and the Fedora 18 Systems Administrator_Guide
Resources
- http://docs.fedoraproject.org/en-US/Fedora/18/html/Release_Notes/sect-Release_Notes-Changes_for_Sysadmin.html#idm36771376 [section 2.3.2.4]
- http://docs.fedoraproject.org/en-US/Fedora/18/html/System_Administrators_Guide/s1_Using_Hostnamectl.html
- http://www.freedesktop.org/wiki/Software/systemd/hostnamed
Other Considerations
When changing a hostname you may need to consider more than just the system hostname change. Below are some possible areas to consider.
Do you need to update DNS?
Will you set an alias for the transition period? Remember to remove it when appropriate?
Do you need to update /etc/hosts on other systems?
Do you need to update nfs references in /etc/fstab or /etc/exports files?
Do programs or scripts have hard coded host references that need to be updated?
Do user clients have to be updated? How will users be informed? In an an enterprise environment has the help desk and client systems group been informed?
...
1 comment:
I set the hostname but upon reboot the systemd-hostnamed changes it back to the original hostname.
I have used hostname, /etc/hostname , /etc/hosts, nmtui, AND hostnamectl, but none of them are persistent upon reboot.
Here is /var/log/messages , where it changes the hostname
Oct 17 16:55:26 sim systemd[1]: No hostname configured.
Oct 17 16:55:26 sim systemd[1]: Set hostname to .
Oct 17 16:55:29 sim systemd: Set hostname to .
Oct 17 16:55:30 sim NetworkManager[372]: hostname 'ip-172-31-18-83'
Oct 17 16:55:32 sim dbus-daemon: dbus[386]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Oct 17 16:55:32 sim dbus[386]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Oct 17 16:55:32 sim systemd: Starting Hostname Service...
Oct 17 16:55:32 sim dbus-daemon: dbus[386]: [system] Successfully activated service 'org.freedesktop.hostname1'
Oct 17 16:55:32 sim dbus[386]: [system] Successfully activated service 'org.freedesktop.hostname1'
Oct 17 16:55:32 sim systemd: Started Hostname Service.
Oct 17 16:55:32 sim systemd-hostnamed: Changed static host name to 'ip-172-31-18-83.ec2.internal'
Oct 17 16:55:32 sim systemd-hostnamed: Changed host name to 'ip-172-31-18-83.ec2.internal'
[root@ip-172-31-18-83 ec2-user]# date
Fri Oct 17 16:57:09 EDT 2014
[root@ip-172-31-18-83 ec2-user]#
Any suggestions?
Post a Comment