Debian/Ubuntu でインストール後にホスト名を変更する方法

はじめに

VM をたくさん使っていると管理が大変になってくるので、ホスト名を変更する方法を調べました。

手順

/etc/hostname の変更
$ sudo vim /etc/hostname
ubuntu001

または

sudo hostname ubuntu001
/etc/hosts の変更
$ sudo vim /etc/hosts
127.0.0.1       localhost
127.0.1.1       ubuntu001.localdomain   ubuntu001 # この行を修正

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ネットワークの再起動
$ sudo /etc/init.d/networking restart

おまけ

/etc/hosts ファイルによくリモートログインするホスト名とIPアドレスを関連づけておけば、たとえば ssh ubuntu001 といった形でSSH接続することができる。

$ sudo vim /etc/hosts
172.16.116.XXX  ubuntu001.localdomain   ubuntu001

という感じで設定しておくと便利。