An active SSH X11 display can serve an application run with sudo when it receives the session's authorization cookie and DISPLAY value. The GUI opens on the local X display while running as root on the remote host.
OpenSSH creates a forwarded DISPLAY value such as localhost:10.0 and stores a matching MIT-MAGIC-COOKIE-1 token through xauth. The token sits in the login user's .Xauthority file while an elevated process looks in root's own, so a working ordinary session can still produce "Can't open display" as root.
A root GUI application can modify system files, and X11 access can let it observe or interact with the local display. Use this only with a trusted remote host and an application that needs root privileges.
Ordinary X11 forwarding must already work, with a local X server and xauth plus xclock on the remote host. The remote login needs permission to run the application with sudo.
Related: How to run a GUI application over SSH with X11 forwarding
Related: How to enable or disable X11 forwarding on an SSH server
Steps to run a GUI application with sudo over SSH X11 forwarding:
- Open an interactive forwarded SSH session from your local workstation.
$ ssh -X user@host.example.net
Every remaining command runs on the remote host in this session, which must stay open while the GUI is in use. Use -Y only for trusted servers or applications that require trusted forwarding, which relaxes X11 security restrictions.
- List the cookie for the forwarded display.
$ xauth list "$DISPLAY" host.example.net/unix:10 MIT-MAGIC-COOKIE-1 <cookie>
<cookie> stands in for the real value, which is a credential, so do not share it. An error or an empty listing means ordinary forwarding needs fixing first.
Related: How to enable or disable X11 forwarding on an SSH server - Start a test application with sudo, pointing it at your own cookie file.
$ sudo XAUTHORITY="$HOME/.Xauthority" xclock
Root reads the login user's existing cookie, so nothing is copied and nothing needs cleaning up afterwards. Applications that refuse root execution or need a complete desktop session may require their own supported administration method. Your own program uses the same form:
sudo XAUTHORITY="$HOME/.Xauthority" <application> - Press Ctrl+C in the remote terminal after the clock appears on your local display.