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

来源:互联网 发布:计算器编程学习 编辑:程序博客网 时间:2024/06/05 00:58

如果你是一个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

原创粉丝点击