CentOS7 更改 hostname

随着 docker 的普及, CentOS7 逐渐取代 Centos6 成为线上主力系统,但问题随之而来。与 python2 升级 python3 类似, CentOS7 中对很多配置和命令都进行了更新,使得很多从6过来的人无法适从。下面就来说说我升级 CentOS7 有遇到的第一个问题,如何更改 hostname 。

按 CentOS6 的思路,修改 hostname ,无非是修改 /etc/sysconfig/network 文件,加入 HOSTNAME=DM 即可,但在 CentOS7 ,修改这个文件是无效的。

CentOS7 修改 Transient hostname 的方式和6无异,都是通过 hostname DM 命令来进行 Transient hostname 的设置,重启后失效。

CentOS7 中 Static hostname 的修改主要通过两种方式,第一种是通过修改 /etc/hostname 文件,直接编辑修改成自己想要的主机名(比如 DM )即可,第二种方式是通过 hostnamectl 这个工具。

先看看 hostnamectl 的默认参数显示:

[root@CEN72 ~]# hostnamectl
   Static hostname: CEN72
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 868a8a7d5ebd46729bbdf044ece9203f
           Boot ID: 0cda0795bd9d4023a23871eea11ab11d
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.36.1.el7.x86_64
      Architecture: x86-64
[root@CEN72 ~]# hostnamectl
   Static hostname: CEN72
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 868a8a7d5ebd46729bbdf044ece9203f
           Boot ID: 0cda0795bd9d4023a23871eea11ab11d
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.36.1.el7.x86_64
      Architecture: x86-64

再看看 hostnamectl 的帮助信息:

[root@CEN72 ~]# hostnamectl --help
hostnamectl [OPTIONS...] COMMAND ...

Query or change system hostname.

  -h --help              Show this help
     --version           Show package version
     --no-ask-password   Do not prompt for password
  -H --host=[USER@]HOST  Operate on remote host
  -M --machine=CONTAINER Operate on local container
     --transient         Only set transient hostname
     --static            Only set static hostname
     --pretty            Only set pretty hostname

Commands:
  status                 Show current hostname settings
  set-hostname NAME      Set system hostname
  set-icon-name NAME     Set icon name for host
  set-chassis NAME       Set chassis type for host
  set-deployment NAME    Set deployment environment for host
  set-location NAME      Set location for host

由上面可以看出,用 hostnamectl 修改 hostname 主要有以下方式:

修改 Static hostname :

[root@CEN72 ~]# 
[root@CEN72 ~]# hostnamectl
   Static hostname: DM
   Pretty hostname: CEN72
Transient hostname: CEN72
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 868a8a7d5ebd46729bbdf044ece9203f
           Boot ID: 0cda0795bd9d4023a23871eea11ab11d
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.36.1.el7.x86_64
      Architecture: x86-64

修改 Pretty hostname

[root@CEN72 ~]# hostnamectl --pretty set-hostname DM      
[root@CEN72 ~]# 
[root@CEN72 ~]# hostnamectl
   Static hostname: DM
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 868a8a7d5ebd46729bbdf044ece9203f
           Boot ID: 0cda0795bd9d4023a23871eea11ab11d
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.36.1.el7.x86_64
      Architecture: x86-64

修改 Transient hostname

[root@CEN72 ~]# hostnamectl --transient set-hostname DM
[root@CEN72 ~]# 
[root@CEN72 ~]# hostname
DM

注意:使用默认参数 hostnamectl set-hostname DM 修改,会把 Pretty hostname 修改成 DM ,而 Static hostname 则被修改成 dm

[root@dm ~]# hostnamectl
   Static hostname: dm
   Pretty hostname: DM
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 868a8a7d5ebd46729bbdf044ece9203f
           Boot ID: 0cda0795bd9d4023a23871eea11ab11d
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.36.1.el7.x86_64
      Architecture: x86-64
消息盒子

# 暂无消息 #

只显示最新10条未读和已读信息