我刚刚通过ssh连接到了一台远程服务器,发现所有在bash中运行的命令/进程的stdout
和stderr
都被重定向到了某处。所以,我有以下问题:
如何检测:
1)在Linux中,哪个文件正在重定向stdout
、stderr
?
并且
2)如何将默认的stdout
和stderr
重新定向回/dev/tty?
非常感谢。
我刚刚通过ssh连接到了一台远程服务器,发现所有在bash中运行的命令/进程的stdout
和stderr
都被重定向到了某处。所以,我有以下问题:
如何检测:
1)在Linux中,哪个文件正在重定向stdout
、stderr
?
并且
2)如何将默认的stdout
和stderr
重新定向回/dev/tty?
非常感谢。
一个应该可以完全按照你在(2)中要求的执行的命令是:
exec >/dev/tty 2>&1
但我怀疑您对问题的分析是错误的。 查看ssh -v ...
的输出将会很有用(其中...
是您在原始ssh
命令中键入的任何参数)。
该命令:
ls -l /proc/$$/fd/{1,2}
这将向您显示哪些文件作为标准输出(文件描述符1)和标准错误(文件描述符2)处于打开状态。
1 -> pipe:[16418291]
与 pid 相关联吗? - Aquarius Power0 -> pipe:[16418291]
- Aquarius Power/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:~#
/dev/pts/3
。在第二个例子中,我将输入重定向到/dev/null
,因此标准输入文件描述符指向/dev/null
。在最后一个例子中,我通过管道将ls
的输出发送到cat
,标准输入文件描述符反映了这一点。据我所知,没有办法找出哪个进程拥有管道的另一端。在所有示例中,都有第四个文件描述符,表示ls
用于读取/proc/self/fd
的句柄。在这种情况下,它显示为/proc/15537
,因为/proc/self
实际上是指向/proc/pid
的符号链接,其中pid
是访问/proc/self
的进程的PID。只有在使用管道连接到tee
命令的另一个控制台参数启动时,才能实现这一点。
让我解释一下。
如果您登录/dev/tty1
,其他人登录/dev/tty2
。 如果您按照以下命令启动您的 shell(bash),则所有STDOUT/STDERR将被重新路由/复制到另一个 shell(在本例中为/dev/tty2
)。
bash 2>&1 | tee /dev/tty2
因此,坐在/dev/tty2
中的某个人将看到您的所有活动。
如果某人的登录shell是/bin/bash 2>&1 | tee /dev/tty2
而不是/bin/bash
,那么每次他登录时都会发生这种情况。但我不确定登录shell是否可以设置成这样。
如果有人以这种方式重定向了您的shell的所有输出,您只需检查后台是否运行任何tee
即可检查它。
ps ax | grep tee
这将输出类似以下的内容
tee /dev/tty2