Linux - X forwarding with sudo

Wednesday Mar 25, 2020

On linux, if we need to ssh to a remote host, sudo and then run something with the UI displayed on our client machine, we have some hoops to jump through:

First, use ssh -X to get to the remote host, this will set a remote DISPLAY env var. Then, after you sudo you have to setup your xauth and display for your new user and you’re all set:

# xauth list $DISPLAY
node01.thegeekdiary.com/unix:10  MIT-MAGIC-COOKIE-1  dacbc5765ec54a1d7115a172147866aa
# echo $DSIPLAY
localhost:10.0
# sudo su - [user]
# xauth add node01.thegeekdiary.com/unix:10  MIT-MAGIC-COOKIE-1  dacbc5765ec54a1d7115a172147866aa
# export DISPLAY=localhost:10.0
# xeyes

The server’s sshd_config has to have X11Forwarding yes turned on for any of this to work, too.