基于systemd系统LXC学习

来源:互联网 发布:克克mp3录音软件 编辑:程序博客网 时间:2024/05/16 19:16

本文记录的问题是:基于systemd系统的Linux发行版本运行在LXC环境下,该LXC的主机名不能由LXC的配置文件中lxc.utsname决定。下面记录下解决过程中发现的问题。

systemd中hostnamectl

通过man systemd-hostnamed.service发现如下解释

SYNOPSIS       systemd-hostnamed.service       /usr/lib/systemd/systemd-hostnamedDESCRIPTION       systemd-hostnamed is a system service that may be used as a mechanism to change the system's hostname.       systemd-hostnamed is automatically activated on request and terminates itself when it is unused.

大概意思是说systemd-hostnamed这个二进制文件是有请求或者结束过程由自动激活的。通过查看lxc的进程发现,在大多数情况下是不存在systemd-hostname这个进程的,只有当执行hostnamectl才会出现该进程,这也正验证了man手册中关于其的说明,执行hostnamectl之前

[root@lxc ~]# ps -efUID         PID   PPID  C STIME TTY          TIME CMDroot          1      0  0 07:12 ?        00:00:00 /sbin/initroot         22      1  0 07:12 ?        00:00:00 /usr/lib/systemd/systemd-journaldroot         26      1  0 07:12 ?        00:00:00 /usr/lib/systemd/systemd-udevdroot        338      1  0 07:12 ?        00:00:00 /usr/lib/systemd/systemd-logindroot        341      1  0 07:12 ?        00:00:00 /usr/sbin/sshd -Ddbus        343      1  0 07:12 ?        00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --sroot        353      1  0 07:12 ?        00:00:00 /usr/sbin/crond -nroot        362      1  0 07:12 ?        00:00:00 login -- rootroot        523      1  0 07:12 ?        00:00:00 /sbin/dhclient -H lxc -q -lf /var/lib/dhclient/dhclient--eth0.lease -pf /root        583      1  0 07:12 ?        00:00:00 /usr/lib/systemd/systemd --userroot        584    583  0 07:12 ?        00:00:00 (sd-pam)root        587    362  0 07:12 console  00:00:00 -bashroot        649    587  0 07:37 console  00:00:00 ps -ef

执行hostnamectl之后

[root@lxc ~]# ps -efUID         PID   PPID  C STIME TTY          TIME CMDroot          1      0  0 07:12 ?        00:00:00 /sbin/initroot         22      1  0 07:12 ?        00:00:00 /usr/lib/systemd/systemd-journaldroot         26      1  0 07:12 ?        00:00:00 /usr/lib/systemd/systemd-udevdroot        338      1  0 07:12 ?        00:00:00 /usr/lib/systemd/systemd-logindroot        341      1  0 07:12 ?        00:00:00 /usr/sbin/sshd -Ddbus        343      1  0 07:12 ?        00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --sroot        353      1  0 07:12 ?        00:00:00 /usr/sbin/crond -nroot        362      1  0 07:12 ?        00:00:00 login -- rootroot        523      1  0 07:12 ?        00:00:00 /sbin/dhclient -H lxc -q -lf /var/lib/dhclient/dhclient--eth0.lease -pf /root        583      1  0 07:12 ?        00:00:00 /usr/lib/systemd/systemd --userroot        584    583  0 07:12 ?        00:00:00 (sd-pam)root        587    362  0 07:12 console  00:00:00 -bashroot        651      1  0 07:39 ?        00:00:00 /usr/lib/systemd/systemd-hostnamedroot        653    587  0 07:39 console  00:00:00 ps -ef

使LXC使用utsname配置的方法

执行命令

hostnamectl set-hostname ""

此时/etc/hostname文件已被系统删除,系统默认主机名是localhost。重启LXC,主机名已经更改为utsname的配置,同时查看hostnamectl

[root@lxc ~]# hostnamectl   Static hostname: n/aTransient hostname: lxc.test.com         Icon name: computer-container           Chassis: container        Machine ID: c849431f196c4907b7eb56fdff80edff           Boot ID: 2eb5290a9f3440b6b8fbf39a6650e608    Virtualization: lxc  Operating System: Fedora 23 (Cloud Edition)       CPE OS Name: cpe:/o:fedoraproject:fedora:23            Kernel: Linux 3.19.0-25-generic      Architecture: x86-64

Static hostname是指/etc/hostname的内容

1 0