首页 > 解决方案 > 无权更改 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,或者有什么方法可以对容器进行更多控制?

标签: linuxdockerttyioctl

解决方案


好吧,看来我已经解决了我自己的问题。我试图在没有权限进行 tty 配置的容器中运行代码。添加--cap-add SYS_TTY_CONFIG到 docker run 命令解决了这个问题。


推荐阅读