How to start a vnc server for the actual display (scraping) with TigerVNC

VNC is a desktop sharing application (Virtual Network Computing) to connect and control a (remote or local) computer's desktop over a network connection. 

However, on linux systems, many VNC server applications allow only to connect to a virtual desktop and not to the actual one. This howto offers you a solution, to connect via TigerVNC server to the actual active session on your Linux desktop.

Requirements

  • A fully functional linux desktop environment
  • root privilege (to install the TigerVNC server)
  • basic knowledge of the linux shell

In order to get the latest packages, you may want to update.

[email protected]:~$ sudo apt-get update

This howto was tested on Debian/GNU Linux 9.5 (stretch) and Ubuntu 18.04

Install TigerVNC

First, you have to install the TigerVNC server.

[email protected]:~$ sudo apt-get install tigervnc-scraping-server

Note, that on most debian-based systems, there is a small package called tigervnc-scraping-server, which you need to install. You don't have to install the main TigerVNC server (package name: tigervnc-standalone-server) to have the functionality to connect to the running desktop session, only if you prefer to connect to a virtual desktop, too.

The TigerVNC server provides a smaller application (x0vncserver) to grant access to the active session.

Then, create a .vnc directory in your home:

[email protected]:~$ mkdir -p ~/.vnc

Create a password for your vnc session:

[email protected]:~$ vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? n

Starting the VNC server

A short description of the x0vncserver:

x0tigervncserver is a TigerVNC Server which makes any X display remotely accessible via VNC, TigerVNC or compatible viewers. Unlike Xvnc(1), it does not create a virtual display. Instead, it just shares an existing X server (typically, that one connected to the physical screen).

Now that you have successfully installed TigerVNC server on your computer, created a password with the vncpasswd command, we can begin to start our vnc server. Make sure that you're on the active session, and write (as user):

[email protected]:~$ x0vncserver -passwordfile ~/.vnc/passwd -display :0

Wed Oct 10 22:17:16 2018
Geometry: Desktop geometry is set to 1920x1080+0+0
Main: XTest extension present - version 2.2
Main: Listening on port 5900

The option -passwordfile ~/.vnc/passwd reads the password file created earlier with the vncpasswd command. The second option -display :0 means, that you want to connect to the session on the display :0, which is usually the active session.

Now you can access your actual desktop with any vnc viewer application on the default vnc port 5900.

You can stop this process whenever you want by pressing Ctrl-c.

If you wish to run it in the background, type:

[email protected]:~$ x0vncserver -passwordfile ~/.vnc/passwd -display :0 >/dev/null 2>&1 &

Now all the output standard output and errors are redirected to /dev/null and with the & at the end, it will run in the background. However, you won't be able anymore to stop the vnc server by pressing Ctrl-c, instead you have to kill it's process id (see below section "Stopping the vnc server").

For more options and syntax, check the x0vncserver manual.

Stopping the VNC server

If your vnc server runs in the background, you have to know the process id, in order to stop it.

[email protected]:~$ ps -fu user | grep [x]0vncserver
user    1328    1   0 23:11 pts/2    00:00:00    /usr/bin/x0vncserver -display :0 -passwordfile /home/user/.vnc/passwd -rfbport 5900

The output will be like this, so notice the pid 1328. In order to stop the vnc server, we have to "kill" this process.

[email protected]:~$ kill -9 1328

The option -9 for the kill command will send the KILL signal to the process id to make sure that it stops.

Script to run the VNC server

There is a script to start and stop the x0vncserver application on github. For testing purposes, you download the psmisc package, too:

[email protected]:~$ sudo apt-get install git psmisc

Then, download the startvnc script using the git command:

[email protected]:~$ git clone https://github.com/sebestyenistvan/runvncserver
Cloning into 'runvncserver'...
remote: Enumerating objects: 77, done.
remote: Counting objects: 100% (77/77), done.
remote: Compressing objects: 100% (57/57), done.
remote: Total 77 (delta 25), reused 60 (delta 18), pack-reused 0
Unpacking objects: 100% (77/77), done.

Your output will look something like above.

Copy the startvnc script from the runvncserver directory to your home:

[email protected]:~$ cp ~/runvncserver/startvnc ~

Change permissions to executable, in order to execute the script:

[email protected]:~$ chmod +x ~/startvnc

