How to set the hostname on CentOS 7

CentOS 7 has a nice command called hostnamectl. With it we can display the current hostname, and set any of the three types of hostname:

  • static hostname (something like example.com)
  • transient hostname (anything you like, assigned when using DHCP)
  • pretty hostname (something like Jay’s MacBook Pro)

By default, a CentOS installation comes back with localhost.localdomain – but that’s not meaningful if you see lots of localhosts on the same network.

If the IP of the box does not change, we can set the static hostname like this:

No feedback means good news. Likewise, we can set a hostname if were using DHCP to get an IP address, even though it may change every time we connect. To make sure we retain the same name no matter what IP we get, let’s set the transient hostname like so:

Note that we can’t use spaces or special characters with static or transient hostnames as far as I know.

Lucky for us there’s also the pretty hostname, which does support special characters. It doesn’t usually appear anywhere on the command line, but GUIs like to display the pretty name of a machine when available:

There’s no need to restart anything, the changes are in effect as soon as we hit return after either command.

To see the current hostnames, we can use the status switch:

Thanks to Vivek Gite for this wonderful explanation!

  • http://www.cyberciti.biz/faq/rhel-redhat-centos-7-change-hostname-command/

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

Add your voice!