大网数据广告活动
服务器运维问答

Run local X11 command from remote SSH connection

· 52CE 我爱测

When I connect to my server using ssh -X and run for example xterm on the server, the xterm opens up on my local machine. What I actually want to do is for the xterm which is installed on the server to open on the server and rend…

问题原文

原帖链接 · 0x29a · CC BY-SA 4.0

When I connect to my server using ssh -X and run for example xterm on the server, the xterm opens up on my local machine.

What I actually want to do is for the xterm which is installed on the server to open on the server and render on the servers physical display.. In other words, the same effect if I sat by the server and typed in xterm on the servers keyboard.

If I connect to the server skipping the -X argument and run xterm, I get:

/usr/bin/xterm: Xt error: Can't open display: 
/usr/bin/xterm: DISPLAY is not set

So I tried to set the DISPLAY variable like this:

export DISPLAY=localhost:0.0

but then I get:

/usr/bin/xterm: Xt error: Can't open display: localhost:0.0

How can I run the xterm command and make it execute on the servers physical screen?

已采纳解答

原帖链接 · kasperd · CC BY-SA 4.0

In order for this to work your server must obviously be running an X server in the first place. Linux servers usually don't have any X server installed, so the presence of an X server on the server is the first thing you have to verify.

Assuming your server is in fact running an X server the most likely explanation is that you are connecting to the X server using the wrong protocol.

There is a difference between DISPLAY=localhost:0.0 and DISPLAY=:0.0.

When you write localhost:0.0 it means the client will resolve the hostname localhost and connect to the X server over TCP. The port number is calculated as 6000 plus display number, which here will be 6000 + 0 = 6000.

When you write :0.0 it means the client will connect to the X server using a unix socket. The advantages of unix sockets are that they are more efficient and access control can be based on uid.

For many years it has been common practice to have TCP disabled by default in X servers and only accept connections over unix sockets. Since you seem to have no specific need for TCP connections I recommend that you just change your command to use unix sockets rather than TCP sockets.

The commands can be abbreviated a little bit as well. This version should work:

DISPLAY=:0 xterm &

Though this command will work, it will not give you exactly the same result as if you had started xterm from the local X session. To achieve the same result you need to ensure that all of the environment variables are the same, not just the DISPLAY variable. Desktop environments tend to set a lot of environment variables.

I find that the easiest way to achieve this is to start a screen command after logging in to a local X session and then attach to this remotely as needed.

So after logging in I open a terminal and type screen -xR somename. Then press ctrla followed by d to detach from it. After logging in remotely I can attach to the screen with screen -xR somename and run the commands I needed such as xterm& in your case. And once I am done again press ctrla followed by d to detach. This is very useful on desktops when I occasionally have needed to restart compiz because it has locked up. I have not yet found any use for this on servers though.

If you didn't start a screen command from the local X session in advance and you need to start an X client on the local display from a remote shell, you need to first set all of the environment variables as in the local X session. To do that you can pick the PID of any of the existing X clients in that session and find all the environment variables like this:

tr '\0' '\n' </proc/$PID/environ

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-12-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。