Then, run the script. 

[email protected]:~$ ./startvnc

Usage: ./startvnc start|stop|restart|status

This script will only work, if you have a .vnc directory in your home and created a vncpasswd (it checks for ~/.vnc/passwd file). We already created the .vnc directory and the password earlier.

To start the vnc server on the actual display, just type:

[email protected]:~$ ./startvnc start
Starting VNC Server on display :0 [ok]

You can test, if your vnc server is running with the option:

[email protected]:~$ ./startvnc status
Status of the VNC server: [running] (pid: 1328)

Few examples:

Using TigerVNC

Or by checking the 5900 TCP port on your system with the fuser command (from the psmisc package):

[email protected]:~$ fuser -vn tcp 5900
                  USER      PID     ACCESS      COMMAND
5900/tcp:         user      1328    F....       x0vncserver

You'll get an output like this if the vnc server is running on port 5900.

Check port with fuser command

More instructions for this script you can find in the readme file:

[email protected]:~$ less runvncserver/README.md

The script will create a logfile, where the output is stored. If something goes wrong or you can't start or stop the x0vncserver, take a look at the logfile under ~/.vnc/logfile

Start the VNC server automatically

If you want to access the active desktop session automatically, you need to edit the .xsessionrc file in your home directory.

[email protected]:~$ echo "/home/user/startvnc start >/dev/null 2>&1" >> ~/.xsessionrc

 Replace user by your username and it will automatically run when starting the X session. The script logs its activity in ~/.vnc/logfile, if something goes wrong, you can check the log file there.

Automatic VNC server start

Notes

This tutorial doesn't deal with setting up a VNC virtual desktop.

Security

Be aware, that the x0vncserver doesn't use encryption by default, so use it carefully over the internet. If you want to use it remotely, you can tunnel it via ssh. However, there are other howtos where you can find a solution to encrypt your VNC session.

Or you can take a look at the ssvnc package.

VNC viewers

If you're looking for VNC viewers, there are plenty of them, for instance:

  • gvncviewer
  • tigervnc-viewer
  • xtightvncviewer
  • xvnc4viewer

Geometry

The x0vncserver on the actual display will use the same geometry as the running desktop on the :0 display. So if you set the -geometry option to a lower size, it won't get scaled, you'll just see a fraction of the desktop size. 

Feedback

Feel free to write feedback. If you tested this tutorial or even the script on another system.

Desktop shared via VNC

Share this page:

Suggested articles

18 Comment(s)

Add comment

Comments

By: DannyB

For years I have used the x11vnc command to start a VNC of the computer's physical console.  But it is not a daemon.  Just a command.  So my routine was something like...

1. SSH in to remote box, forwarding the VNC port I will be using.  I must *already* be logged in on the physical console as the user I SSH in as.

2. Run Tmux or Screen

3. Use a canned one-liner shell script to start x11vnc with appropriate parameters

4. Run local VNC viewer to see physical console of remote machine.  If someone is sitting at the physical console, they can see me moving the mouse, windows appearing, etc.

One such script is:

#!/bin/bashkillall x11vnc

(x11vnc -passwdfile vnc-passwd.txt -allow 10.49.11.82 -forever -shared -noskip_dups -repeat &) &

 

By: Istvan

Well, fortunately the guys at tigervnc created this scraping version of their vnc server. It's quite resource friendly, nevertheless it doesn't have the full capabilities of the perl version.

I like tmux and in general console much more than a desktop, but sometimes you can't avoid to use it.

 

Thanks for sharing your thoughts :-)

By: Valcsi

Hello! OFF: What is this Debian desktop theme?! So Beautiful! :)THNX!

By: Istvan

Hi! It's no particular theme. The window manager is cinnamon with a custom debian menu icon, custom desktop background. If you search for 'debian wallpaper' I'm sure you'll find it (same for the icon). Otherwise I can send you mine. HTH :-)

By: Enrique

Works for me  !! Thanks !!!

 

By: Chris

This guide is completely broken in two different ways for me on Ubuntu Bionic, armbian arm64.

The "tigervnc-scraping-server" package does not install vnc4passwd. Additionally when you try to start the server, this error appears: "x0vncserver -passwordfile ~/.vnc/passwd -display :0 Invalid MIT-MAGIC-COOKIE-1 keyx0vncserver: unable to open display ":0""

