无法以普通用户身份运行 Expect “no more ptys.”

无法以普通用户身份运行 Expect “no more ptys.”

我有一个运行着 CentOS 7.4 的 LXC 容器。
在以普通用户身份运行时,我收到以下错误。

user@server ~>  expect -c "spawn ls"
spawn ls
The system has no more ptys.  Ask your system administrator to create more.
    while executing
"spawn ls"


但如果我们以 root 用户执行相同操作,它就可以起作用。

[root@server]#  expect -c "spawn ls"
spawn ls

mount 的输出在挂载点中也有这些。/dev/pts 上的 devpts 类型 devpts (rw,relatime,seclabel,mode=620,ptmxmode=000)

我已经在服务器上运行了以下更新。

mknod -m 600 /dev/console c 5 1 2>/dev/null
mknod -m 666 /dev/null c 1 3 2>/dev/null
mount -n -t tmpfs none /dev 2>/dev/null
mknod -m 622 /dev/console c 5 1 2>/dev/null
mknod -m 666 /dev/null c 1 3 2>/dev/null
mknod -m 666 /dev/zero c 1 5 2>/dev/null
mknod -m 666 /dev/ptmx c 5 2 2>/dev/null
mknod -m 666 /dev/tty c 5 0 2>/dev/null
mknod -m 444 /dev/random c 1 8 2>/dev/null
mknod -m 444 /dev/urandom c 1 9 2>/dev/null
chown root:tty /dev/{console,ptmx,tty} 2>/dev/null
ln -s /proc/self/fd /dev/fd 2>/dev/null
ln -s /proc/self/fd/0 /dev/stdin 2>/dev/null
ln -s /proc/self/fd/1 /dev/stdout 2>/dev/null
ln -s /proc/self/fd/2 /dev/stderr 2>/dev/null
ln -s /proc/kcore /dev/core 2>/dev/null
mkdir /dev/pts 2>/dev/null
mkdir /dev/shm 2>/dev/null
mount -t devpts -o gid=4,mode=620 none /dev/pts 2>/dev/null
mount -t tmpfs none /dev/shm 2>/dev/null
chmod 666 /dev/null

但我仍然无法以普通用户身份运行 expect 命令。
我有一些脚本需要以普通用户身份使用 expect 语句运行。

我已经尝试了所有能想到的办法。请帮忙!!

答案1

经过大量搜索后,我发现需要在 chroot 内创建 /dev/ptmx 和 /dev/pts 结构。

 #!/bin/sh
 mknod /dev/ptmx c 5 2
 chmod 666 /dev/ptmx
 mkdir /dev/pts
 chmod 755 /dev/pts
 mount -t devpts -o gid=5,mode=620 none /dev/pts

感谢网站https://mintcast.org/building-linux/ 我从上述网站引用了该脚本和详细信息。

执行脚本后,我就可以执行命令了

[user@server]#  expect -c "spawn ls"
spawn ls

答案2

与 chroot 环境无关,但对于遇到此问题的其他任何人,请检查/var/log/audit/audit.logSELinux 是否未阻止访问。即使该程序是由 root 运行的,我也遇到了这个问题,并发现了以下内容:

type=AVC msg=audit(1560463619.636:16181): avc:  denied  { read write } for  pid=32466
comm="myscript.exp" name="ptmx" dev="devtmpfs" ino=1149 scontext=system_u:system_r:fail2ban_t:s0
tcontext=system_u:object_r:ptmx_t:s0 tclass=chr_file permissive=0

使用audit2allow我可以生成一个策略来允许我的脚本运行。

相关内容