0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LXCコンテナでホスト名が変わらないときの対処方法

Last updated at Posted at 2018-11-02

はじめに

LXD/LXCで管理しているコンテナのホスト名は lxc move や lxc copy や lxc rename で変更することができる。
しかし、まれにホスト名が変わらない時があったので、その時の対応をメモしておきます。

環境

  • LXDホスト

  • OS : Ubuntu 18.04.1 LTS

  • lxc : 3.0.1

  • lxd : 3.0.1

  • LXCコンテナ

  • OS : CentOS 7.5.1804

やること

「c1-centos7」を「centos7-sv01」に変更する。

コンテナの状態を確認

変更前の「c1-centos7」が稼働中です。

user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc list
+------------+---------+---------------------+------+------------+-----------+
|    NAME    |  STATE  |        IPV4         | IPV6 |    TYPE    | SNAPSHOTS |
+------------+---------+---------------------+------+------------+-----------+
| c1-centos7 | RUNNING | 172.31.0.233 (eth0) |      | PERSISTENT | 0         |
+------------+---------+---------------------+------+------------+-----------+
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc exec centos7-sv01 -- cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc exec centos7-sv01 -- hostname
c1-centos7
user01@ubuntu1804:~$
user01@ubuntu1804:~$

名前を変更するときはコンテナを停止させる

コンテナが動いていると名前が変更できないので、一旦停止させる

user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc rename c1-centos7 centos7-sv01
Error: Renaming of running container not allowed
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc stop c1-centos7
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc list
+------------+---------+------+------+------------+-----------+
|    NAME    |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+------------+---------+------+------+------------+-----------+
| c1-centos7 | STOPPED |      |      | PERSISTENT | 0         |
+------------+---------+------+------+------------+-----------+
user01@ubuntu1804:~$
user01@ubuntu1804:~$

LXCで管理するコンテナ名を変更する

ここでは lxc rename で変更することにする。lxc move でも結果は同じ。
ちなみに lxc move は異なるLXDホスト間でコンテナを移動させるコマンドです。

※lxc rename コマンドでコンテナ名を変更
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc rename
説明:
  コンテナまたはコンテナのスナップショットの名前を変更します

Usage:
  lxc rename [<remote>:]<container>[/<snapshot>] <container>[/<snapshot>] [flags]

Global Flags:
      --debug         デバッグメッセージをすべて表示します
      --force-local   強制的にローカルのUNIXソケットを使います
  -h, --help          ヘルプを表示します
  -v, --verbose       詳細な情報を出力します
      --version       バージョン番号を表示します
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc rename c1-centos7 centos7-sv01
user01@ubuntu1804:~$

※コンテナ名が変わった
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc list
+--------------+---------+------+------+------------+-----------+
|     NAME     |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+--------------+---------+------+------+------------+-----------+
| centos7-sv01 | STOPPED |      |      | PERSISTENT | 0         |
+--------------+---------+------+------+------------+-----------+
user01@ubuntu1804:~$

※コンテナを起動
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc start centos7-sv01
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc list
+------------+---------+---------------------+------+------------+-----------+
|    NAME    |  STATE  |        IPV4         | IPV6 |    TYPE    | SNAPSHOTS |
+------------+---------+---------------------+------+------------+-----------+
| c1-centos7 | RUNNING | 172.31.0.233 (eth0) |      | PERSISTENT | 0         |
+------------+---------+---------------------+------+------------+-----------+
user01@ubuntu1804:~$

※コンテナ自体のホスト名を確認してみると、変更されてない...
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc exec centos7-sv01 -- hostname
c1-centos7 ← ★
user01@ubuntu1804:~$
user01@ubuntu1804:~$

原因

コンテナ自体のホスト名の設定ファイル /etc/hosts と /etc/hostname が変更されてないから。

user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc exec centos7-sv01 -- cat /etc/hosts
127.0.0.1   localhost
127.0.1.1   c1-centos7 ← ★

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc exec centos7-sv01 -- cat /etc/hostname
c1-centos7 ← ★
user01@ubuntu1804:~$
user01@ubuntu1804:~$

対策

コンテナ自体の /etc/hosts と /etc/hostname を変更する

※sedコマンドで /etc/hosts を変更する
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc exec centos7-sv01 -- sed -i 's/c1-centos7/centos7-sv01/' /etc/hosts
user01@ubuntu1804:~$ lxc exec centos7-sv01 -- cat /etc/hosts
127.0.0.1   localhost
127.0.1.1   centos7-sv01 ← ★

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
user01@ubuntu1804:~$


※sedコマンドで /etc/hostname を変更する
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc exec centos7-sv01 -- sed -i 's/c1-centos7/centos7-sv01/' /etc/hostname
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc exec centos7-sv01 -- cat /etc/hostname
centos7-sv01 ← ★
user01@ubuntu1804:~$


※コンテナを再起動
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc restart centos7-sv01
user01@ubuntu1804:~$

※コンテナのホスト名を確認、ホスト名が変更されました
user01@ubuntu1804:~$
user01@ubuntu1804:~$ lxc exec centos7-sv01 -- hostname
centos7-sv01 ← ★
user01@ubuntu1804:~$
user01@ubuntu1804:~$

感想

同じCentOS7のコンテナでもホスト名が変わるときと、変わらないときがある。
この違いについては時間がないので調べていません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?