通过脚本重命名 Debian 主机:需要检查我的工作

通过脚本重命名 Debian 主机:需要检查我的工作

只是想确保我已经涵盖了所有基础内容(不包括域名)。据我所知,一切正常。

#!/bin/sh

# Needs 1 and only 1 argument
if [ "$#" -ne 1 ]; then
  echo "Usage: $0 NEW-HOSTNAME" >&2
  exit 1
fi

# I could probably put in a regex here to check that the hostname is good.

# Change the hostname in the session (does this even do anything?)
sudo hostname -b ${1}

# Change the visible hostname permanently (writes to /etc/hostname)
sudo hostnamectl set-hostname ${1}

# Change the mail hostname if there is one
if [ -f "/etc/mailname" ]; then
  echo ${1} | sudo tee /etc/mailname > /dev/null
fi

# Change the hostname in the hosts file (aka the fun one)
my_temp=`mktemp`
grep -v "127.0.1.1" < /etc/hosts > ${my_temp}
echo 127.0.1.1    ${1} >> ${my_temp}
sudo cp ${my_temp} /etc/hosts
rm -f ${my_temp}

# And now for a reboot
echo "Rebooting."
sudo shutdown -r now

我想如果我真的很神经质,我可以保存旧主机名并在重新启动后将其用作参考以清除任何剩余的 xauth cookie。

相关内容