Linux修改主机名

1.查看当前的主机名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost datas]# hostname
localhost
[root@localhost datas]# hostnamectl
   Static hostname: localhost.localdomain
Transient hostname: localhost
         Icon name: computer-vm
           Chassis: vm
        Machine ID: f1d9ecb6c6bd4e02b58e19bba402056c
           Boot ID: 91603e0798814b54a928e70fd2a2754f
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-693.el7.x86_64
      Architecture: x86-64

2.临时修改主机名

1
2
3
4
5
[root@localhost datas]# hostname yin 临时修改,重启服务器后就不生效了
[root@localhost datas]# hostname
yin
[root@localhost datas]# hostname -i 查看本机器名对应的ip地址
fe80::20c:29ff:fe5f:6d17%ens33 192.168.75.205

3.永久修改主机名

需要修改以下两个配置文件

1
2
3
4
5
6
1、方法一使用hostnamectl命令
[root@xlucas1 ~]# hostnamectl set-hostname xlucas2
 
2、方法二:修改配置文件 /etc/hostname 保存退出
[root@xlucas1 ~]# vi /etc/hostname
xlucas2
原文地址:https://www.cnblogs.com/lzkalislw/p/12896561.html