本篇介绍系统容器后台运行时,在没有SSH情况下如何本地登录进入容器
环境: 1.宿主 debian 13 lxc 6.0 shell提示符: root@debian:~# 和 linlin@debian:~$
2.容器 debian 12 shell提示符: root@vmcln:~#
一.宿主控制台 1.开机 1)进入桌面环境,打开第一个图形界面程序lxterminal模拟终端
2 )
linlin@debian:~$ tty
/dev/pts/0
linlin@debian:~$
可见当前模拟终端产生了伪终端pts/0,本文将该终端记作[pts/0],下同
3 )
linlin@debian:~$ ps -ef
UID PID PPID C STIME TTY TIME CMD
...
root 755 1 0 08:44 ? 00:00:00 /usr/lib/systemd/systemd-logind <= 应是多座位(seat)的管理,不是控制台的登录程序login
...
root 1456 1196 0 08:46 tty7 00:00:01 /usr/lib/xorg/Xorg :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
root 1461 1 0 08:46 tty1 00:00:00 /sbin/agetty -o -- \u --noreset --noclear - linux
就只tty1、tty7两个虚拟控制台
4)设备文件
linlin@debian:~$ ls -l /dev/console
crw------- 1 root root 5, 1 4月18日 08:46 /dev/console <= System console,其时间同agetty
linlin@debian:~$ ls -l /dev/pts/0
crw--w---- 1 linlin tty 136, 0 4月18日 08:55 /dev/pts/0 <= First Unix98 pseudo-TTY
linlin@debian:~$ ls -l /dev/tty*
crw-rw-rw- 1 root tty 5, 0 4月18日 08:44 /dev/tty <= Current TTY device
crw------- 1 root tty 4, 0 4月18日 08:44 /dev/tty0 <= Current virtual console
crw------- 1 root tty 4, 1 4月18日 08:47 /dev/tty1 <= 其时间慢于agetty
crw------- 1 root tty 4, 10 4月18日 08:44 /dev/tty10
...
crw------- 1 root tty 4, 19 4月18日 08:44 /dev/tty19
crw------- 1 root tty 4, 2 4月18日 08:44 /dev/tty2
...
crw------- 1 root tty 4, 63 4月18日 08:44 /dev/tty63
crw------- 1 root tty 4, 7 4月18日 08:47 /dev/tty7 <= 其时间慢于Xorg
crw------- 1 root tty 4, 8 4月18日 08:44 /dev/tty8
crw------- 1 root tty 4, 9 4月18日 08:44 /dev/tty9
crw-rw---- 1 root dialout 4, 64 4月18日 08:44 /dev/ttyS0
crw-rw---- 1 root dialout 4, 65 4月18日 08:44 /dev/ttyS1
crw-rw---- 1 root dialout 4, 66 4月18日 08:44 /dev/ttyS2
crw-rw---- 1 root dialout 4, 67 4月18日 08:44 /dev/ttyS3
linlin@debian:~$
命令ls -l显示的时间是修改时间,可用命令stat查看/dev/console及各tty创建时间都是08:44(早于Xorg) 如上/dev/tty7的主设备号是4,次设备号是7
2.切换控制台 1)切换到tty2 按<ctrl><alt><F2>,并立即切回tty7(X11)
linlin@debian:~$ ps -ef | grep tty
root 1456 1196 0 08:46 tty7 00:00:03 /usr/lib/xorg/Xorg :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
root 1461 1 0 08:46 tty1 00:00:00 /sbin/agetty -o -- \u --noreset --noclear - linux
root 1972 1 0 09:03 tty2 00:00:00 /sbin/agetty -o -- \u --noreset --noclear - linux
linlin@debian:~$
可见产生了tty2
2)启动服务
linlin@debian:~$ cat /usr/lib/systemd/system/getty-static.service
...
ExecStart=systemctl --no-block start getty@tty2.service getty@tty3.service getty@tty4.service getty@tty5.service getty@tty6.service
...
linlin@debian:~$
参考上面命令
root@debian:~# systemctl --no-block start getty@tty4.service
linlin@debian:~$ ps -ef | grep tty
root 1456 1196 0 08:46 tty7 00:00:03 /usr/lib/xorg/Xorg :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
root 1461 1 0 08:46 tty1 00:00:00 /sbin/agetty -o -- \u --noreset --noclear - linux
root 1972 1 0 09:03 tty2 00:00:00 /sbin/agetty -o -- \u --noreset --noclear - linux
root 2098 1 0 09:14 tty4 00:00:00 /sbin/agetty -o -- \u --noreset --noclear - linux
linlin@debian:~$
可见产生了tty4,并没按<ctrl><alt><F4>
linlin@debian:~$ ls -l /etc/systemd/system/getty.target.wants
总计 0
lrwxrwxrwx 1 root root 34 2018年11月27日 getty@tty1.service -> /lib/systemd/system/getty@.service
linlin@debian:~$
可见只一个文件
linlin@debian:~$ cat /lib/systemd/system/getty@.service
...
ExecStart=-/sbin/agetty -o '-- \\u' --noreset --noclear - ${TERM}
...
3)登录进程 3.1)切换到tty2
login:linlin <= 输入用户名并回车
password: <= 不输入口令,不回车
3.2)切换到tty7
linlin@debian:~$ ps -ef | grep tty
root 1456 1196 0 08:46 tty7 00:00:03 /usr/lib/xorg/Xorg :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
root 1461 1 0 08:46 tty1 00:00:00 /sbin/agetty -o -- \u --noreset --noclear - linux
root 1972 1 0 09:03 tty2 00:00:00 /bin/login --
root 2098 1 0 09:14 tty4 00:00:00 /sbin/agetty -o -- \u --noreset --noclear - linux
linlin@debian:~$
可见login同原来tty2/agetty的进程号,或许是输入用户后才产生login进程
隔了60秒后,login进程结束,重新产生新的tty2/agetty进程
linlin@debian:~$ ps -ef | grep tty
root 1456 1196 0 08:46 tty7 00:00:05 /usr/lib/xorg/Xorg :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
root 1461 1 0 08:46 tty1 00:00:00 /sbin/agetty -o -- \u --noreset --noclear - linux
root 2098 1 0 09:14 tty4 00:00:00 /sbin/agetty -o -- \u --noreset --noclear - linux
root 2115 1 0 09:22 tty2 00:00:00 /sbin/agetty -o -- \u --noreset --noclear - linux
linlin@debian:~$
3.3)切换到tty2 login:
因为新的tty2,提示符重回到待输入用户名状态
登录后
linlin@debian:~$ tty
/dev/tty2
linlin@debian:~$
3.4)查看相关源码 [login: ]提示符是agetty进程输出 [password:]提示符是login 进程输出
应该是agetty进程将用户名传给login进程 https://sources.debian.org/src/util-linux/2.41-4/term-utils/agetty.c/
...
login_argv[login_argc++] = username;
...
execv(options.login, login_argv); // login program覆盖掉agetty
...
查看很早期版本login https://sources.debian.org/src/util-linux/2.7.1-3/login-utils/login.c/
...
#ifndef USE_PAM
execvp(childArgv[0], childArgv + 1); // 不带PAM编译,shell进程覆盖掉login进程,登录后不见login
error = 1;
#else /* USE_PAM */
oldSigHandler = signal(SIGINT, SIG_IGN);
childPid = fork(); // 带PAM编译,创建子进程
if (childPid < 0) {
...
} else if (childPid) {
/* parent */
wait(&childStatus); // login父进程
...
} else {
/* child */
execvp(childArgv[0], childArgv + 1); // shell子进程
exit(1);
}
#endif /* USE_PAM */
...
3.5)登录失败的情景 输入错误口令,login程序在控制台输出如下:
Login incorrect
login: <= 此时仍是login进程
切回tty7查看,login进程不变,并没新的agetty,说明此时的提示符[login:]是由login进程输出 超过60秒无输入用户,重新产生agetty及新的提示符[login:]
二.创建容器
root@debian:~# lxc-create -n vmcln -t debian
容器的/etc/inittab文件有如下内容
1:2345:respawn:/sbin/getty 38400 console
c1:12345:respawn:/sbin/getty 38400 tty1 linux
c2:12345:respawn:/sbin/getty 38400 tty2 linux
c3:12345:respawn:/sbin/getty 38400 tty3 linux
c4:12345:respawn:/sbin/getty 38400 tty4 linux
注: 1)宿主没/etc/inittab
2)若宿主使用传统sysvinit则也有/etc/inittab文件,内容大致如下
1:2345:respawn:/sbin/getty 38400 tty1
2:23:respawn:/sbin/getty 38400 tty2
3:23:respawn:/sbin/getty 38400 tty3
3)容器的/etc/inittab是模版脚本添加上的,注意是用c1、c2(与宿主1、2不同)
三.容器后台运行 1.打开第二个模拟终端[pts/1]并启动容器
root@debian:~# lxc-start -n vmcln
因为容器后台运行,没进入容器shell 但可在宿主上查看容器有哪些进程
linlin@debian:~$ ps -ef
UID PID PPID C STIME TTY TIME CMD
...
linlin 2978 1886 0 12:44 pts/1 00:00:00 bash
root 2980 2978 0 12:44 pts/1 00:00:00 su -
root 2981 2980 0 12:44 pts/1 00:00:00 -bash
root 3673 1 0 13:05 ? 00:00:00 [lxc monitor] /var/lib/lxc vmcln
root 3678 3673 0 13:05 ? 00:00:00 /sbin/init
root 3717 3678 0 13:05 ? 00:00:00 /lib/systemd/systemd-journald
root 3747 3678 0 13:05 ? 00:00:00 dhclient ...
root 3844 3678 0 13:05 pts/0 00:00:00 /sbin/agetty -o -p -- \u --noclear --keep-baud - 115200,38400,9600 vt220
1)PID列是进程号,PPID列是父进程 进程号1886是宿主上的进程lxterminal 进程号3673是容器进程 进程号3678是容器里的第一个进程,该进程号是从宿主视角来看,不是容器视角里进程号
从PID关联PPID依次查看3673->3678->3844,进程号3844(agetty)应属于容器
2)TTY列是终端 pts/1是打开第二个模拟终端产生的伪终端,上面ps命令就在该终端里执行 进程3844(agetty)不是在模拟终端[pts/0]上运行,为何也显示pts/0?但应该是容器的tty0 tty是设备,可能ps并不会区分宿主和容器的设备,只是显示设备号对应的名称
2.1)伪终端设备
linlin@debian:~$ ls /dev/pts
0 1 ptmx
linlin@debian:~$
2.2)关闭[pts/0]
linlin@debian:~$ ls /dev/pts
1 ptmx
linlin@debian:~$ ps -ef
UID PID PPID C STIME TTY TIME CMD
...
root 3844 3678 0 13:05 ? 00:00:00 /sbin/agetty -o -p -- \u --noclear --keep-baud - 115200,38400,9600 vt220
...
linlin@debian:~$
可见/dev/pts/0设备文件不存在了,显示'?'猜测ps的TTY列按/dev/是否存在文件显示
linlin@debian:~$ strace ps -e
openat(AT_FDCWD, "/proc/2978/cmdline"...
read(4, "bash\0"...
...
newfstatat(AT_FDCWD, "/dev/pts1"...) = -1 ENOENT (没有那个文件或目录)
newfstatat(AT_FDCWD, "/dev/pts/1"...) = 0
...
openat(AT_FDCWD, "/proc/3844/cmdline"...
read(4, "/sbin/agetty..."...
...
newfstatat(AT_FDCWD, "/dev/pts0"...) = -1 ENOENT (没有那个文件或目录)
newfstatat(AT_FDCWD, "/dev/pts/0"...) = -1 ENOENT (没有那个文件或目录)
...
write(1, " 2978 pts/1 00:00:00 bash\n"...
...
write(1, " 3844 ? 00:00:00 agetty"...
...
2.3)重新打开[pts/0],ps再次显示pts/0
2.4)可见打开模拟终端创建伪终端设备,关闭模拟终端删除伪终端设备 设备文件可预先创建,也可按需触发创建
linlin@debian:~$ ls -l /dev/ptmx
crw-rw-rw- 1 root tty 5, 2 4月18日 13:08 /dev/ptmx <= 该时间总是ls执行时的时刻,是不是命令输出信息到其关联pts便产生了'修改'?stat查看创建时间是系统启动时
linlin@debian:~$ strace xterm
...
openat(AT_FDCWD, "/dev/tty", O_RDWR) = 4
...
openat(AT_FDCWD, "/dev/ptmx", O_RDWR) = 4
...
read(7, "\2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0/dev/pts"...
...
是open ptmx,没open pts ptmx作为主设备,pts作为从设备,一对多 打开ptmx自动创建pts
2.缺省控制台 1)[pts/0]
root@debian:~# lxc-console -n vmcln
Connected to tty 1
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself
lxc-console试图连接tty1,但因为没有tty1,所以没有登录界面
2)[pts/1]
linlin@debian:~$ ps -ef
...
root 3888 3884 0 13:09 pts/0 00:00:00 lxc-console -n vmcln
linlin 3894 1886 0 13:10 pts/1 00:00:00 bash
linlin 3901 3894 99 13:11 pts/1 00:00:00 ps -ef
linlin@debian:~$
并没新的agetty
3.指定控制台 1)[pts/0]
root@debian:~# lxc-console -n vmcln -t 0
Connected to tty 0
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself
lxc-console试图连接tty0,也是没有登录界面 不是pts/0上有agetty进程吗?难道pts/0用于lxc-start前台?
经反复测试,原来虽然lxc-console没立即输出[login: ]提示符,但按回车就输出[login: ]提示符,可以正常登录
下文当无[login: ]提示符时要多按回车,以免误解为不成功
四.容器前台运行 1.打开[pts/0]并启动容器
linlin@debian:~$ ps
PID TTY TIME CMD
1983 pts/0 00:00:00 bash
2030 pts/0 00:00:00 ps
linlin@debian:~$ su -
密码:
root@debian:~# ps
PID TTY TIME CMD
2031 pts/0 00:00:00 su
2035 pts/0 00:00:00 bash
2038 pts/0 00:00:00 ps
root@debian:~#
带前台运行-F参数
root@debian:~# lxc-start -n vmcln -F
...
Queued start job for default target multi-user.target.
[ OK ] Created slice system-getty.slice - Slice /system/getty.
...
[ OK ] Reached target sysinit.target - System Initialization.
...
[ OK ] Started console-getty.service - Console Getty.
[ OK ] Reached target getty.target - Login Prompts.
...
[ OK ] Finished systemd-update-utmp-runlevel.service - Record Runlevel Change in UTMP.
Debian GNU/Linux 12 vmcln console
vmcln login:
已打开容器登录界面
2.暂不登录 1)打开[pts/1]并查看进程信息
linlin@debian:~$ ps -ef
...
root 2066 2035 0 08:12 pts/0 00:00:00 lxc-start -n vmcln -F
root 2071 2066 0 08:12 ? 00:00:00 /sbin/init
root 2102 2071 0 08:12 ? 00:00:00 /lib/systemd/systemd-journald
root 2135 2071 0 08:12 ? 00:00:00 dhclient ...
root 2163 2071 0 08:12 pts/0 00:00:00 /sbin/agetty -o -p -- \u --noclear --keep-baud - 115200,38400,9600 vt220
...
linlin@debian:~$
3.前台登录容器
Debian GNU/Linux 12 vmcln console
vmcln login: root
密码: <= 输入口令
root@vmcln:~# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 08:12 ? 00:00:00 /sbin/init
root 18 1 0 08:12 ? 00:00:00 /lib/systemd/systemd-journald
root 50 1 0 08:12 ? 00:00:00 dhclient ...
root 78 1 0 08:12 pts/0 00:00:00 /bin/login -p --
root 85 78 0 08:13 pts/0 00:00:00 -bash
root 88 85 0 08:13 pts/0 00:00:00 ps -ef
root@vmcln:~#
登录后login代替了原进程agetty,容器进程有自己的命名空间,容器78进程号应对应宿主2163进程号
root@vmcln:~# ls -l /etc/systemd/system/getty.target.wants
总计 0
lrwxrwxrwx 1 root root 9 4月 2日 10:18 container-getty@0.service -> /dev/null
lrwxrwxrwx 1 root root 9 4月 2日 10:18 container-getty@1.service -> /dev/null
lrwxrwxrwx 1 root root 9 4月 2日 10:18 container-getty@2.service -> /dev/null
lrwxrwxrwx 1 root root 9 4月 2日 10:18 container-getty@3.service -> /dev/null
lrwxrwxrwx 1 root root 9 4月 2日 10:18 container-getty@4.service -> /dev/null
lrwxrwxrwx 1 root root 17 4月 2日 10:18 getty@tty1.service -> ../getty@.service
lrwxrwxrwx 1 root root 17 4月 2日 10:18 getty@tty2.service -> ../getty@.service
lrwxrwxrwx 1 root root 17 4月 2日 10:18 getty@tty3.service -> ../getty@.service
lrwxrwxrwx 1 root root 17 4月 2日 10:18 getty@tty4.service -> ../getty@.service
root@vmcln:~# cat /etc/systemd/system/getty-static.service
...
ExecStart=systemctl --no-block start getty@tty2.service getty@tty3.service getty@tty4.service
...
root@vmcln:~#
上面getty内容是创建容器过程中模版脚本修改过的
参考上面命令,启动服务
root@vmcln:~# systemctl --no-block start container-getty@3.service
root@vmcln:~# ps -ef
...
root 85 78 0 08:13 pts/0 00:00:00 -bash
root 92 1 0 08:21 pts/3 00:00:00 /sbin/agetty -o -p -- \u --noclear - vt220
root 94 85 0 08:22 pts/0 00:00:00 ps -ef
root@vmcln:~#
已产生agetty/tty3(pts/3)
注:在宿主启动getty@tty2.service可产生agetty,在容器却不行
root@vmcln:~# tty
/dev/console
root@vmcln:~# ls -l /dev/console
crw------- 1 root tty 136, 0 4月19日 08:23 /dev/console <= 该时间总是ls执行时的时刻,应该是命令输出信息到当前控制台便产生了'修改'?
root@vmcln:~# stat /dev/console
...
访问时间:2025-04-19 08:24:36.000000000 +0800
修改时间:2025-04-19 08:24:36.000000000 +0800
变更时间:2025-04-19 08:13:34.607964935 +0800 <= 每次登录进入shell的时刻
创建时间:-
root@vmcln:~# ls -l /dev/pts
总计 0
crw------- 1 root tty 136, 0 4月19日 08:25 0 <= 该时间总是ls执行时的时刻
crw--w---- 1 root tty 136, 1 4月19日 08:12 1
crw--w---- 1 root tty 136, 2 4月19日 08:12 2
crw--w---- 1 root tty 136, 3 4月19日 08:12 3
crw--w---- 1 root tty 136, 4 4月19日 08:12 4
crw-rw-rw- 1 root root 5, 2 4月19日 08:25 ptmx <= PTY master multiplex,该时间总是ls执行时的时刻
root@vmcln:~#
可见容器的/dev/console设备号等同/dev/pts/0,不同于宿主/dev/console设备号 当前处在pts/0,执行命令输出信息会使其产生'修改'
4.在[pts/1]启动容器控制台
linlin@debian:~$ ps -ef
...
linlin 1979 1743 0 08:10 ? 00:00:06 lxterminal
linlin 1983 1979 0 08:10 pts/0 00:00:00 bash <= 模拟终端[pts/0],其父进程是lxterminal
root 2031 1983 0 08:10 pts/0 00:00:00 su -
root 2035 2031 0 08:10 pts/0 00:00:00 -bash
root 2066 2035 0 08:12 pts/0 00:00:00 lxc-start -n vmcln -F
root 2071 2066 0 08:12 ? 00:00:00 /sbin/init
root 2102 2071 0 08:12 ? 00:00:00 /lib/systemd/systemd-journald
root 2135 2071 0 08:12 ? 00:00:00 dhclient ...
root 2163 2071 0 08:12 pts/0 00:00:00 /bin/login -p --
root 2173 2163 0 08:13 pts/0 00:00:00 -bash <= 对应容器里进程号85
linlin 2187 1979 0 08:14 pts/1 00:00:00 bash <= 模拟终端[pts/1]
root 2200 2187 0 08:17 pts/1 00:00:00 su - <= \
root 2202 2200 0 08:17 pts/1 00:00:00 -bash <= / [pts/1]的子孙进程
root 2269 2071 0 08:21 ? 00:00:00 /sbin/agetty -o -p -- \u --noclear - vt220
root 2286 2202 99 08:25 pts/1 00:00:00 ps -ef
linlin@debian:~$
root@debian:~# lxc-console -n vmcln
Connected to tty 1
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself
默认连接tty1,因不存在tty1,没登录界面,按<Ctrl+a q>退出
参数指定tty3
root@debian:~# lxc-console -n vmcln -t 3
Connected to tty 3
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself
Debian GNU/Linux 12 vmcln pts/3
vmcln login:
已打开登录界面
5.暂不登录tty3,打开[pts/2]并查看进程信息
linlin@debian:~$ ps -ef
...
root 2269 2071 0 08:21 ? 00:00:00 /sbin/agetty -o -p -- \u --noclear - vt220
root 2297 2202 0 08:26 pts/1 00:00:00 lxc-console -n vmcln -t 3
linlin 2304 1979 0 08:27 pts/2 00:00:00 bash
linlin 2313 2304 0 08:28 pts/2 00:00:00 ps -ef
linlin@debian:~$
1)容器tty3控制台输入用户名但password提示符下不输入口令
linlin@debian:~$ ps -ef
...
root 2269 2071 0 08:21 ? 00:00:00 /bin/login -p -- <= login代替agetty
root 2297 2202 0 08:26 pts/1 00:00:00 lxc-console -n vmcln -t 3
linlin 2304 1979 0 08:27 pts/2 00:00:00 bash <= 模拟终端[pts/2]
linlin 2322 2304 0 08:30 pts/2 00:00:00 ps -ef
linlin@debian:~$
桌面环境打开了3个模拟终端窗口界面,但只一个lxterminal进程,好像现代的终端模拟器实现都是此方式 而每次打开传统的xterm都是独立的进程,每个xterm进程创建了各自bash子进程
2)容器tty3控制台60 秒后登录超时重新返回login提示符
linlin@debian:~$ ps -ef
...
root 2202 2200 0 08:17 pts/1 00:00:00 -bash
root 2297 2202 0 08:26 pts/1 00:00:00 lxc-console -n vmcln -t 3
linlin 2304 1979 0 08:27 pts/2 00:00:00 bash
root 2324 2071 0 08:30 ? 00:00:00 /sbin/agetty -o -p -- \u --noclear - vt220
linlin 2352 2304 99 08:32 pts/2 00:00:00 ps -ef
linlin@debian:~$
可见进程2269/login结束,产生2324/agetty
6.容器tty3控制台登录
Debian GNU/Linux 12 vmcln pts/3
vmcln login: root
密码:
root@vmcln:~# sleep 120 &
root@vmcln:~# ps
PID TTY TIME CMD
98 pts/3 00:00:00 login
104 pts/3 00:00:00 bash
109 pts/3 00:00:00 sleep
110 pts/3 00:00:00 ps
root@vmcln:~#
1)[pts/2]
linlin@debian:~$ ps -ef
...
root 2297 2202 0 08:26 pts/1 00:00:00 lxc-console -n vmcln -t 3
linlin 2304 1979 0 08:27 pts/2 00:00:00 bash
root 2324 2071 0 08:30 ? 00:00:00 /bin/login -p --
root 2362 2324 0 08:33 ? 00:00:00 -bash
root 2380 2362 0 08:37 ? 00:00:00 sleep 120
linlin 2387 2304 0 08:38 pts/2 00:00:00 ps -ef
linlin@debian:~$
sleep没显示pts/3
2)打开模拟终端[pts/3],sleep就显示pts/3了
7.不知如何自动启动container-getty@3.service
root@vmcln:~# systemctl list-unit-files | grep tty
console-getty.service enabled-runtime disabled
container-getty@.service indirect enabled
getty-static.service static -
getty@.service enabled enabled
serial-getty@.service disabled enabled
getty-pre.target static -
getty.target static -
root@vmcln:~# ls /usr/lib/systemd/system/con*
/usr/lib/systemd/system/console-getty.service /usr/lib/systemd/system/container-getty@.service
root@vmcln:~# ls /usr/lib/systemd/system/getty*
/usr/lib/systemd/system/getty-pre.target /usr/lib/systemd/system/getty-static.service
/usr/lib/systemd/system/getty@.service /usr/lib/systemd/system/getty.target
/usr/lib/systemd/system/getty.target.wants:
getty-static.service
root@vmcln:~#
1)容器/etc/systemd/system/getty-static.service中ExecStart一行加上container-getty@3.service,启动容器仍没产生tty3
2)systemctl enable 服务getty-static.service、container-getty@.service、container-getty@3.service都提示The unit files have no installation config
五.小结 1.上面缺省是systemd,宿主和容器启动后都只有一个agetty控制台,都可以systemctl --no-block start出一个控制台,但宿主可以按<ctrl><alt><F2>触发出一个控制台
2.在宿主虚拟控制台也可运行lxc-console
六.容器使用传统sysvinit代替systemd 1.安装sysvinit-core
root@vmcln:~# apt-get install sysvinit-core
...
下列软件包将被【卸载】:
systemd-sysv
下列【新】软件包将被安装:
initscripts insserv orphan-sysvinit-scripts psmisc startpar sysv-rc sysvinit-core
...
正在设置 sysvinit-core (3.06-4) ...
Not restarting sysvinit
root@vmcln:~#
2.后台运行
root@debian:~# lxc-start -n vmcln
linlin@debian:~$ ps -ef
...
root 3861 1 0 11:34 ? 00:00:00 [lxc monitor] /var/lib/lxc vmcln
root 3866 3861 0 11:34 ? 00:00:00 init [3]
root 4445 3866 0 11:34 ? 00:00:00 dhclient ...
root 4534 3866 0 11:34 pts/0 00:00:00 /sbin/getty 38400 console
root 4535 3866 0 11:34 pts/1 00:00:00 /sbin/getty 38400 tty1 linux
root 4536 3866 0 11:34 ? 00:00:00 /sbin/getty 38400 tty2 linux
root 4537 3866 0 11:34 ? 00:00:00 /sbin/getty 38400 tty3 linux
root 4538 3866 0 11:34 ? 00:00:00 /sbin/getty 38400 tty4 linux
linlin 4561 3695 99 11:35 pts/1 00:00:00 ps -ef
linlin@debian:~$
可见容器启动后产生4个tty
3.控制台登录 1)tty1
root@debian:~# lxc-console -n vmcln
Connected to tty 1
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself
Debian GNU/Linux 12 vmcln tty1
vmcln login: root
Password:
root@vmcln:~# ps -ef
...
root 656 1 0 11:34 pts/2 00:00:00 /sbin/getty 38400 tty2 linux
root 657 1 0 11:34 pts/3 00:00:00 /sbin/getty 38400 tty3 linux
root 658 1 0 11:34 pts/4 00:00:00 /sbin/getty 38400 tty4 linux
...
root@vmcln:~# ls -l /sbin/getty
lrwxrwxrwx 1 root root 6 11月22日 04:01 /sbin/getty -> agetty
root@vmcln:~#
2)tty2
root@debian:~# lxc-console -n vmcln
Connected to tty 2
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself
Debian GNU/Linux 12 vmcln tty2
vmcln login:
3)因为tty1~tty4在启动时已产生,所以lxc-console不用指定tty
七.总结 1.agetty程序在util-linux软件包,login程序在login软件包,都不属于systemd项目
linlin@debian:~$ dpkg -S agetty
util-linux: /usr/share/doc/util-linux/modems-with-agetty.txt
util-linux: /usr/share/man/man8/agetty.8.gz
util-linux: /usr/sbin/agetty
linlin@debian:~$
agetty和控制台紧密相关
2.[login:]提示符(以在tty3为例) 打开模拟终端,第一次lxc-console会立即输出[login:],登录进入后
1)正常退出登录,login进程结束,在当前tty3产生新进程号agetty,然后关闭模拟终端,当再次打开模拟终端并lxc-console(该tty3)不会立即输出[login:],需按回车才输出[login:]
2)未退出登录就直接关闭模拟终端,之前由login产生的子进程bash并不结束(仍在tty3),并没新的agetty产生,再次打开模拟终端并lxc-console(该tty3),无[login:],按回车直接进入之前shell界面,不需再登录 即会话并没结束,但会造成已退出的误解,在生产环境就会存在安全隐患
2.1)在sysvinit和systemd,login进程也并不结束
2.2)测试过systemd忘记什么条件情况,login进程结束,bash不结束
3)以上两情况lxc-console未按回车前造成不成功的误会
3.无论在宿主/容器、前台/后台、虚拟控制台/模拟终端,登录界面输入用户名并回车,但不输入口令,login覆盖agetty,60 秒后登录超时并由init(systemd/sysvinit)产生新agetty(tty号不变) 4.sysvinit使用/etc/inittab并启动多个tty,systemd不使用该配置文件