-su: /dev/tty: No such device or address

15,842

The manpage for su states that the executed command will have no controlling terminal. Any writes to /dev/tty will return the ENXIO error:

$ errno ENXIO
ENXIO 6 No such device or address

sudo does allocate a controlling terminal:

sudo -u someone /bin/bash -c "echo hello > /dev/tty"

There's no need for you to make a symbolic link to /dev/tty (/dev/stdout and /dev/stderr is enough) or use sudo if you use the USER directive in the Dockerfile or supervisor.

Share:
15,842

Related videos on Youtube

manifestor
Author by

manifestor

Updated on June 04, 2022

Comments

  • manifestor
    manifestor almost 2 years

    could please someone explain to me why this happens?

    # su - someone -s /bin/bash -c "ls -la /dev/tty"
    crw-rw-rw- 1 nobody nogroup 5, 0 Dec  7 20:53 /dev/tty
    # BUT:
    # su - someone -s /bin/bash -c "echo hello > /dev/tty"
    -su: /dev/tty: No such device or address
    

    I'm trieng to build a docker Container which has two services inside. Those services a startet by a Shell-Script:

    CMD ["./starter.sh"]
    

    Withing the Dockerfile I have redirected the Logs to /dev/stderr or /dev/tty

    # None of the following works:
    RUN ln -sf /dev/tty /var/log/thelog.log
    RUN ln -sf /dev/stdout /var/log/thelog.log
    RUN ln -sf /dev/stderr /var/log/thelog.log
    

    The problem is that I'm trying to run one of the services as not root (su - someone -c "service"), which give's the following error:

    unable to open log file [/var/log/thelog.log]: [6] No such device or address
    

    How could I solve this problem? I want the logs to be linked to /dev/* AND want to run the User as non-root. Also I tried to add the User to the group tty, which did not work out.

    Thanks.

    • tripleee
      tripleee over 6 years
      Replacing devices with symlinks seems like a very brittle and dangerous thing to do. The kernel manipulates the tty, it's not a static file descriptor, that's why you get the error in the first place.
    • tripleee
      tripleee over 6 years
      Wrap the service in something which captures its output to a sensible place. Most sane services don't really print anything to the console anyway.
    • manifestor
      manifestor over 6 years
      Hi, thanks, but doing so is a common practice with docker. Redirecting to /dev/* makes you see the logs with docker logs mycontainer. So the crux of this matter is the su - and starting the process as another (!=root) user.
  • manifestor
    manifestor over 6 years
    Hi thanks. Yes, true I could use supervisor and I know about USER. But how do you start 2 services with a Shell-Script where one must run as non root and one as root without using supervisor? If I use USER non_root I could not start the root-service as I have only one RUN command.
  • Ricardo Branco
    Ricardo Branco over 6 years
    Either use supervisor or run each service in a different container.
  • manifestor
    manifestor over 6 years
    Just wanted to complement, that even with supervisord it is not possible to use /dev/tty as a non-root User.