CentOS下如何修改主機名

1、臨時生效,重啓服務器後就會失效
mysql

命令語法:hostname  新主機名sql

提示:用hostname命令修改主機名後,須要退出當前用戶從新登陸後才能夠生效。vim

例1:bash

[root@localhost /]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@localhost /]# hostname
localhost
[root@localhost /]# hostname mysql-master
[root@localhost /]# logout
[oldboy@localhost /]$ su - root
Password: 
Last login: Fri Mar 29 02:11:30 CST 2019 on pts/0
[root@mysql-master ~]# hostname
mysql-master

2、永久性生效服務器

永久性修改主機名,會由於系統版本的不一樣 而修改的方法也不一樣,下面以CentOS 6和CentOS 7這兩個版本爲案例來說解。ide

1)CentOS 6 系統版本spa

永久性修改主機名須要編輯配置文件,配置文件路徑以下:
it

/etc/sysconfig/network

例2:
ast

[root@localhost /]# cat /etc/redhat-release 
CentOS release 6.5 (Final)
[root@localhost /]# hostname
localhost
[root@localhost /]# vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=mysql-slave01
#重啓服務器
[root@localhost /]# reboot
[root@mysql-slave01 ~]# cd /
[root@mysql-slave01 /]# hostname
mysql-slave01

2)CentOS 7 系統版本class

CentOS 7 的系統版本再也不是修改配置文件了,而是直接用命令就能夠永久性的修改主機名。不過仍然須要重啓服務器後才能夠生效

命令語法:hostnamectl set-hostname 新主機名

[root@localhost /]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@localhost /]# hostname
localhost
[root@localhost /]# hostnamectl set-hostname mysql-master
#重啓服務器
[root@localhost /]# reboot
[root@mysql-master ~]# cd /
[root@mysql-master /]# hostname
mysql-master

提示:

永久性修改主機名,CentOS 6系統版本是修改編輯配置文件而CentOS 7系統版本能夠用命令修改,不過兩個都須要重啓服務器後才能夠生效。

相關文章
相關標籤/搜索