当LXC容器的主机名没有改变时,可以采取以下对策
首先在使用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:~$
应对措施 dui cuò shī)以母语中文重述如下:
修改容器本身的 /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容器的情况下,有时候主机名会更改,有时候则不会更改。
由于时间紧迫,我还没有对这个差异进行调查。
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:~$
应对措施 dui cuò shī)以母语中文重述如下:
修改容器本身的 /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容器的情况下,有时候主机名会更改,有时候则不会更改。
由于时间紧迫,我还没有对这个差异进行调查。
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 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:~$
应对措施 dui cuò shī)以母语中文重述如下:
修改容器本身的 /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容器的情况下,有时候主机名会更改,有时候则不会更改。
由于时间紧迫,我还没有对这个差异进行调查。
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:~$