查看本机用户名
[root@testdb78 ~]# hostnamectl
Static hostname: testdb78
Icon name: computer-vm
Chassis: vm
Machine ID: 18e675bf92904ea599d57e5f070c4a98
Boot ID: 396dd89d102247fc86d52bca354df33d
Virtualization: kvm
Operating System: Red Hat Enterprise Linux Server 7.6 (Maipo)
CPE OS Name: cpe:/o:redhat:enterprise_linux:7.6:GA:server
Kernel: Linux 3.10.0-957.el7.x86_64
Architecture: x86-64
redhat7和Centos7以下的系统直接输入hostname命名查看
方法一:直接修改文件hostname
[root@testdb78 ~]# vi /etc/hostname
注意该文件的第一行是生效的,其他行都是不生效的,一定要把改的主机名写在第一行。改完后,重启服务器生效。方法二:使用hostnamectl命令修改
命令为:sudo hostnamectl set-hostname <newhostname>如:sudo hostnamectl set-hostname test_hostname
这条命令本质上和第一种方法是一样,会删除/etc/hostname文件中的主机名,然后替换为新的主机名。
方法三:临时修改实时生效,可以配合上面两种方法使用
hostname <new-hostname>临时生效,重启后会恢复,可以和上面两种方法配合使用,既可以直接生效,重启也不会改变。