Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

How do I reduce these two commands to one? I am connecting from my client to an x11vnc server and the two commands below already work. I just wish to do it in one step:

first command:

ssh -fNL 5901:localhost:5678 -i ~/.ssh/some_id_rsa

and second:

vncviewer localhost:5901

From reading the man page, it seems like the -via option might do it. Unfortunately, the man page leaves me very confused. For reference (not that I understand it) here is what my man page says:

   -via gateway
          Automatically create encrypted TCP tunnel to the gateway machine before con‐
          nection, connect to the host through  that  tunnel  (TightVNC-specific).  By
          default,  this  option  invokes SSH local port forwarding, assuming that SSH
          client binary can be accessed as /usr/bin/ssh. Note that when using the -via
          option,  the  host  machine name should be specified as known to the gateway
          machine, e.g.  "localhost"  denotes  the  gateway,  not  the  machine  where
          vncviewer  was  launched.  The environment variable VNC_VIA_CMD can override
          the            default             tunnel             command             of
          /usr/bin/ssh -f -L "$L":"$H":"$R" "$G" sleep 20.  The tunnel command is exe‐
          cuted with the environment variables L, H, R, and G taken the values of  the
          local  port number, the remote host, the port number on the remote host, and
          the gateway machine respectively.
share|improve this question

1 Answer 1

up vote 2 down vote accepted

This is what the man page is trying to say. I have the following setup.

  vncviewer         .-,(  ),-.    
   __  _         .-(          )-.           gateway           vncserver 
  [__]|=|  ---->(    internet    )-------> __________ ------> ____   __ 
  /::/|_|        '-(          ).-'        [_...__...°]       |    | |==|
                     '-.( ).-'                               |____| |  |
                                                             /::::/ |__|

NOTE: The above diagram was done using asciio.

The vncviewer is running from my laptop. From my laptop I can run the following command and connect to the vncserver which is behind my router:

$ vncviewer vncserver_host:0 -via

This will instantly connect me to the vncserver. This command is shown on my laptop which helps to show what the man page is trying to explain:

/usr/bin/ssh -f -L 5599:vncserver_host:5900 sleep 20

This is the command that vncviewer is automatically constructing when you use the -via gateway switch.

including ssh configurations

You can make use of the ~/.ssh/config file and put entries in this file like this:

Host *
IdentityFile ~/.ssh/id_dsa

Or you can target a specific host like this:

Host mygateway
    User sam
    IdentityFile ~/.ssh/someother_id_rsa

This will allow you to leverage the Host entries in this file like this:

$ vncviewer vncserver_host:0 -via mygateway
share|improve this answer
Thank you. That's awesome ascii art! However, one of the things I could not figure out is where to put the -i option (my id_rsa key). Another issue is specifying the ports... I'm still not clear on those from your answer. – MountainX Jul 21 '13 at 1:44
@MountainX - I would create an entry in the ~/.ssh/config file and include it there. You can either do it per Host entry in the file (IdentityFile ~/.ssh/id_rsa) or you can create a Host * entry and put it under there. – slm Jul 21 '13 at 1:47
@MountainX - see updates, let me know if that makes sense. – slm Jul 21 '13 at 1:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.