如何修复su时丢失的X11验证信息(xsudo bash版)

如果你是一个sudoer,却不知道root的密码,那么你在运行某些需要特别权限的X程序的时候可能会遇到以下的类似信息:

[pc01]> sudo ethereal
ssh(28492) X11 connection rejected because of wrong authentication.
The application 'ethereal' lost its connection to the display localhost:11.0;
most likely the X server was shut down or you killed/destroyed
the application.
[pc01]> sudo su
[pc01]> whoami
root
[pc01]> ethereal
ssh(28492) X11 connection rejected because of wrong authentication.
The application 'ethereal' lost its connection to the display localhost:11.0;
most likely the X server was shut down or you killed/destroyed
the application.

该问题在需要登录到远程机器上时尤其突出(当然,ssh需要支持X11 forward)。

出现该错误的原因是:X11验证机制是通过COOKIE来完成的,而这些COOKIE在默认情况下只应用于一个用户。因此在使用su或者sudo改变euid以后,原有的X11验证信息已经无效,从而无法运行程序。

为了避免这个问题,可以采取以下方法。

[pc01]  >  echo $DISPLAY 
localhost:
10.0
[pc01] 
>  xauth list  | grep : 10
pc01.xx.
/ unix: 10   MIT - MAGIC - COOKIE - 1   10866d9259e65500229ff48344df0371
[pc01] 
>  sudo su  -
[root@pc01 
~ ]# xauth add pc01.xx. / unix: 10   MIT - MAGIC - COOKIE - 1   10866d9259e65500229ff48344df0371
[root@pc01 
~ ]# ethereal

此法将原有的X11验证信息传递给新的用户,重新建立了和Xserver的连接,从而解决了X11验证信息在su过程中丢失的问题。

为了简化此过程,可以写一个脚本自动实现此功能,不妨称其为xsudo:

# !/ bin / bash

#       xsudo ...
#

Usage()
{
        echo 
"xsudo to forward X11 authentication when sudo, almost the same as 
sudo"
}


if  [ $#  - lt  1  ]; then
        Usage
        exit 
0
fi

host
= $HOST
display
= $DISPLAY
port
= `echo $DISPLAY  | cut  - d :  - 2   | cut  - d .  - 1 `

key
= `xauth list |  grep $host  | grep  " :$port  "   | grep unix`
sudo su 
- " xauth add $key && $* "

将xsudo添加到path,就可以很方便地随意sudo了:D

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值