Setting hostname in CentOS 7 and RHEL 7

Setting the hostname in CentOS 6 used to done with:


This would only set the hostname until server restart though. To get the hostname to stick, the file: /etc/sysconfig/network would need to be edited. The line HOSTNAME= needs to be updated from (localhost.localdomain is the default value):



In CentOS 7 everything is done with the command hostnamectl.

For a temporary setting of the hostname

hostnamectl set-hostname

And to make it stick, use the flag --static:

hostnamectl set-hostname --static