Start GUI program on the remote machine using your local display through ssh

Author:

This article is probably not only for absolute beginners, so many Linux guys do not know they can just execute a GUI program on their remote machine but the output of the graphics to be drawn in the local display, it’s like your local display is connected to the remote computer through the network. And you do not need any special software like VNC Client/Server, TeamViewer – all you need is the OpenSSH package you probably have when managing a remote machine. And it is absolutely secure because all the communication is performed within ssh.
Suppose you have a machine, which has some GUI capabilities (you know if we speak for servers, not all servers need the GUI, but in some cases we need it) and you want to execute a graphical program on the remote.
Here is how you can login to be sure you can execute the program remotely:

ssh -o ForwardX11=yes -o ForwardAgent=yes -o ForwardX11Trusted=yes [USER]@[IP]

And you can also execute a remote command with the ssh:

ssh -o ForwardX11=yes -o ForwardAgent=yes -o ForwardX11Trusted=yes myuser@192.168.0.245 'virtualbox'

This command will execute virtualbox on your remote machine with login myuser@192.168.0.245 and the output the display on your local machine.

Here are some examples. Gentoo Desktop using KDE with konsole get to the remote machine on our local network 198.160.245:

SCREENSHOT 1) Login in your remote machine and there type firefox. The firefox will be executed on the remote machine, but the graphics will be output on your local display.

main menu
Remote execute Firefox through ssh

SCREENSHOT 2) Login in your remote machine and there type virtualbox. The VirtualBox will be exeucted on the remote machine. You can see we started a virtual machine in our virtualbox program.

main menu
Remote execute VirtualBox through ssh

SCREENSHOT 3) While still logged in the remote machine you can execute gnome and even native gnome programs. Here we executed the Gnome Software center. You may install a programe and it will be installed on the remote machine.

main menu
Remote execute Gnome software through ssh

SCREENSHOT 4) nautilus started and you can manage your files.

main menu
Remote execute Gnome nautilus through ssh

* Troubleshooting 1

The following error

X11 forwarding request failed on channel 0

In recent OpenSSH versions the default configuration of the server forbids the x11 forward, so explicitly enable it in the server’s configuration file:

X11Forwarding yes

After enabling this option in the server’s configuration and restarting the sshd service, the error disappears.

* Troubleshooting 2

You may encounter an error similar to:

No xauth program.

You should install xauth on the server:

      Ubuntu

      apt install xauth
      
      CentOS 7

      yum install xorg-x11-xauth
      
      Gentoo

      emerge -va x11-apps/xauth
      

    * Bonus – set ssh configuration

    Of course you can set these options in your client ssh configuration file in “~/.ssh/config”.
    Create a file “~/.ssh/config” and put these configs:

    mkdir -p ~/.ssh/
    chmod 700 ~/.ssh
    touch ~/.ssh/config
    echo <<< EOL
    Host rexec 192.168.0.245
        HostName 192.168.0.245
        Port 22
        User myuser
        ForwardX11=yes
        ForwardAgent=yes
        ForwardX11Trusted=yes
    #    IdentityFile ~/.ssh/personal-ident
    EOL >> ~/.ssh/config
    

    You could uncomment the IdentityFile if you set to use keys for password-less login. (Beyond the scope of this article).
    And then you could just type in your local console OR in your favorite GUI->run command, or even you can create a menu item to execute command (so next time you just click with the mouse on the icon in your Desktop/menu) like this:

    ssh rexec virtualbox
    

    To execute remotely virtualbox and to show the graphics on your local display.

Leave a Reply

Your email address will not be published. Required fields are marked *