一、 SUSE Linux 12

1. 当前生效

hostname host123

2. 永久生效

  • 设置$HOSTNAME变量
  • 修改/etc/hostname文件
  • 重启/etc/rc.d/boot.local服务
export HOSTNAME=host123
echo $HOSTNAME>/etc/hostname
/etc/rc.d/boot.local restart

启动系统时会调用/etc/rc.d/boot.local脚本,判断$HOSTNAME变量是否设置,若未设置则读取/etc/hostname设置主机名。

3. 配置/etc/host

vi /etc/hosts

192.168.2.5 host123

完成,如果有停机时间可以重启下验证

二、 centos 6

1. 当前生效

hostname myhost
# 设置立即生效
exec bash

2. 永久生效

  • 修改配置文件
vi /etc/sysconfig/network

NETWORKING=yes
HOSTNAME=myhost
  • 修改/etc/hosts
vi /etc/hosts

192.168.12.12   myhost

 三、 centos 7

1. 当前生效

hostname linux01
# 设置立即生效
exec bash

2. 永久生效

hostnamectl set-hostname linux01
  • 修改/etc/hosts
vi /etc/hosts

192.168.0.108   linux01