<p>I'm trying to understand how securityContext work in my cluster (k8s v 1.24 &amp; the node is an Ubuntu 18.04).</p>
<p>I simply want to do a simple cat /dev/tty0 from the container.</p>
<p>Here is my simplified configuration of the node :</p>
<pre><code>bash-4.2# ls -al /dev/tty0
crw--w---- 1 root tty 4, 0 Jul 25 05:16 /dev/tty0

bash-4.2# grep tty /etc/group
tty:x:5
</code></pre>
<p>I mounted /dev/tt0 to access it from the container &amp; run the container with group Id 5 &amp; userId 0 (i tried also without the runAsUser but the behaviour is the same)</p>
<pre><code>spec:
  volumes:
    - name: tty
      hostPath:
        path: /dev/tty0
  containers:
    - name: mycontainer
      image: ...
      volumeMounts:
        - name: tty
          mountPath: /dev/tty0
      securityContext:
        runAsUser: 0
        runAsGroup: 5
</code></pre>
<p>When I log in the container:</p>
<pre><code>bash-4.2# id
uid=0(root) gid=5(tty) groups=5(tty)
bash-4.2# ls -al /dev/tty0
crw--w---- 1 root tty 4, 0 Jul 25 05:16 /dev/tty0
</code></pre>
<p>But i cannot access /dev/tty0.</p>
<pre><code>bash-4.2# cat /dev/tty0
cat: /dev/tty0: Operation not permitted
</code></pre>
<p>While from the node I don't have this error message.</p>
<p>This is just for testing purpose, my originale use case is the launching of Xorg but I get the Operation not permitted error message.</p>
<p>I tried adding the &quot;privileged: true&quot; securityContext, and with this it works. However, it is not a good practise to use this maximum capacity to the container and I'm trying to understand what is the minimal security context to give to the pod.</p>
<p>Thanks!</p>

Ptiseb
<p>The securityContext specifies that the container should run as the user with UID0 and GID 5. When the container runs with root privileges, it still could have certain restrictions imposed by the underlying container runtime for security reasons.</p>
<p>In your case, the issue is, that accessing <strong>/dev/tty0 typically requires elevated privileges because it's a critical device file representing the first virtual terminal.</strong></p>
<p>You can grant elevated privileges like this:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  volumes:
    - name: tty
      hostPath:
        path: /dev/tty0
  containers:
    - name: mycontainer
      image: ...
      volumeMounts:
        - name: tty
          mountPath: /dev/tty0
      securityContext:
        privileged: true
</code></pre>
<p>But attention, It's generally a good practice to run containers with the least privileged user necessary to perform their intended task, because running a container with root privileges can be dangerous as it allows direct access to system resources and can compromise the host system</p>
