CentOS修改主机名

在做多个主机的测试时,可以通过主机名更加直观的分辨出不同的主机,这里记录下修改主机名的方式及测试过程。

  • 查看主机名

方式一
echo @HOSTNAME 或者hostname

方式二
hostnamectl

[root@localhost ~]# hostnamectl
   Static hostname: localhost.localdomain
Transient hostname: localhost
         Icon name: computer-vm
           Chassis: vm
        Machine ID: f5b4422ae39d4e5ab2e0fc32018cc6fe
           Boot ID: f44ad9cf36b84812b4ab0956d1a3216d
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.90.1.el7.x86_64
      Architecture: x86-64
[root@localhost ~]# 

其中:
Static hostname,静态主机名
Transient hostname,临时主机名

查看静态主机名用hostnamectl,查看临时主机名用hostname

  • 修改主机名

修改主机名需要两步:

  1. 修改主机名
  2. 修改下/etc/hosts中的对应关系

特别说明下:
修改主机名后,如果不修改/etc/hosts中的对应关系,从本地ping 主机名的时候会ping主机的外网地址,修改后,ping的是127.0.0.1.解析的过程会快一些

修改前:

[root@combo73 ~]# ping combo73
PING combo73 (192.168.0.73) 56(84) bytes of data.
64 bytes from localhost (192.168.0.73): icmp_seq=1 ttl=64 time=0.041 ms
64 bytes from localhost (192.168.0.73): icmp_seq=2 ttl=64 time=0.043 ms
64 bytes from localhost (192.168.0.73): icmp_seq=3 ttl=64 time=0.062 ms
64 bytes from localhost (192.168.0.73): icmp_seq=4 ttl=64 time=0.061 ms
64 bytes from localhost (192.168.0.73): icmp_seq=5 ttl=64 time=0.056 ms
64 bytes from localhost (192.168.0.73): icmp_seq=6 ttl=64 time=0.064 ms
64 bytes from localhost (192.168.0.73): icmp_seq=7 ttl=64 time=0.058 ms
64 bytes from localhost (192.168.0.73): icmp_seq=8 ttl=64 time=0.058 ms
64 bytes from localhost (192.168.0.73): icmp_seq=9 ttl=64 time=0.627 ms
64 bytes from localhost (192.168.0.73): icmp_seq=10 ttl=64 time=0.058 ms
64 bytes from localhost (192.168.0.73): icmp_seq=11 ttl=64 time=0.094 ms
64 bytes from localhost (192.168.0.73): icmp_seq=12 ttl=64 time=0.064 ms
64 bytes from localhost (192.168.0.73): icmp_seq=13 ttl=64 time=0.056 ms
64 bytes from localhost (192.168.0.73): icmp_seq=14 ttl=64 time=0.062 ms
64 bytes from localhost (192.168.0.73): icmp_seq=15 ttl=64 time=0.057 ms
64 bytes from localhost (192.168.0.73): icmp_seq=16 ttl=64 time=0.054 ms
64 bytes from localhost (192.168.0.73): icmp_seq=17 ttl=64 time=0.087 ms
64 bytes from localhost (192.168.0.73): icmp_seq=18 ttl=64 time=0.071 ms
64 bytes from localhost (192.168.0.73): icmp_seq=19 ttl=64 time=0.068 ms
64 bytes from localhost (192.168.0.73): icmp_seq=20 ttl=64 time=0.066 ms
64 bytes from localhost (192.168.0.73): icmp_seq=21 ttl=64 time=0.053 ms
^C
--- combo73 ping statistics ---
21 packets transmitted, 21 received, 0% packet loss, time 24161ms
rtt min/avg/max/mdev = 0.041/0.088/0.627/0.121 ms

修改后:

[root@combo73 ~]# ping combo73
PING combo73 (127.0.0.1) 56(84) bytes of data.
64 bytes from combo73 (127.0.0.1): icmp_seq=1 ttl=64 time=0.029 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=2 ttl=64 time=0.058 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=3 ttl=64 time=0.059 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=4 ttl=64 time=0.046 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=5 ttl=64 time=0.057 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=6 ttl=64 time=0.053 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=7 ttl=64 time=0.053 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=8 ttl=64 time=0.054 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=9 ttl=64 time=0.053 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=10 ttl=64 time=0.046 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=11 ttl=64 time=0.045 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=12 ttl=64 time=0.055 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=13 ttl=64 time=0.054 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=14 ttl=64 time=0.062 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=15 ttl=64 time=0.148 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=16 ttl=64 time=0.065 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=17 ttl=64 time=0.057 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=18 ttl=64 time=0.054 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=19 ttl=64 time=0.058 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=20 ttl=64 time=0.064 ms
64 bytes from combo73 (127.0.0.1): icmp_seq=21 ttl=64 time=0.104 ms
^C
--- combo73 ping statistics ---
21 packets transmitted, 21 received, 0% packet loss, time 20030ms
rtt min/avg/max/mdev = 0.029/0.060/0.148/0.025 ms
hostnamectl  set-hostname 新主机名

hostnamectl set-hostname combo73

[root@combo73 ~]# hostnamectl
   Static hostname: combo73
         Icon name: computer-vm
           Chassis: vm
        Machine ID: f5b4422ae39d4e5ab2e0fc32018cc6fe
           Boot ID: f44ad9cf36b84812b4ab0956d1a3216d
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.90.1.el7.x86_64
      Architecture: x86-64

修改主机名后,可以看到临时主机名消失了

重新登录下,环境变量中的主机名和命令提示符中的主机名也改过来了
[root@combo73 ~]# echo $HOSTNAME
combo73

改下/etc/hosts中的配置
修改前:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

修改为:

127.0.0.1   combo73
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         combo73
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丁达尔之光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值