VNC

Aug 7, 2011·
Julio Batista Silva
Julio Batista Silva
· 4 min read
blog iPhone

Virtual Network Computing (VNC) is a system that lets you control a remote computer graphically over the RFB protocol.

TightVNC, Vino and X11vnc are some of the VNC remote access programs available for Linux. In this post I will talk about TightVNC.

  1. Install TightVNC:

    julio@julio-acer ~ $ sudo pacman -S tightvnc
    
  2. Create the initial files and set an access password

    julio@julio-acer ~ $ vncserver :1
    You will require a password to access your desktops.
    
    Password:
    Verify:
    Would you like to enter a view-only password (y/n)? n
    
    New 'X' desktop is julio-acer:1
    
    Creating default startup script /home/julio/.vnc/xstartup
    Starting applications specified in /home/julio/.vnc/xstartup
    Log file is /home/julio/.vnc/julio-acer:1.log
    

    By default, the server runs on TCP port 5900+n, where n is the display number.

  3. To start configuring, kill the running server

    julio@julio-acer ~ $ vncserver -kill :1
    
  4. Edit the configuration file, much like .xinitrc

    julio@julio-acer ~ $ vim ~/.vnc/xstartup
    #!/bin/sh
    export XKL_XMODMAP_DISABLE=1 #Avoids an error when mapping the keyboard
    exec startxfce4
    #exec awesome
    
  5. For safety, allow local connections only, so that anyone wanting to connect to the computer remotely has to authenticate over ssh.

    To allow only local connections, just start vncserver with the -localhost option: vncserver -localhost :1

  6. Starting and stopping the VNC server automatically

    1. Start the VNC server when the computer boots:

      julio@julio-acer ~ $ sudo vim /etc/rc.local
      su julio -c "vncserver -localhost :1"
      
    2. Stop the VNC server when the computer shuts down:

      julio@julio-acer ~ $ sudo vim /etc/rc.local.shutdown
      su julio -c "/usr/bin/vncserver -kill :1"
      

Connecting from another computer

$ ssh -p 22 julio@casa.juliobs.com -L 8900/localhost/5901

Where I put -p 22, change the port to the number you use for SSH. The -L option does the SSH tunneling. Then run vncviewer localhost:8900.

Connecting from the iPhone

The iSSH app has the option of connecting over VNC directly (RAW), but as already explained, SSH tunneling is safer.

  1. Open iSSH

  2. Go to Add Configuration… and make it look like the following configuration:

    (image unavailable)
    
  3. Go back to the program’s home screen and pick the configuration you just created.

    (image unavailable)
    

    Since we are using an iPhone 3G with a 480x320 resolution (the iPhone 4 has a 960x640 screen), it can be worth configuring TightVNC to resize the screen so we get a full view of the desktop.

    Start the server with the -geometry option. For landscape view:

    julio@julio-acer ~  $  vncserver -localhost -geometry 480x268 :1
    

    I took about 52 pixels off the height to make up for the top of the screen.

    For portrait view:

    julio@julio-acer ~  $  vncserver -localhost -geometry 320x416 :1
    

Sometimes I also want to control the same display that is being shown on the remote computer, or simply to monitor the programs that are running. That is not possible with TightVNC, but it is easily done with X11vnc.

  1. Install X11vnc

    julio@julio-acer ~  $  sudo pacman -S x11vnc
    
  2. If you like, set an .Xauthority

    julio@julio-acer ~  $  x11vnc -display :0 -auth ~/.Xauthority
    
  3. Set a password

    julio@julio-acer ~  $  mkdir ~/.x11vnc
    julio@julio-acer ~  $  x11vnc -storepasswd your_password_here ~/.x11vnc/passwd
    stored passwd in file: /home/julio/.x11vnc/passwd
    
  4. Just as with TightVNC, use the -localhost option for safety.

    In short, the VNC server can be reached with the following script, which you may want to put in .xinitrc:

    julio@julio-acer ~  $  x11vnc -localhost -many -display :0 -auth ~/.Xauthority &>/dev/null &
    

    The -many option keeps the server from shutting down when you end the session.

  5. Connect through the SSH tunnel

    julio@servidor ~  $  ssh -p 22xx -t -L 5900:localhost:5900 casa.juliobs.com 'sudo x11vnc -display :0 -auth /home/julio/.Xauthority'
    
  6. Access from the iPhone works exactly the same way as with TightVNC, but here we use port 5900.


Problems:

Apparently tightVNC does not support RandR, which is needed to change the screen resolution and to open some programs such as Chromium and Firefox. There are a few patches on the internet that solve the problem. Another program that seems to support RandR is RealVNC.


Julio Batista Silva
Authors
Senior Cloud Developer

I’m a Brazilian-German computer engineer based in Germany, passionate about tech, science, photography, and languages.

I’ve been programming for about two decades already, exploring everything from mobile apps and web development to machine learning. These days I focus on cloud SRE and data engineering.

I volunteer in the open source and Python communities, helping organize PyCon DE and PyData Berlin, serving on the PSF D&I Work Group, mentoring, and contributing code and translations.

On my blog, I share Linux tips, setup guides, and personal notes I’ve written for future reference. I hope others find them helpful as well. The content is available in multiple languages.

Browse my gallery for some of my photography.

Away from the keyboard, you’ll find me at concerts, playing clarinet, cycling, scuba diving, or exploring new places, cultures, and cuisines.

Always happy to connect! 🙂

comments powered by Disqus