Skip to content

VNC

Be aware! Everyone with the VNC password will get access to your account

It is recommended not to use VNC. Use jupyterhub or screen or tmux instead. However, for GUI applications there is no other way.

The VNC (Virtual Network Computing) allows to view a graphical user interface (GUI) from a remote server in an viewer application. This can be used to launch GUI programs on the servers.

Xvnc is the Unix VNC server. Applications can display themselves on Xvnc as if it were a normal display, but they will appear on any connected VNC viewers rather than on a physical screen. The VNC protocol uses the TCP/IP ports 5900+N, where N is the display number.

Currently VNC is installed on: - SRVX8 [staff] - JET01 [staff] - AURORA [staff]

Userservices

It is highly recommended to use the userservices scripts available on all IMGW Servers to make configurations for VNC.

using the userservices
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ userservices vnc -h
################################################################################
User Services   -  VNC Server Setup/Launcher/Stopper

  vnc -h -s -x -d -l

Options:
    -h      Help
    -c      Check for vnc server(s) running
    -s      Stop vnc server(s)
    -x      Write xstartup in /home/spack/.vnc
    -d      Prepare vncserver Service
    -p []   Port: 1 - 99
    -l      Launch vnc server/service
    -w []   Desktop Session: icewm, xfce
################################################################################
Author: MB
Date: 25.01.2021
Path: /home/swd/userservices/userservices.d
################################################################################
Installed Desktops: icewm-session 
################################################################################

Running the script without any options will run all necessary steps. In case of error try removing your .vnc directory, as older configurations might be in the way. There shall be at least two desktop options: icewm and xfce. You can specify this directly with the -w [DESKTOP] option.

Setup - Manual

Please consider using the userservices vnc script to do this setup.

First of all check if a VNC server is already running or not. Depending on the results you have two options:

  1. Use an existing. (Note the Port/Display Number)
  2. Stop all and start a new VNC server
  3. on Jet setup a service running your VNC Server.
Bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Check VNC Server directly, lists available with DISPLAY/PORT
vncserver -list

# Check for processes:
ps ux | grep Xvnc

# Kill a VNC Display
vncserver -kill :[DISPLAY]

# Start a new VNC Server
vncserver

Jet Cluser

on Jet there are the user services available to you:

Bash
1
2
# Help information on VNC userservice
userservices vnc -h

if you have never used this script or a vncserver just running userservices vnc will be enough for you to setup the VNC service. It will tell you which port you have been automatically assigned.

Connecting, setting the window manager

Use a VNC client (e.g. TigerVNC Viewer (All OS) or RealVNC VNC Viewer on Windows, or Remmina on any Linux distribution) and connect to srvx?.img.univie.ac.at:[DISPLAY].

Note: Since version 1.10 of tigerVNC it is possible to resize the windows directly

Connect with a viewer:

  1. Hostname: srvx1.img.univie.ac.at or jet01.img.univie.ac.at
  2. VNC Port: vncserver -list or systemctl status --user vncserver.slice
  3. VNC passwort (vncpasswd allows to set this on the server)

The VNC server will automatically start a graphical window manager/desktop environment. Details are stored in .vnc/xstartup. Your default configuration will load GNOME, and .vnc/xstartup will be something like this:

Bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/sh

unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
/etc/X11/xinit/xinitrc
# Assume either Gnome will be started by default when installed
# We want to kill the session automatically in this case when user logs out. In case you modify
# /etc/X11/xinit/Xclients or ~/.Xclients yourself to achieve a different result, then you should
# be responsible to modify below code to avoid that your session will be automatically killed
if [ -e /usr/bin/gnome-session ]; then
    vncserver -kill $DISPLAY
fi

It is recommended to use IceWM instead. This is a more lightweight alternative to GNOME. To switch to IceWM, first log out from GNOME and disconnect from your VNC viewer, then change your .vnc/xstartup to this:

