Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Failed to open the X11 display running vbox via sudo[solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Desktop Environments
View previous topic :: View next topic  
Author Message
DaggyStyle
Advocate
Advocate


Joined: 22 Mar 2006
Posts: 4883

PostPosted: Mon May 27, 2013 6:08 pm    Post subject: Failed to open the X11 display running vbox via sudo[solved] Reply with quote

Hello, I have a script that run virtualbox as a specific user.

think is that when I try to run it I get the following:
Code:
                         
starting virtualbox...                                                                                     
No protocol specified
Failed to open the X11 display!
shutting down virtualbox...


here is my relevant /etc/sudoers:
Code:
# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
# Failure to use 'visudo' may result in syntax or file permission errors
# that prevent sudo from running.
#
# See the sudoers man page for the details on how to write a sudoers file.
#

# Host alias specification

# User alias specification

# Cmnd alias specification

# Defaults specification
Defaults env_reset
Defaults:%virt_users env_keep = "VIRT_SW VIRT_IMG DISPLAY USER HOME XAUTHORITY"
# Runas alias specification

# User privilege specification
root    ALL=(ALL) ALL

# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL) ALL

# Same thing without a password
# %wheel        ALL=(ALL) NOPASSWD: ALL

# Samples
# %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users  localhost=/sbin/shutdown -h now
%virt_users ALL = (virt_admin) NOPASSWD: /home/virt_admin/exec_vm.sh


where when I run the command, I run it as follows:
Code:
#!/bin/bash -x

DIR="$(echo ~virt_admin)"
SCRIPT_NAME="${DIR}/exec_vm.sh"
sudo -u $(stat --printf="%U" ${SCRIPT_NAME}) ${SCRIPT_NAME}

any ideas why I get that error and how to solve this?

Thanks,
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
ProjectFootball


Last edited by DaggyStyle on Wed May 29, 2013 12:33 pm; edited 2 times in total
Back to top
View user's profile Send private message
blakdeath
Guru
Guru


Joined: 21 Sep 2012
Posts: 392

PostPosted: Tue May 28, 2013 8:05 am    Post subject: Reply with quote

take a look at this link
I think it might be useful
http://blog.fox.geek.nz/2012/10/granting-root-access-to-all-xorg-x11.html
Back to top
View user's profile Send private message
DaggyStyle
Advocate
Advocate


Joined: 22 Mar 2006
Posts: 4883

PostPosted: Tue May 28, 2013 7:56 pm    Post subject: Reply with quote

blakdeath wrote:
take a look at this link
I think it might be useful
http://blog.fox.geek.nz/2012/10/granting-root-access-to-all-xorg-x11.html


Solved! thanks for the tip, here is what I've done:
  • added the following lines to exec_vm.sh:
    Code:
    X_AUTH_FILE="$(find /var/run/xauth/ -name "A${DISPLAY}-*" -group root)"
    X_COOKIE_TOKEN="$(sudo /usr/local/bin/get_X_auth_cookie.sh ${X_AUTH_FILE})"

    also
    Code:

    xauth add ${DISPLAY} MIT-MAGIC-COOKIE-1 ${X_COOKIE_TOKEN}
    # run cmd
    xauth remove ${DISPLAY}

  • created /usr/local/bin/get_X_auth_cookie.sh with the following content:
    Code:
    #!/bin/bash

    AUTHFILE="$1"

    echo "$(xauth -f "$AUTHFILE"  nlist | cut -d" " -f 9)"

  • modified /etc/sudoers to the following:
    Code:
    # sudoers file.
    #
    # This file MUST be edited with the 'visudo' command as root.
    # Failure to use 'visudo' may result in syntax or file permission errors
    # that prevent sudo from running.
    #
    # See the sudoers man page for the details on how to write a sudoers file.
    #

    # Host alias specification

    # User alias specification

    # Cmnd alias specification

    # Defaults specification
    Defaults env_reset
    Defaults:%virt_users env_keep = "VIRT_SW VIRT_IMG DISPLAY"
    # Runas alias specification

    # User privilege specification
    root    ALL=(ALL) ALL

    # Uncomment to allow people in group wheel to run all commands
    # %wheel        ALL=(ALL) ALL

    # Same thing without a password
    # %wheel        ALL=(ALL) NOPASSWD: ALL

    # Samples
    # %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
    # %users  localhost=/sbin/shutdown -h now
    virt_admin ALL = NOPASSWD: /sbin/modprobe, /usr/bin/rc-config, /usr/bin/virsh, /usr/local/bin/get_X_auth_cookie.sh
    %virt_users ALL = (virt_admin) NOPASSWD: /home/virt_admin/exec_vm.sh


and viola, it works :)
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
ProjectFootball
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Desktop Environments All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum