作者:yongjian 虽然现在多用sudo来执行root权限,但有时在某些情况下还是会用到su, 特别是在远程没有sudo配置的环境。这样,在转换到另外一个用户后(多数是root), X authentication的信息就丢失了。自己写了一个脚本可以使到su到另外一个用户后仍能保持X权限, 运行X apps.
代码: #!/bin/sh # # ------------------------------------------------------------------------- # Script Name: sussh # Author: yongjian Xu - i3dmaster # Updated: 04/25/2005 # # Description: # Allows one to su to another user within a SSH session and still be # able to launch X11 apps. # -------------------------------------------------------------------------
# Check to see if a user id was supplied on the command line case $# in 1) uid="$1" ;; *) echo "Usage: $0 <username>" exit 1 ;; esac
# Check if $DISPLAY is set if [[ $DISPLAY = "" ]]; then echo "The DISPLAY environment variable must be defined." echo "You may need to turn on X11 forwarding in your SSH client." exit 1 fi
if [[ $(echo $DISPLAY | grep "localhost") != "" ]]; then DISPORT=$(echo $DISPLAY | awk -F: '{print $2}'| awk -F. '{print $1}') NEWDISPORT="$(uname -n)/unix:$DISPORT" else NEWDISPORT=$DISPLAY fi
XAUTH="/usr/bin/X11/xauth" # Set the location of xauth ARG=$($XAUTH list $NEWDISPORT) # Get the X11 magic cookie
su - $uid -c "$XAUTH add $ARG; \ DISPLAY=$DISPLAY; \ SSH_TTY=$SSH_TTY; \ SSH_CLIENT='$SSH_CLIENT'; \ export DISPLAY SSH_TTY SSH_CLIENT; \ $SHELL"
exit 0 |