Change HostName in CentOS 7

CentOS6 系列更改主机名是通过修改 /etc/sysconfig/network
CentOS7 跟以前有所不同,新加了一个 hostnamectl命令,专门来修改主机名。

$ sudo hostnamectl set-hostname <host-name>

$ hostnamectl status

[wangzezhi@bj-cloud-01 ~]$ hostnamectl status
   Static hostname: bj-cloud-01
         Icon name: computer-server
           Chassis: server
        Machine ID: 2b933b2034de465aaa68cdb89504d95c
           Boot ID: afc77526d1454e16ab719463d1f1b3ce
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-229.el7.x86_64
      Architecture: x86_64

相关Fabric脚本

def set_hostname(host_name):  
    """ Set the Hostname """
    sed("/etc/sysconfig/network", "^HOSTNAME=.*$", "HOSTNAME=%s" %(host_name,), use_sudo=True)
    run("sudo hostname %s"  %(host_name,),)
    append('/etc/hosts', "127.0.1.1   %s" % host_name, use_sudo=True)
def hostname_ctl(host_name):  
    run('sudo hostnamectl set-hostname %s' %host_name)
    run('sudo hostnamectl status')