Bash
1
2
3
4
5
6
7
#!/bin/sh

[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
xterm -geometry -sb -sl 500 -fn 9x15bold -title "$VNCDESKTOP Desktop" &
icewm &

Some information on what could be put into .Xresources is given here. It might be possible to replace icewm here with startxfce4 to choose XFCE Desktop environment.

VNC as a Service

This is only here for reference, on SRVX2 and Jet use the userservices vnc.

Setup, replace [DISPLAY] with an appropriate number, e.g. 3:

Bash
1
2
3
4
5
6
mkdir -p ~/.config/systemd/user
cp /usr/lib/systemd/user/vncserver@.service ~/.config/systemd/user/
systemctl --user daemon-reload
vncpasswd
systemctl --user enable vncserver@:[DISPLAY].service --now
loginctl enable-linger

Finally stop/restart the VNC server.

Bash
1
2
3
4
5
6
7
8
# Restart
systemctl --user restart vncserver@:[DISPLAY].service --now
# Stop
systemctl --user stop vncserver@:[DISPLAY].service --now
# Disable Service
systemctl --user disable vncserver@:[DISPLAY].service --now
# Enable Service
systemctl --user enable vncserver@:[DISPLAY].service --now

Monitor the status:

Bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Like this
systemctl --user status vncserver@:[DISPLAY].service
# or
systemctl --user status vncserver.slice

 vncserver@:7.service - Remote desktop service (VNC)
   Loaded: loaded (/jetfs/home/mblaschek/.config/systemd/user/vncserver@.service; enabled; vendor preset: e>
   Active: active (running) since Mon 2020-10-05 17:00:23 CEST; 3 months 8 days ago
   CGroup: /user.slice/user-54212.slice/user@54212.service/vncserver.slice/vncserver@:7.service
           ├─  32405 gpg-agent --homedir /root/.emacs.d/elpa/gnupg --use-standard-socket --daemon
           ├─  59959 /usr/bin/gnome-keyring-daemon --start --foreground --components=secrets
           ├─ 116498 /usr/lib64/firefox/firefox -contentproc -childID 47 -isForBrowser -prefsLen 10637 -pre>
           ├─ 177114 /bin/sh /usr/bin/xdg-open about:blank
           ├─ 177225 /usr/lib64/firefox/firefox about:blank
...

Change the resolution of your VNC Session

xrandr gives you a list of available resolutions, that can be use. Requires a $DISPLAY variable to be set, using your VNC display number does the trick, e.g. :3.

Bash
1
2
# Change VNC display resolution [width x height]
$ userservices vnc-geometry 1920x1080

Change the resolution to e.g. 1920x1080 (HD):

Bash
1
xrandr -s 1920x1080 -d $DISPLAY

Adding resolutions according to your display's resolution have a look at

add_xrandr_resolution
Add a new monitor resolution to VNC
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
# By Michael Blaschek
# Date 15.08.2020
# CC BY 4.0 International
# University of Vienna, Austria

# Description:
# Add custom VNC resolution
# Requires: xrandr and a running VNC session

if [ $# -lt 2 ]; then
    echo "e.g. try: $0 1920 1380"
    echo "or 2560 1320"
else
    refresh=60
    if [ $# -eq 3 ]; then
        refresh=$3
    fi
    command=$(cvt $1 $2 $refresh | grep Modeline | awk '{for(i=3;i<NF;i++){printf "%s ",$i}}')
    xrandr --newmode  "$1x$2" ${command/Modeline/}
    xrandr --addmode VNC-0 "$1x$2"
    xrandr -d $DISPLAY -s "$1x$2"
fi

and run it like this:

Add a custom resolution to your virtual display
1
2
# running the script and adding a resolution you require, in pixel
$ add_xrandr_resolution [width] [height]

Note: $DISPLAY is an environment variable that is usually set to your VNC server port.


Last update: February 1, 2024
Created: October 14, 2021