Linux 如何为当前会话自动设置 $DISPLAY 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1057343/
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
How do i automatically set the $DISPLAY variable for my current session
提问by
I see that $display is set to localhost:0,0 if i am running over a vnc server this may not be correct, is there a way to automatically set it in my login script?
我看到 $display 设置为 localhost:0,0 如果我在 vnc 服务器上运行这可能不正确,有没有办法在我的登录脚本中自动设置它?
回答by Jonathan
do you use Bash? Go to the file .bashrc in your home directory and set the variable, then export it.
你使用 Bash 吗?转到主目录中的文件 .bashrc 并设置变量,然后将其导出。
DISPLAY=localhost:0.0 ; export DISPLAY
显示=本地主机:0.0;出口显示
you can use /etc/bashrc if you want to do it for all the users.
如果您想为所有用户执行此操作,则可以使用 /etc/bashrc。
You may also want to look in ~/.bash_profile and /etc/profile
您可能还想查看 ~/.bash_profile 和 /etc/profile
EDIT:
编辑:
function get_xserver ()
{
case $TERM in
xterm )
XSERVER=$(who am i | awk '{print $NF}' | tr -d ')''(' )
XSERVER=${XSERVER%%:*}
;;
aterm | rxvt)
;;
esac
}
if [ -z ${DISPLAY:=""} ]; then
get_xserver
if [[ -z ${XSERVER} || ${XSERVER} == $(hostname) || \
${XSERVER} == "unix" ]]; then
DISPLAY=":0.0" # Display on local host.
else
DISPLAY=${XSERVER}:0.0 # Display on remote host.
fi
fi
export DISPLAY
回答by Nifle
You'll need to tell your vnc client to export the correct $DISPLAY once you have logged in. How you do that will probably depend on your vnc client.
登录后,您需要告诉您的 vnc 客户端导出正确的 $DISPLAY。您如何执行此操作可能取决于您的 vnc 客户端。
回答by Tanktalus
I'm guessing here, based on issues I've had in the past which I did solve:
我在这里猜测,基于我过去确实解决过的问题:
- you're connecting to a vnc server on machine B, displaying it using a VNC client on machine A
- you're launching a console (xterm or equivalent) on machine B and using that to connect to machine C
- you want to launch an X-based application on machine C, having it display to the VNC server on machine B, so you can see it on machine A.
- 您正在连接到机器 B 上的 vnc 服务器,使用机器 A 上的 VNC 客户端显示它
- 你在机器 B 上启动一个控制台(xterm 或等效的)并使用它连接到机器 C
- 你想在机器 C 上启动一个基于 X 的应用程序,让它显示到机器 B 上的 VNC 服务器,这样你就可以在机器 A 上看到它。
I ended up with two solutions. My original solution was based on using rsh. Since then, most of our servers have had ssh installed, which has made this easier.
我最终得到了两个解决方案。我最初的解决方案是基于使用 rsh。从那时起,我们的大多数服务器都安装了 ssh,这使这变得更容易。
Using rsh, I put together a table of machines vs OS vs custom options which would guide this process in perl. Bourne shell wasn't sufficient, and we don't have bash on Sun or HP machines (and didn't have bash on AIX at the time - AIX 5L wasn't out yet). Korn shell wasn't much of an option, either, since most of our Linux boxes don't have pdksh installed. But, if you don't face these limitations, you can implement the idea in ksh or bash, I think.
使用 rsh,我将机器 vs 操作系统 vs 自定义选项放在一起,这将在 perl 中指导这个过程。Bourne shell 还不够,我们在 Sun 或 HP 机器上没有 bash(当时在 AIX 上没有 bash - AIX 5L 还没有发布)。Korn shell 也不是一个很好的选择,因为我们的大多数 Linux 机器都没有安装 pdksh。但是,如果您不面对这些限制,我认为您可以在 ksh 或 bash 中实现该想法。
Anyway, I would basically run 'rsh $machine -l $user "$cmd"' where $machine, of course, was the machine I was logging in to, $user, similarly obvious (though when I was going in as "root" this had some variance as we have multiple roots on some machines for reasons I don't fully understand), and $cmd was basically "DISPLAY=$DISPLAY xterm", though if I were launching konsole, for example, $cmd would be "konsole --display=$DISPLAY". Since $DISPLAY was being evaluated locally (where it's set properly), and not being passed literally across rsh, the display would always be set correctly.
无论如何,我基本上会运行 'rsh $machine -l $user "$cmd"' 其中 $machine,当然,是我登录的机器,$user,同样明显(尽管当我以“root “这有一些差异,因为我们在某些机器上有多个根,原因我不完全理解),并且 $cmd 基本上是“DISPLAY=$DISPLAY xterm”,但如果我启动 konsole,例如,$cmd 将是“konsole --display=$DISPLAY”。由于 $DISPLAY 是在本地进行评估(在正确设置的地方),而不是通过 rsh 逐字传递,因此显示将始终正确设置。
I also had to make sure that no one did anything silly like reset DISPLAY if it was already set.
我还必须确保没有人做任何愚蠢的事情,例如如果已经设置了重置显示。
Now, I just use ssh, make sure that X11Forwarding is set to yes on the server (sshd_config), and then I can just ssh to the machine, let X commands go across the wire encrypted, and it'll always go back to the right place.
现在,我只使用 ssh,确保服务器上的 X11Forwarding 设置为 yes(sshd_config),然后我就可以 ssh 到机器,让 X 命令通过加密的线路,它总是会回到正确的位置。
回答by neuro
Your vncserver have a configuration file somewher that set the display number. To do it automaticaly, one solution is to parse this file, extract the number and set it correctly. A simpler (better) is to have this display number set in a config script and use it in both your VNC server config and in your init scripts.
您的 vncserver 有一个配置文件,用于设置显示编号。要自动执行,一种解决方案是解析此文件,提取数字并正确设置。更简单(更好)的是在配置脚本中设置此显示编号,并在 VNC 服务器配置和初始化脚本中使用它。
回答by Nick
Here's something I've just knocked up. It inspects the environment of the last-launched "gnome-session" process (DISPLAY is set correctly when VNC launches a session/window manager). Replace "gnome-session" with the name of whatever process your VNC server launches on startup.
这是我刚刚敲的东西。它检查上次启动的“gnome-session”进程的环境(当 VNC 启动会话/窗口管理器时正确设置了 DISPLAY)。将“gnome-session”替换为您的 VNC 服务器在启动时启动的任何进程的名称。
PID=`pgrep -n -u $USER gnome-session`
if [ -n "$PID" ]; then
export DISPLAY=`awk 'BEGIN{FS="="; RS="##代码##"} =="DISPLAY" {print ; exit}' /proc/$PID/environ`
echo "DISPLAY set to $DISPLAY"
else
echo "Could not set DISPLAY"
fi
unset PID
You should just be able to drop that in your .bashrc file.
您应该能够将其放入 .bashrc 文件中。