为 X-11 转发编写 bash 脚本

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

Writing bash script for X-11 forwarding

bashunix

提问by Bruce

I was having problem with SSH X-11 forwarding while I used sudo. I found a solution for it.

我在使用 sudo 时遇到了 SSH X-11 转发问题。我找到了解决方案。

$hostname
server4.a.b.edu

First I do:

首先我做:

$ echo $DISPLAY
localhost:10.0

then

然后

$ xauth list
server1.a.b.edu/unix:12  MIT-MAGIC-COOKIE-1  6026864294a0e081ac452e8740bcd0fe
server4.a.b.edu/unix:10  MIT-MAGIC-COOKIE-1  f01fbfe0c0d68e30b45afe3829b27e58

Then I need to do

然后我需要做

$ sudo xauth add server4.a.b.edu/unix:10  MIT-MAGIC-COOKIE-1  f01fbfe0c0d68e30b45afe3829b27e58

for sudo to work, for the cookie with my server name and display.

为了 sudo 工作,为了使用我的服务器名称和显示的 cookie。

How do I write a bash script to automate this?

我如何编写一个 bash 脚本来自动化这个?

采纳答案by evil otto

You shouldn't need a script at all; it's just a single command.

你根本不需要脚本;这只是一个命令。

sudo xauth add `xauth list $DISPLAY`

回答by Joao Gustavo

One thing that worked on RHEL6 was exporting the XAUTHORITY variable before sudoing, for example:

在 RHEL6 上工作的一件事是在执行 sudo 之前导出 XAUTHORITY 变量,例如:

export XAUTHORITY=~/.Xauthority
sudo xclock

回答by Nicholas Sushkin

I do

我愿意

sudo su otheruser -c "xauth add $(xauth list :${DISPLAY##*:}); xterm"

Replace xterm with the GUI command you want to run

将 xterm 替换为您要运行的 GUI 命令

回答by Jonas Berlin

I use this after I've logged in with ssh:

我在使用 ssh 登录后使用它:

echo -n "xauth add `xauth list :${DISPLAY#*:}`" | sudo su - otheruser
sudo su - otheruser
echo -n "xauth remove :${DISPLAY#*:}" | sudo su - otheruser

Basically it strips out the hostname part of the display. xauthstill finds it when used like this.

基本上它去掉了显示的主机名部分。xauth像这样使用时仍然可以找到它。

It works even if you don't have sudo permissions for any other command than "su - otheruser". It also removes the added cookie afterwards for increased security and cleanliness. :)

即使您没有“ su - otheruser”以外的任何其他命令的 sudo 权限,它也能工作。之后它还会删除添加的 cookie,以提高安全性和清洁度。:)

You can replace the sudo su - otherusercommand in the middle with any sudo command variant you like.

您可以将sudo su - otheruser中间的命令替换为您喜欢的任何 sudo 命令变体。