Leaderboard ad


How to Change the System Hostname in Red Hat 7 or CentOS 7

Changing a hostname in RHEL7/CentOS 7 is a little different now that it uses SystemD by default.  You can set the hostname of any system with the hostname command.  This will not be a permanent change, as the system will revert back to the old hostname after it reboots

Here are some ways to change the host name on a Red Hat 7, CentOS 7, or Fedora 21 system and have it survive a reboot.

1) hostnamectl command

You can use the hostnamectl command to change the pretty, transient and static hostnames (More on that later - or skip to bottom).  To change all the hostnames at one time issue the hostnamectl command with the set-hostname option like so:

hostnamectl set-hostname myhost
NOTE: Replace myhost with the host name of your choice.

To change only one of the three particular hostnames, use:

hostnamectl set-hostname myhost --option
NOTE: Replace option with the particular hostname you want to change.  Valid options are pretty, static or transient.

If setting the pretty hostname it is recommended to wrap it in quotes to avoid shell confusion with special characters, like so:

hostnamectl set-hostname "Savona's Laptop" --pretty
To clear a hostname use the same command but replace the hostname with two quotes like so:

hostnamectl set-hostname ""
You can use status to get current information from hostnamectl like so:

hostnamectl status
Example output:

# hostnamectl status
Static hostname: putor
Pretty hostname: Savona's Laptop
Icon name: computer-laptop
Chassis: laptop
Machine ID: 9a544394179e8347be83cc734
Boot ID: 0d11dac360b213edbef30872321090123
Operating System: Fedora 21 (Twenty One)
CPE OS Name: cpe:/o:fedoraproject:fedora:21
Kernel: Linux 3.19.3-200.fc21.x86_64
Architecture: x86-64

2) Edit /etc/hostname directly

The hostnamectl will edit the /etc/hostname file if you set the static hostname.  You also have the option of editing /etc/hostname directly with vi or your editor of choice.  The file should contain one line with nothing but your hostname.

What the difference between pretty, static and transient hostnames you ask?

The hostnamectl command distinguishes three different hostnames.

Pretty: The "pretty" hostname is a high-level hostname which can include special characters (e.g. Savona's Laptop).

Static: This is the conventional hostname most server admins are used to. It is used to initialize the kernel hostname at boot.

Transient: Transient hostnames are those obtained by network configuration (e.g. DHCP). If a static hostname is set and valid, transient hostname is not used.

For information on how to change the hostname of a system running Red Hat 6 / CentOS 6 or earlier versions of Fedora, see this link:

No comments:

Post a Comment