如何为当前会话自动设置$ DISPLAY变量

时间:2022-10-31 19:09:51

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服务器上运行这可能不正确,有没有办法在我的登录脚本中自动设置它?

5 个解决方案

#1


8  

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

DISPLAY = localhost: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

#2


7  

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="\0"}  $1=="DISPLAY" {print $2; 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文件中。

#3


1  

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
  • 您正在连接到机器B上的vnc服务器,使用机器A上的VNC客户端显示它
  • you're launching a console (xterm or equivalent) on machine B and using that to connect to machine C
  • 你正在机器B上启动一个控制台(xterm或同等版本)并使用它来连接到机器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.
  • 你想在机器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,我将一个机器与操作系统表和自定义选项放在一起,这将在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.

我还必须确保没有人做任何愚蠢的事情如重置DISPLAY,如果它已经设置。

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,确保在服务器(sshd_config)上将X11Forwarding设置为yes,然后我可以ssh到机器,让X命令通过加密的线路,并且它将始终返回到正确的位置。

#4


1  

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服务器配置和init脚本中使用它。

#5


0  

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客户端。

#1


8  

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

DISPLAY = localhost: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

#2


7  

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="\0"}  $1=="DISPLAY" {print $2; 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文件中。

#3


1  

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
  • 您正在连接到机器B上的vnc服务器,使用机器A上的VNC客户端显示它
  • you're launching a console (xterm or equivalent) on machine B and using that to connect to machine C
  • 你正在机器B上启动一个控制台(xterm或同等版本)并使用它来连接到机器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.
  • 你想在机器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,我将一个机器与操作系统表和自定义选项放在一起,这将在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.

我还必须确保没有人做任何愚蠢的事情如重置DISPLAY,如果它已经设置。

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,确保在服务器(sshd_config)上将X11Forwarding设置为yes,然后我可以ssh到机器,让X命令通过加密的线路,并且它将始终返回到正确的位置。

#4


1  

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服务器配置和init脚本中使用它。

#5


0  

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客户端。