CentOS7主机名修改

1.命令行更改主机名,重启终端即生效。

#hostnamectl --static set-hostname k8s-master

 2.通过脚本进行主机名修改

# cat config-hostname.sh 
#!/bin/bash

# get ip address
ip=$(ip a show dev ens32 | grep -w inet | awk '{print $2}' | cut -d '/' -f1)

# get hostname from hosts files
hostname=$(grep "${ip}" /etc/hosts | awk '{print $2}')

# modify hostname
hostname ${hostname}
sed -i "s/HOSTNAME=.*$/HOSTNAME=${hostname}/g" /etc/sysconfig/network