By: PoMeLo

Thanks for all! It's worked for me!

By: .

I had the same problem. You need to log in before display :0 shows up. After you log in, you can run the vnc server and it should work

By: CellToolz

i feel like the easiest way to get around the "unable to open display :0" problem is to issue this command instead

x0vncserver -passwordfile ~/.vnc/passwd -display $DISPLAY

this worked for my by using the current system variable $DISPLAY to set the actual display number.  I also edited the vncstart script from github to reflect this change aswell so there wont be any problems later on.  Thx this was a great write up. : ) 

Used this on the latest Kali Linux Rollin to date (Dec 6 2019)

-CellToolz

 

 

By: Jon

I can connect if I'm already logged in, but I get a black screen if I'm logged out.  I'm using xubuntu 18.04 (lightdm).

By: Sean

Thanks, this worked for me.

By: Richard

Finally! A good and simple VNC tutorial that gets it to display on screen 0. Thank you so much!

 

The only modification I did was to use this guide here, to be able to specify the password via a script as well, using the "expect" command

 

############

# Full script for any future readers:

# Create an 'expect' script to fill in the VNC password

    # Source - https://askubuntu.com/a/328323

    

sudo apt install expect

cat > ~/ConfigureVNC.sh << 'END_SCRIPT'

#!/bin/sh

 

prog=/usr/bin/vncpasswd

mypass="PASSWORD_GOES_HERE"

 

mkdir -p ~/.vnc

 

/usr/bin/expect <<EOF

spawn "$prog"

expect "Password:"

send "$mypass\r"

expect "Verify:"

send "$mypass\r"

expect "view-only password (y/n)?"

send "n\r"

expect eof

exit

EOF

 

ls ~/.vnc/ -la

END_SCRIPT

 

chmod +x ~/ConfigureVNC.sh

~/ConfigureVNC.sh

 

By: mark

you sir, are a gentleman and a scholar.  Thanks for this!

By: wistlo

On Debian 10 (Buster) with gnome desktop, a change to the username using usermod with --home breaks remote access to display :0, with message

No protocol specified Error: Can't open display: :0.0

The gnome user name must be changed in gnome settings in the graphical environment to match the newly created user.  (There may be a scripted way to do this, as well). 

This article is an excellent guide, exactly what I needed.  I had used x11vnc for years, looked at LibVNC, but all I really needed was a simple precomplied binary to give nothing but console access for remote support (no need to create a virtual session).  For remote Debian users, it's similar to installing tightvnc server on Windows. 

By: MartinK

Works perfectly on CentOS 7, I can log into the existing local session from a laptop using the Windows Remote Desktop perfectly. 

 

However, I can't see the mouse cursor - moving the mouse clearly moves the "invisible" cursor.

Any solutions for this, please? Thank you.

By: Ben Zandstra

Kept running into a black screen only when connecting to the vnc . Lot's of solutions tried. Only as soon as I plugged in an HDMI monitor into the Ubuntu box the active VNC session switched to the full HD remote screen. Go figure....

By: Thomas

Thanks for this great tutorial and script. Works for me with Linux Mint 20.3

Thomas

By: Chris

I followed these steps and it all works perfectly except for starting the vnc server from the .xsessionrc file. It appears to start correctly (can see it listening with fuser), but when a client connects it crashes the vncserver. If I run the same command (/home/chris/startvnc start) from a terminal it accepts client connections. I tried adding a pause to the startup in case it was related to that and I tried moving the command to other startup scripts (.xsession etc) but that didn't help.

 

When the client connects the log reports this:

Mon Sep 12 17:18:08 2022

Connections: accepted: 192.168.1.12::53962

 SConnection: Client needs protocol version 3.8

 SConnection: Client requests security type VncAuth(2)

 XDesktop:    Enabling 8 buttons of X pointer device

X Error of failed request:  BadMatch (invalid parameter attributes)

  Major opcode of failed request:  130 (MIT-SHM)

  Minor opcode of failed request:  4 (X_ShmGetImage)

  Serial number of failed request:  63

  Current serial number in output stream:  63

 

I looked up Major 130 Minor 4 and it seems to be related to screen resolutions. 

 

Has anyone seen anything like this?

ta,

chris