想要快速更改您的服务器主机名,那就使用这个简单的脚本吧。
1.创建脚本文件
$ vim /tmp/hostname.sh
添加以下内容:
#!/usr/bin/env bash #!//renwole.com/archives/786 OLD_HOSTNAME="$( hostname )" NEW_HOSTNAME="$1" if [ -z "$NEW_HOSTNAME" ]; then echo -n "Please enter new hostname: " read NEW_HOSTNAME < /dev/tty fi if [ -z "$NEW_HOSTNAME" ]; then echo "Error: no hostname entered. Exiting." exit 1 fi echo "Changing hostname from $OLD_HOSTNAME to $NEW_HOSTNAME..." hostname "$NEW_HOSTNAME" sed -i "s/HOSTNAME=.*/HOSTNAME=$NEW_HOSTNAME/g" /etc/sysconfig/network if [ -n "$( grep "$OLD_HOSTNAME" /etc/hosts )" ]; then sed -i "s/$OLD_HOSTNAME/$NEW_HOSTNAME/g" /etc/hosts else echo -e "$( hostname -I | awk '{ print $1 }' )\t$NEW_HOSTNAME" >> /etc/hosts fi echo "Done."
2.赋予可执行权限并运行该脚本
$ cd /tmp/ $ chmod 755 hostname.sh $ /tmp/hostname.sh Please enter new hostname:renwole Changing hostname from renwolecom to renwole... Done.
修改完成。
3.查看核实是否成功修改
[root@1renwolecom ~]# hostname renwole
已经生效。