Linux下如何修改主机名

临时方案

使用hostname命令

[root@Server-1 cer]# hostname xxxx

重启生效

使用hostnamectl命令

  • 查看当前hostname信息
[root@Server-1 cer]# hostnamectl
   Static hostname: Server-1
Transient hostname: xxxx
         Icon name: computer-vm
           Chassis: vm
        Machine ID: b00cb986ec8e4fe4967c169f60547bb7
           Boot ID: fe66b35c25b44fbc8973e48f0ac8f735
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.45.1.el7.x86_64
      Architecture: x86-64
[root@Server-1 cer]# 
  • 设置hostname,set-hostname
[root@Server-1 cer]# hostnamectl set-hostname Server-1
[root@Server-1 cer]# 

修改hosts,同步本地解析

[root@Server-1 cer]# vim /etc/hosts
127.0.0.1 Server-1 Server-1
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4

::1 Server-1 Server-1
::1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6

Leave a Reply