linux - 无权更改 docker 容器内的 tty 模式
问题描述
我正在尝试在 docker 容器中运行一些软件,该容器想要为当前使用的 tty 执行 VT_SETMODE。这将始终失败,并显示“不允许操作”的错误。
我试过玩权限/组,但没有运气。
最后我创建了一个小片段来重现错误:
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/vt.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
int main() {
const char *tty_path;
tty_path = "/dev/tty1";
int fd = open(tty_path, O_RDWR | O_CLOEXEC);
if (fd < 0) {
printf("ERROR: Failed to open %s\n", strerror(errno));
return 1;
}
struct vt_mode mode = {
.mode = VT_AUTO,
};
errno = 0;
ioctl(fd, VT_SETMODE, &mode);
if (errno) {
printf("ERROR: %s\n", strerror(errno));
return 1;
}
return 0;
}
我使用一个简单的 dockerfile 在 docker 容器中运行代码:
FROM archlinux/base
RUN pacman -Sy --noconfirm gcc
那是从命令开始的:
docker build -f Dockerfile -t tty-test . && docker run --device /dev/tty1 -v $HOME/tty-test:/volume -it tty-test /bin/bash -c 'cd /volume && gcc tty_r.c && ./a.out ; /bin/bash'
输出是这样的:
ERROR: Operation not permitted
谁能解释为什么无法从容器访问 tty,或者有什么方法可以对容器进行更多控制?
解决方案
好吧,看来我已经解决了我自己的问题。我试图在没有权限进行 tty 配置的容器中运行代码。添加--cap-add SYS_TTY_CONFIG
到 docker run 命令解决了这个问题。
推荐阅读
- android-studio - OpScreenModeManager 在真实设备(不是模拟器)上运行应用程序时向 Android Studio 日志发送垃圾邮件
- c - 使用 Glade for C 调整图像大小
- r - R DT:在(excel)导出中显示行分组
- wordpress - WP 更改类别/标签 URL 结构
- linux-device-driver - 发送 12 个数据包后 Ping 不发送,出现错误“ping: sendmsg: No buffer space available”
- java - Criteria.DISTINCT_ROOT_ENTITY 实际上是如何工作的?
- spring-boot - 运行程序时出现错误:应用程序运行失败
- php - Laravel 桌面应用程序
- python - Python For-Loop, range() 带条件
- uwp - 如何从 C++/WinRT 中的 URI 对象获取文件名