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

时间:2022-03-30 16:40:47

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用户,或者是登录的用户,所以原则上我有person来做这个。我怎么说服X呢?

Some examples of situations like this:

这类情况的一些例子如下:

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

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

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

2 个解决方案

#1


40  

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

简而言之,您必须设置DISPLAY environment变量,然后应用程序将运行。

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 -X handles this for you, which is why it's awesome, but the manual procedure involves running xauth extract - $DISPLAY on 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提取(在X服务器帐户上显示$DISPLAY,并将数据输入xauth merge)。(警告:数据是二进制的。)

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

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

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

#2


16  

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

结果是,您必须知道X显示(放置在显示环境变量中)和magic 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("\0").select { |e| e =~ /^(DISPLAY|XAUTHORITY)=/ }'

#1


40  

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

简而言之,您必须设置DISPLAY environment变量,然后应用程序将运行。

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 -X handles this for you, which is why it's awesome, but the manual procedure involves running xauth extract - $DISPLAY on 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提取(在X服务器帐户上显示$DISPLAY,并将数据输入xauth merge)。(警告:数据是二进制的。)

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

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

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

#2


16  

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

结果是,您必须知道X显示(放置在显示环境变量中)和magic 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("\0").select { |e| e =~ /^(DISPLAY|XAUTHORITY)=/ }'