スクリプトを使って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をすべて消去することができるようです。

ベストアンサー1

おすすめ記事