sudo X11 application over ssh
To run any console-based programs remote, you typically just:
- ssh to remote machine
- sudo whatever-console-program
To run X11 application as the ssh user, it is also straight-forward:
- ssh to remote machine
- whatever-x11-application
It gets a bit complicated when you need to do both - to run an X11 application as root user on a remote machine. In my case, I wanted to run gparted
as root on a remote linux machine.
-
Enable X11 forwarding on remote machine, /etc/ssh/sshd_config
# Ensure that this line is present # If you had to modify the file, then you will have to restart sshd X11Forwarding yes
-
Start ssh session to remote machine with
-X
optionssh -X -C NON_ROOT_USER@remote_machine # Ensure X11 forwarding succeeded echo $DISPLAY
-
Swith remote user to root in the ssh session
# Let the new shell inherit some environment variable crucial for X11/Wayland to work sudo -EH -s
-
Check connection access. The error is expected.
xhost X11 connection rejected because of wrong authentication
-
Add NON_ROOT_USER credentials to root user /.Xauthority
xauth add $(xauth -f ~NON_ROOT_USER/.Xauthority list | tail -1) xhost # Output message may vary access control enabled, only authorized clients can connect # Voila! Now you can run your X11 application
Remove access credentials
xauth remove $DISPLAY