Monday, January 14, 2013

VNC server for headless Ubuntu 12.04 and VNC client for Windows 7 - success!

Objective: To set up a GUI client on Windows 7 to manage my Ubuntu server.

The server is a headless Ubuntu 12.04 box on Amazon Web Services. The client machine is my Windows 7 laptop.

I found lots of advice about GUI interfaces for Ubuntu, but none of it worked in my situation. I encountered problems such as:

  • It was often suggested to use xRDP as the server and the built in Windows Remote Desktop as the client. That would be great if it worked, but I just couldn't get it to work. Once in a while, I could log in, but most of the time I got password-related errors.
  • A lot of the advice was for old  versions of Ubuntu.
  • The advice assumed a GUI was already installed on the server, that is, it was Ubuntu desktop rather than a headless Ubuntu server.
I finally found a solution that worked: vnc4server as the server and TightVNC Viewer as the client. Many thanks to Coddswallop's post on vnc4server setup.

Setting up vnc4server on the Ubuntu server
  • Use an SSH client to connect to the server.
  • Enter sudo apt-get update.
  • Enter sudo apt-get install gnome-core gnome-session-fallback.
  • Enter sudo apt-get install vnc4server.
  • Enter vncserver.
  • You'll be prompted to make up a password. Enter whatever you like, and make a note of it. You'll need this password when using the VNC client to connect.
  • Re-enter the same password when prompted.
  • Enter vncserver -kill :1.
  • Enter cp .vnc/xstartup .vnc/xstartup.bak.
  • Use your favorite text editor (I used VIM) to modify the file .vnc/xstartup, so that it looks like this:


#!/bin/sh

      # Uncomment the following two lines for normal desktop:
        unset SESSION_MANAGER
          #exec /etc/X11/xinit/xinitrc
            gnome-session --session=gnome-classic &

                [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
                  [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
                    xsetroot -solid grey
                      vncconfig -iconic &
                        #x-terminal-emulator -geometry 80×4+10+10 -ls -title ?$VNCDESKTOP Desktop? &
                          #x-window-manager &

                          • Enter vncserver -geometry 1200x650. You can change 1200x650 to whatever width and height suits your client machine's display, and you can add an optional depth parameter to change the color depth.
                          • Enable the necessary port in your firewall. In my case, this was port 5901. In the AWS Control Panel, I edited the applicable Security Group, creating an inbound rule for port 5901 for my laptop's IP address.
                          • Note: After rebooting the server, you'll need to reissue the command vncserver -geometry 1200x650. If you wish, you could set up your server to execute this command automatically on startup.

                          Setting up TightVNC Viewer on the Windows 7 client
                          • I visited http://www.tightvnc.com/download.php and downloaded the Installer for Windows (64-bit).
                          • I ran the resulting tightvnc-2.6.4-setup-32bit.msi.
                          • Windows asked whether I wanted to give the installer permission to run, and I said yes.
                          • During the installation, I was asked whether to allow the installer to create a Windows firewall rule. I said no.
                          • I selected custom installation and installed only the client, omitting the server component.
                          • From the Start Menu, I launched TightVNC Viewer.
                          • In the Remote Host field, I entered my server's public IP address followed by port :5901. Example: 12.34.56.78:5901. Then I clicked Connect.
                          • When prompted for a password, I entered the password I made up when installing the server.
                          • A GUI interface to my server opened. Yay!

                          Friday, January 11, 2013

                          Rails - opening a new browser tab or window in the foreground

                          In a Rails website, suppose you'd like to open a new window (or new tab, depending on the user's browser settings) when the user clicks a link. And suppose you'd like to make sure the new tab or window comes to the foreground. And suppose you'd like to specify the URL the "Rails way," using link_to rather than hard-coding an HTML a tag. Here's how...

                          This example assumes "merchant" is an instance of a Rails model class with RESTful routes. It creates a "Shop now" link that opens the merchant's page in a new tab or window and gives it the focus. Of course, this relies on JavaScript's being enabled in the user's browser.

                          <%= link_to "Shop now", merchant, onclick: "var w = window.open(this.href); w.focus(); return false;" %>