Linux 如何将 stdout、stderr 重新路由回 /dev/tty

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8994593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:12:52  来源:igfitidea点击:

How to reroute stdout, stderr back to /dev/tty

linuxbashstdoutstderrtty

提问by Sergey

I just ssh-ed to some remote server and found that stdoutand stderrof all commands/processes I am trying to run in bash is redirected to somewhere. So, I got following questions

我只是SSH-ED一些远程服务器,发现stdoutstderr所有的命令/处理我试图在bash运行被重定向到某个地方。所以,我有以下问题

How to detect:

如何检测:

1) Which file stdout, stderris beeing rerouted in Linux?

1)哪个文件stdoutstderr是beeing在Linux中重新路由?

and

2) And how reroute by default stdoutand stderrback to /dev/tty?

2)以及如何在默认情况下重新路由stdoutstderr返回到 /dev/tty?

Thank you in advance.

先感谢您。

采纳答案by zwol

A command that should do literally what you asked for in (2) is

应该按照您在(2)中要求的字面意思执行的命令是

exec >/dev/tty 2>&1

But I suspect that your analysis of the problem is incorrect. It would be useful to see the output of ssh -v ...(where ...is whatever arguments you typed in your original sshcommand).

但是我怀疑你对问题的分析是错误的。查看ssh -v ......您在原始ssh命令中输入的任何参数在哪里)的输出会很有用。

回答by Shiplu Mokaddim

It can only be done if your longing shell is started with a pipe to teecommand with another console as a parameter.

只有当您渴望的 shell 以管道启动时,才能完成,tee并以另一个控制台作为参数进行命令。

Let me explain.

让我解释。

If you are logging in /dev/tty1and someone else is logging in /dev/tty2. If you start your shell (bash) by following command all the STDOUT/STDERR will be rerouted/copied to another shell (/dev/tty2in this case).

如果您正在登录/dev/tty1而其他人正在登录/dev/tty2。如果您通过以下命令启动您的 shell (bash),所有 STDOUT/STDERR 都将被重新路由/复制到另一个 shell(/dev/tty2在这种情况下)。

bash 2>&1 | tee /dev/tty2

So, someone sitting in /dev/tty2will see all of your activity.

所以,坐在里面的人/dev/tty2会看到你的所有活动。

If someone logins shell is /bin/bash 2>&1 | tee /dev/tty2instead of /bin/bashIt'll happen every time he logs in. But I am not sure login shell can be set that way.

如果有人登录 shell/bin/bash 2>&1 | tee /dev/tty2而不是/bin/bash每次登录时都会发生。但我不确定登录 shell 可以这样设置。

If someone reroutes all the output of your shell this way you can check it just by checking if any teeis running in background.

如果有人以这种方式重新路由您的 shell 的所有输出,您可以通过检查是否有任何tee在后台运行来检查它。

ps ax | grep tee

This will output something like

这将输出类似

tee /dev/tty2

回答by caf

The command:

命令:

ls -l /proc/$$/fd/{1,2}

will show you which files are open as stdout (file descriptor 1) and stderr (file descriptor 2).

将显示哪些文件作为 stdout(文件描述符 1)和 stderr(文件描述符 2)打开。

回答by Robbie Mckennie

An answer to your first question could be found in /proc/self/fd. It contains symlinks to the files (or other things, pipes, sockets, etc) that your bash instance is connected to.

可以在 中找到您的第一个问题的答案/proc/self/fd。它包含指向 bash 实例所连接的文件(或其他东西、管道、套接字等)的符号链接。

root@mammon:~# ls -l /proc/self/fd
total 0
lrwx------ 1 root root 64 May 21 02:18 0 -> /dev/pts/3
lrwx------ 1 root root 64 May 21 02:18 1 -> /dev/pts/3
lrwx------ 1 root root 64 May 21 02:18 2 -> /dev/pts/3
lr-x------ 1 root root 64 May 21 02:18 3 -> /proc/15529/fd/
root@mammon:~# ls -l /proc/self/fd < /dev/null
total 0
lr-x------ 1 root root 64 May 21 02:18 0 -> /dev/null
lrwx------ 1 root root 64 May 21 02:18 1 -> /dev/pts/3
lrwx------ 1 root root 64 May 21 02:18 2 -> /dev/pts/3
lr-x------ 1 root root 64 May 21 02:18 3 -> /proc/15536/fd/
root@mammon:~# ls -l /proc/self/fd | cat
total 0
lrwx------ 1 root root 64 May 21 02:18 0 -> /dev/pts/3
l-wx------ 1 root root 64 May 21 02:18 1 -> pipe:[497711]
lrwx------ 1 root root 64 May 21 02:18 2 -> /dev/pts/3
lr-x------ 1 root root 64 May 21 02:18 3 -> /proc/15537/fd/
root@mammon:~#

In the first example, you can see the first 3 file descriptors (which are the standard output, input, and error, respectively) all point to my pseudo-terminal /dev/pts/3. In the second example I've redirected the input to /dev/null, so the standard input file descriptor points to /dev/null. And in the final example I've sent the output of lsto catthrough a pipe, and the standard input file descriptor reflects this. As far as I know there is no way to find which process has the other end of the pipe. In all examples there is the fourth file descriptor that represents the handle that lshas for reading /proc/self/fd. In this case it says /proc/15537because /proc/selfis in fact a symlink to /proc/pidwhere pidis the PID of the process accessing /proc/self.

在第一个示例中,您可以看到前 3 个文件描述符(分别是标准输出、输入和错误)都指向我的伪终端/dev/pts/3。在第二个示例中,我将输入重定向到/dev/null,因此标准输入文件描述符指向/dev/null. 在最后一个例子中,我通过管道发送了lsto的输出,cat标准输入文件描述符反映了这一点。据我所知,无法找到管道的另一端是哪个进程。在所有示例中,都有第四个文件描述符代表ls用于读取的句柄/proc/self/fd。在这种情况下,它说/proc/15537,因为/proc/self事实上是一个符号链接到/proc/pid这里pid是进程访问的PID /proc/self