(一)CentOS6.5版本的修改主机名的方法

通过hostname 主机名  但已重启就会恢复到原先的。不过可以采用以下的进行更改既可。


1,通过修改文件/etc/sysconfig/network .  eg:

# cat /etc/sysconfig/network

NETWORKING=yes

HOSTNAME=Server34 ---->修改想要的名字


   2,修改文件/etc/hosts

127.0.0.1   localhost Server34 localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6


3,reboot 既可修改原主机名。


(二)CentOS7.2修改主机名

CentOS7.2修改主机名相对于6.5版本又有所改进,不需要重启就可以直接修改,具体的方法有如下三种:

(1)修改配置文件/etc/hostname来进行修改

[root@localhost ~]# vim /etc/hostname 
server23
[root@localhost ~]# systemctl restart systemd-hostnamed.service
[root@localhost ~]# hostname
server23

(2)通过nmtui修改主机名

[root@localhost ~]# nmtui                                                                                                                                                                                                    
                                                                                                                            
                                                                                                                            
                                    ┌─────────────────┤ Set Hostname ├──────────────────┐                                   
                                    │                                                   │                                   
                                    │ Hostname Server23________________________________ │                                   
                                    │                                                   │                                   
                                    │                                     <Cancel> <OK> │                                   
                                    │                                                   │                                   
                                    └───────────────────────────────────────────────────┘
                                                                                                                                                                
[root@localhost ~]# hostname
Server23

 (3) hostnamectl的命令行工具

           
[root@localhost ~]# hostnamectl -help
hostnamectl [OPTIONS...] COMMAND ...
Query or change system hostname.
  -h --help              Show this help
     --version           Show package version
     --no-ask-password   Do not prompt for password
  -H --host=[USER@]HOST  Operate on remote host
  -M --machine=CONTAINER Operate on local container
     --transient         Only set transient hostname
     --static            Only set static hostname
     --pretty            Only set pretty hostname
Commands:
  status                 Show current hostname settings
  set-hostname NAME      Set system hostname
  set-icon-name NAME     Set icon name for host
  set-chassis NAME       Set chassis type for host
  set-deployment NAME    Set deployment environment for host
  set-location NAME      Set location for host
[root@localhost ~]# hostnamectl status
   Static hostname: Server23
         Icon name: computer-vm
           Chassis: vm
        Machine ID: b832bc29266640fe83d7256d53840cdc
           Boot ID: 33590b06c20b45bab1e8e12fbf4986d2
    Virtualization: microsoft
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.el7.x86_64
      Architecture: x86-64
[root@localhost ~]# hostnamectl set-hostname servers23
[root@localhost ~]# hostname
servers23