Linux 如何从 X 会话外部(例如从控制台或 SSH)运行 X 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1584411/
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-03 17:47:06  来源:igfitidea点击:

How to run an X program from outside the X session (e.g. from the console or SSH)

linuxsshcronx11

提问by JasonSmith

Without being the person logged in at the console, how do I run an X application and have it display on that X session? Assume I am either root, or I am the same user who logged in, so in principle I have persmission to do this. But how do I convince X of this?

如果不是在控制台登录的人,我如何运行 X 应用程序并将其显示在该 X 会话中?假设我是 root,或者我是登录的同一用户,所以原则上我有权这样做。但我如何说服 X 相信这一点?

Some examples of situations like this:

此类情况的一些示例:

  • Log in with SSH and run a program that displays on the remote computer's screen (nottunneled through SSH—that is totally different)
  • A cron job to take a screenshot of the X session via ImageMagick's importcommand
  • Running a keystroke logger for audit purposes
  • 使用 SSH 登录并运行一个显示在远程计算机屏幕上的程序(不是通过 SSH 隧道——这是完全不同的)
  • 通过 ImageMagick 的import命令截取 X 会话屏幕截图的 cron 作业
  • 出于审计目的运行击键记录器

This is a simpler version of Launch OpenGL app straight from a windowless Linux Terminal

这是直接从无窗口 Linux 终端启动 OpenGL 应用程序的更简单版本

采纳答案by hobbs

The short answer is that you have to set the DISPLAYenvironment variable, and then the app will run.

简短的回答是您必须设置DISPLAY环境变量,然后应用程序才能运行。

The long answer is that we've got Xauth, and unless you're running as the same user on the same machine that's probably not going to work unless you export the Xauth credential from the account running the X server to the account running the X client. ssh -Xhandles this for you, which is why it's awesome, but the manual procedure involves running xauth extract - $DISPLAYon the X server account and feeding that data into xauth merge -on the client account. (Warning: the data is binary.)

长的答案是我们有 Xauth,除非你在同一台机器上以相同的用户身份运行,除非你将 Xauth 凭证从运行 X 服务器的帐户导出到运行 X 的帐户客户。ssh -X为您处理这个,这就是它很棒的原因,但是手动过程涉及xauth extract - $DISPLAY在 X 服务器帐户上运行并将该数据输入xauth merge -到客户端帐户。(警告:数据是二进制的。)

On modern Linux systems, there is one X session at :0 and the X11 authority data file is always $HOME/.Xauthorityso you can most often set two environment variables, for example, in Bash:

在现代 Linux 系统上,在 :0 处有一个 X 会话,而 X11 权限数据文件总是$HOME/.Xauthority如此,因此您通常可以设置两个环境变量,例如,在 Bash 中:

export XAUTHORITY=/home/$your_username/.Xauthority
export DISPLAY=':0'

回答by JasonSmith

The upshot is that you have to know the X display (placed in the DISPLAYenvironment variable) and the magic cookie (placed in a file, with the filename in the XAUTHORITYenvironment variable).

结果是你必须知道 X 显示(放置在DISPLAY环境变量中)和魔法 cookie(放置在一个文件中,文件名在XAUTHORITY环境变量中)。

The quick-and-dirty way

快速而肮脏的方式

On the system running X, if you are root or you are the same user who logged in to X, just assume the most common display and cookie files (works on almost any standard desktop install of any distro).

在运行 X 的系统上,如果您是 root 或者您是登录到 X 的同一用户,只需假设最常见的显示和 cookie 文件(适用于任何发行版的几乎所有标准桌面安装)。

env DISPLAY=:0 XAUTHORITY=/home/whoever/.Xauthority /path/to/my/X/program

The more surefire way

更可靠的方式

Find them from the environment of an already-running X program. Again, if you are root or the same user who is logged in, this will tell you (if the user is using GNOME):

从已经运行的 X 程序的环境中找到它们。同样,如果您是 root 用户或登录的同一用户,这将告诉您(如果用户使用的是 GNOME):

cat /proc/`pgrep -f ^x-session-manager`/environ \
  | ruby -ne 'puts $_.split("##代码##").select { |e| e =~ /^(DISPLAY|XAUTHORITY)=/ }'