using x11 forwarding with ssh and vnc?

12,475

Solution 1

In case we connect to a remote X-Server via VNC we have the advantage that the graphical application will stay running even when the SSH connection to the remote is down. We can then reconnect to resume the graphical application. See also:

To speed up reconnection we may combine the connection to the remote with an SSH session by using vncviewer from tightvncviewer Install tightvncviewer with the option -via. By this we can run on the remote server:

vncserver:0  ## or any other display number e.g. :1

to establish a connection on the viewer via an SSH tunnel:

vncviewer -via user@remote localhost:0

Doing so will need considerably more bandwith because the whole desktop will have to be transmitted from the VNC server. Therefore it may not really be a good idea for low bandwith connections but it may give you a somewhat better experience on high bandwith but unstable connections as compared with X-forwarding.

Solution 2

The command used:

ssh -C -NL 5901:localhost:5901 [email protected] &

forward local 5901 -> remote.cluster:5901

So you can connect to localhost::5901 to connect to the VNC session running on remote.cluster (which you don't have direct access to).

X11 forwarding is different from VNC. You need to use -X, for example

ssh -X [email protected]

Than you will be able to run remote GUI applications on your local machine, provided that you have a X Server running locally (Ubuntu Desktop or Xming on Windows).

Update:

I made a mistake in the forwarding, it should be:

ssh -C -L 5901:localhost:5901 [email protected]

Then you can vnc to localhost:5901, traffic will be forwarded to remote.cluster:5901. Apology for the carelessness...

If no GUI is required, consider using tmux or screen to avoid loss of session.

Share:
12,475

Related videos on Youtube

simona
Author by

simona

Updated on September 18, 2022

Comments

  • simona
    simona over 1 year

    I am working on a remote cluster via ssh with the -X option, because I need to visualize data and graphs, over a vpn protocol. Sometime due to the instability of the internet connection I lose my session. They told me I could use vnc in order to not lose my current session, so that if the internet connection drops I can reconnect and continue with my previously open session.

    What I do is to log in the remote cluster 'remote.cluster' and type

     vncserver :1
    

    then I open another terminal on my system and I type

     ssh -C -NL 5901:remote.cluster:5901 [email protected] &
    

    Then I start vinagre on my system and I connect using ssh protocol. The problem is that I do not have x11 forwarding and I cannot open windows. If I try to connect using the vnc protocol with vinagre it doesn't connect, because I get something as connection timeout.

    What should I do?

  • simona
    simona over 11 years
    I always use ssh -X and it works, the point is that I would like to set up the thing in a way that when my connection drops I can reconnect and find again my previous session. So, are you saying I should use vnc protocol in vinagre to connect to the remote cluster? it does not work: I get `connection to host remote.host was closed'
  • simona
    simona over 11 years
    what I really want to do is, in the link you provide, what michael_n says in the second comment to the first answer, but it doesn't work for me because I am not able to reconnect with screen -r, maybe I am doing something wrong
  • simona
    simona over 11 years
    I did what you say and the vnc is asking for a password, that I didn't set, so should I ask it to the remotehost administrator, right? Another thing: if I use vnc via ssh as above in your answer, will it be slow because all the graphics drawing will be executed on the remote machine rather than my local one?
  • Takkat
    Takkat over 11 years
    Vncviewer -via does ask 1) for the ssh password for the remote and then 2) for the vnc password but the latter only in case it was set on the remote. All drawing will be done on the remote server and then sent as a compressed graphic via the ssh tunnel (see also tightvnc.com/vncviewer.1.php)
  • Terry Wang
    Terry Wang over 11 years
    I updated the reply, I didn't notice a mistake in the forwarding command.
  • simona
    simona over 11 years
    Thank you, I do not really need GUI, just to visualize some plots, can you give me a starting point for screen or tmux, I do not know anything about them
  • simona
    simona over 11 years
    Thank you for your solution. Because it was not clear to me when I read your solution, I would like to point out to future readers that that the only necessary commands are vncserver :1 and vncviewer -via user@remote localhost:1. Btw the graphics visualization is very fast, I am surprised that it is actually riun on the remote cluster and not on my local machine
  • Terry Wang
    Terry Wang over 11 years
    I personally prefer tmux, you can use screen or byobu (Ubuntu enhanced screen), similar. Running tmux on the remote server is basically running a daemon/session on that box, so that when you get disconnected, you will be able to reconnect via SSH and attach to that session - tmux attach without losing what you were working on in terminal. A sample .tmux.conf gist.github.com/3950393
  • Takkat
    Takkat over 11 years
    @simona: thank you for pointing at this. Hope with the edit it's clearer now.