Search This Blog

Monday, October 7, 2013

Ubuntu: Using VNC over SSH

Recently my inherent laziness caused me to start researching a remote desktop like environment from my laptop to my Xubuntu media server.  I wanted to do this so that while sitting in one part of the house, I could remote in to my media server in the back room and rearrange files, perform updates, etc. Essentially, I wanted something to work as a headless environment from the comfort of my couch.

In order to remotely control the desktop, I needed to setup some sort of secure environment.  We all know that VNC by default is not secure.  RDP (Remote Desktop Protocol), mainly used for accessing Windows machines, also has it's flaws.  I started looking into VNC (Virtual Network Computing) over SSH (Secure Shell) tunneling.  My client computer (laptop) is Ubuntu 13.04 and the server I wish to connect to is Xubuntu 13.04.  Please do note that since I'm using Xubuntu, the following instructions may not work for you if you don't use lightdm as a window manager.


  1. Start by installing ssh server on the remote machine:
    1. sudo apt-get install openssh-server
    2. If you are using Webmin to configure your system (highly recommended, see here), it is a fairly simple setup after the install completes.  The default setup will work for now, but we need to lock it down.  More on that in a second.
    3. If you are running Ubuntu on your local machine, you already have the openssh client installed by default.  I cannot speak for Windows, OS X, or other flavors of Linux, but finding a package should be pretty simple using the great interwebs.
    4. In the terminal on your client computer (ie your laptop), generate ssh public and private keys:
      1. mkdir ~/.ssh
      2. chmod 700 ~/.ssh
      3. ssh-keygen -t rsa
      4. You will be prompted for a location to save the keys, and a passphrase for the keys. This passphrase will protect your private key while it's stored on the hard drive and be required to use the keys every time you need to login to a key-based system.
      5. If you do not wish to password-protect your key file (not recommended), just press enter without typing a password.  Remember, if your laptop is ever stolen, a brute force attempt may be made to unlock your key file, and then the server you connect to can be compromised.
      6. Note: Public keys are what you give out to servers.  It is what is used in conjunction with your private key - stored locally - to authenticate.  Under no circumstances should you give out your private key!
    5. Now that you have generated your public and private SSH keys, it's time to transfer your public key to the server:
      1. ssh-copy-id username@host
      2. Replace username with the username you login with on the server.  Use the server's local IP address (assuming you're doing this over a LAN) as the host.
      3. When prompted for the password, enter the password associated with the username you provided for that machine.
      4. For more details on steps 4 and 5 above, jump on over to SSH Keys on the Ubuntu Community website.
    6. Great!  You're on your way to having a secure shell environment that's actually secure.  Now, we must proceed with locking down openssh-server:
      1. In Webmin, login to the server and find the SSH Server section.
        1. If you find that SSH Server is in the "Unused" section, click Refresh Modules on the left, bottom.  Now logout and log back in.  You will now find it under the "Servers" section.
      2. Authentication:
        1. Allow authentication by password? No
        2. Permit logins with empty passwords? No
        3. Allow login by root? No
        4. Allow RSA (SSH 1) authentication? No
        5. Allow DSA (SSH 2) authentication? No
        6. Check permissions on key files? Yes
        7. Display /etc/motd at login? No
        8. Ignore users' known_hosts files? No
        9. User authorized keys file: Default
        10. Maximum login attempts per connection: 2
        11. Ignore .rhosts files? Yes
      3. Networking:
        1. Listen on addresses: All addresses
          1. If you have a dedicated IP address for your client, setting this to it's IP would make it even more secure, allowing only connections within the local network.
        2. Listen on port: 22
          1. 22 is the default port for SSH.  If you wish to change this for more security, connecting will become more difficult.
        3. Accept protocols: SSH v2
          1. With modern SSH clients there is no need to enable v1.  In fact, there are known vulnerabilities in older SSH servers, including a CRC32 Compensation Attack.
        4. Disconnect if client has crash? Yes
        5. Time to wait for login? 120 seconds
        6. Allow TCP fowarding? Yes
          1. This sounded insecure to me at first.  But, after further research, it actually encapsulates any traffic based on TCP into the SSH tunnel, making insecure traffic (checking mail, surfing the web) secure.  However, if you're a LAN admin and have other security restrictions in place for network traffic, enabling TCP forwarding would allow one to bypass those restrictions.
        7. Allow connection to forwarded ports? No
      4. Client-Host Options:
        1. If you have not tweaked this section before, the only available option will be All Hosts
        2. Click the Add options for client host link at the bottom.
        3. Enter the host name or IP address of the server
          1. "*" can be used for host names.  ie *.foo.com will allow SSH to anything on the foo.com domain.
        4. Compression level: Worst
          1. Setting it to anything else will consume unneeded CPU cycles on a fast network and actually slow down file transfers when using scp
        5. Use privileged source port? No
          1. By default SSH clients will use the privileged source port when connecting, which indicates to the server that it is a trusted program and thus can be relied on to provide correct information about the user running it. This is necessary for rlogin-style authentication to work, but unfortunately many networks have their firewalls configured to block connections with privileged source ports, which completely blocks SSH. To have the clients use a normal port instead, select No for the Use privileged source ports? field. Unless you are using host-based authentication, this will cause no harm.
        6. All other options can be left as default.
      5. Access Control:
        1. Select the users you want to allow to connect, or type them in using commas to separate. "?" can be used as a wildcard.  ie admin_? will allow any users starting with admin_ to connect.
      6. Once you have made all of these changes, Stop Server and Start Server from the module's index page to apply the changes.
    7. SSH-server is now configured and locked down using Webmin, you have generated and published your keys from the client to the server, and are ready to move on to configuring VNC over SSH.  But first we must verify that all the changes just made didn't break our SSH connection
      1. ssh user@host
      2. You will be prompted with something like:
      3. The authenticity of host '10.0.X.XX (10.0.X.XX)' can't be established.
        ECDSA key fingerprint is XX:XX...XX.XX.
        Are you sure you want to continue connecting (yes/no)?
      4. Note: Don't panic!  You are connecting to an "unknown" server using only your key for the first time.  Unless some hacker is really efficient at setting up a man-in-the-middle attack on your server between the time you installed the ssh-server to now, you are most likely connecting to what you intended to connect to.
      5. Type yes
      6. Warning: Permanently added '10.0.X.XX' (ECDSA) to the list of known hosts.
        Permission denied (publickey).
        1. I didn't expect this error.  Upon exiting and logging in again, all was well with the world and I did not receive the same error.
      7. Once you have established a connection successfully, we can move on.  Type exit and execute until you are cleared away from the SSH session.
    8. We will log back into the remote shell, but this time we will use trusted X11 forwarding (-Y option) in order to use a graphical text editor.
      1. ssh -Y user@hostname 
      2. x11vnc -storepasswd
        1. Enter a secure password, and again to verify it.
        2. Store this password as /etc/x11vnc.pass (not the default location)
        3. sudo chmod 744 /etc/xllvnc.pass
      3. cd /etc/lightdm
      4. sudo gedit lightdm.conf
        1. Assuming gedit is installed on your machine.  If not, use whatever text editor is installed, or vi if you are comfortable with that.
      5. Append the last line to your file, so that it looks like this:
        1. Save and close
        2. Note: If this step were skipped, x11vnc will not start after rebooting your server and must be manually started after logging in locally.  Some may wish to do this as an extra layer of security.
      6. sudo service lightdm restart
      7. Now the service is running and will run each time your computer starts.
    9. Back on your local computer (outside the SSH session), you will want to bring the remote display to you:
      1. Install SSVNC from the Ubuntu software center for your local VNC viewer.
      2. In the VNC Host:Display field:
        1. username@host
        2. I found it to work best if you let the software decide the display port rather than attempting to connect to username@host:displayPort (ie benmctee@10.0.X.XX:0 as the instructions tell you to do).
        3. Enter your VNC password that you set above when prompted.
  2. Assuming you have not encountered any errors - as I did while writing this - you should be viewing your server remotely!
    1. To stretch / shrink the display:
      1. F8 > Scale Viewer > auto or fit depending on your preference.
    2. Full screen view:
      1. F9
    3. To close the remote viewer but leave you logged in on the server, simply close the window.  If you wish to log out, do that normally as well.
As with all my tutorials, if you have questions or run into issues, please let me know in the comments section below.
References:
http://www.cs.utexas.edu/users/habals/blog/index.php/archives/22
http://askubuntu.com/questions/304017/how-to-set-up-remote-desktop-through-ssh
https://help.ubuntu.com/community/VNC/Servers
https://help.ubuntu.com/community/SSH/OpenSSH/Keys
http://doxfer.webmin.com/Webmin/SSHServer
http://kotechnology.blogspot.com/2011/12/xubuntu-1110-vnc-server.html
http://ubuntuforums.org/showthread.php?t=1860295
http://mlepicki.com/2011/10/remote-vnc-login-to-ubuntu-11-10/