I use MobaXterm as my primary terminal client. Besides some excellent features, it has a selling point - the built-in X server. Yet you may still be in a position where you can't just forward your X windows to your local machine and have to make some extra steps.

Some environments are very restrictive on how you access privileged OS accounts, so you cannot use the application owner's account to access the system but only sudo into it. There are plenty of how-tos regarding forwarding X sessions for sudoers. In general, before switching to the user, you should:  

  • Collect session variable  DISPLAY value
  • Find out your X session authentication  details
  • Switch to the new user with sudo su
  • Replicate your original X session for the new user environment

To automate and simplify this process I create a small Bash script on all VMs where I may need X session and placed it into ~/bin folder.

cat > ~/bin/x-appuser <<EOF
echo -e "Graphical session commands:"
echo -e "===================================="
echo -e "xauth add $(xauth list |tail -1)"
echo -e "export DISPLAY=${DISPLAY}"
sudo su - appowner

chmod u+x ~/bin/x-appuser
X session configuration commands

Now, if you need only terminal session you switch to the appowner account as usual and use this script if you need to run some GUI-based applications.

# Regular user switch
[yourname]$ sudo su - appuser
[appuser]$ exit
# Enable X session forwarding
[yourname]$ x-appuser
Graphical session commands:
xauth add my-vm.domain.name/unix:10 MIT-MAGIC-COOKIE-1 c543177654cc33ee66fffg
export DISPLAY=localhost:3.0
Regular and session with X forwarding

Now, anytime before you want to run xclock, copy and execute those two lines from the command output. with the cookie and display details commands.