Setup raspberry pi vnc server (X11VNC)
Typically, a VNC server is installed on Raspberry Pi to remotely control the desktop. By VNC client you get a virtual desktop of Raspberry Pi on your own computer. With this remote desktop you can then remotely control Raspberry Pi or remotely control it.
VNC is especially useful if you prefer to work with a graphical desktop instead of using SSH on the console or if you want to peer over someone’s shoulder and provide remote support. The advantage is that screen content can be mirrored.
The goal is to create a VNC connection to see and control the current desktop on Raspberry Pi. This solution is suitable if you want to look over a person sitting on Raspberry Pi from a distance over the shoulder (remote support). The VNC server X11VNC is best suited for this purpose.
To access Raspberry Pi via the VNC network, you need a VNC client. There are a lot of them. As a rule, one with a small range of functions, which is usually free, is sufficient. Therefore, here only a small selection.
- Windows: z. B. TightVNC
- Mac OS X: z. B. Chicken
- Linux: z. B. vncviewer (tigervnc) or others
- Obtain a suitable VNC client for your client PC.
- Install a VNC server on Raspberry Pi.
- Set up the VNC server to start automatically.
Of course, to be able to remotely control the desktop via VNC, Raspberry Pi must automatically start the desktop. To do this, set the start mode to “Enable Boot to Desktop / Scratch” with “raspi-config”. Optionally with or without automatic login by the user “pi”.
In addition, it is recommended to set the graphics memory in “raspi-config” under “Advanced Options> Memory Split” to the value 64 (MB).
For the settings to be adopted, Raspberry Pi needs to be restarted.
Solution: Install VNC server for remote support
Note: Be careful not to have multiple VNC servers installed at the same time and start automatically at boot time. Before you install X11VNC, undo previous VNC server installation and configuration.
First we install the package “x11vnc”.
sudo apt-get update
sudo apt-get install x11vnc
Then we start “x11vnc” in a separate terminal window or tab.
x11vnc -usepw -forever -display: 0
The command provides that the X11VNC server continues to run even after the connection has been disconnected (option -forever) and the VNC password is requested when the connection is established by the VNC client. The first time the X11VNC server is started, this password must be set. This password has nothing to do with the user password. This password can be chosen freely and must be confirmed a second time. A subsequent question about a readonly password can be answered in the negative.
As long as the X11VNC server is running in the terminal, you can call up and control the current desktop with a VNC client. For this you need the IP address or the host name of Raspberry Pi and the port number 5900 (eg 192.168.1.2:5900), which you enter both in the VNC client.
If necessary, the X11VNC server can be terminated with Ctrl + C.
Note: The command parameter “: 0” refers to the currently visible desktop. He has the port “5900”. If you want to operate virtual desktops via VNC with X11VNC, then increase to “: 1” and select the port “5901” accordingly. For the second virtual desktop then “: 2” and the port “5902”, and so on.
Extension: Autostart of the X11VNC server after the user login
Of course it is inconvenient to start the X11VNC server manually with every VNC connection. It would be nice if it would start automatically when booting Raspberry Pi.
For this we create an autostart file.
In the file we add the following lines:
Type = Application
Name = x11vnc
Exec = x11vnc -usepw -forever -display: 0
StartupNotify = false
Then save and close the file: Ctrl + O, Return, Ctrl + X.
Now you should restart Raspberry Pi once.
After that, if the desktop was started, a VNC connection to Raspberry Pi should be possible.
Note: In this case, the X11VNC server will not be started until the user has logged in or is automatically logged on.
Extension: Autostart of the X11VNC server before the user login
You can also start the X11VNC server before the user login. Then he displays the login desktop.
First, we need to put the VNC password in a central location.
sudo x11vnc -storepasswd /etc/x11vnc.pass
Enter the password twice and confirm the saving in the file.
Then we create a systemd unit:
sudo nano /lib/systemd/system/x11vnc.service
There we enter the following lines:
Description = Start X11VNC
After = multi-user.target
Type = simple
ExecStart = /usr/bin/x11vnc -display: 0 -auth guess -forever -loop -noxdamage -repeat -rfbauth /etc/x11vnc.pass -rfbport 5900 -shared
WantedBy = multi-user.target
Then save the file and close it with Ctrl + O, Return, Ctrl + X.
Then we need to configure the system d-unit for automatic startup.
sudo systemctl enable x11vnc.service
Then restart Raspberry Pi once.
Problem: Resolution too small or too big
If you want to view and control the current screen (parameter “: 0”) with X11VNC, then the size of the VNC client will take the current resolution of the screen on Raspberry Pi. If there is no HDMI screen connected, then the resolution is quite small, which is then taken over by the VNC client. But that does not work seriously. In this case you are forced to set the resolution for HDMI.
Conversely, it may as well happen that the resolution of your own screen is smaller, so the resolution of the VNC window, which is based on the current resolution of the remote system. In such a case one must scroll in the VNC window, which is very cumbersome.
Here is the help of the “-scale” option, which is appended to the x11vnc command. With this option you can specify a factor or a target resolution.
Variant with a minimization factor:
... -scale 0.9
Variant with the desired target resolution:
... -scale 800x600
How the option for your own needs must look like, you have to try it out.
Problem: VNC connections to X11VNC are unstable
X11VNC may be disconnected if certain applications are running or used on the desktop. As a rule, the crashes can be reconstructed. In this case you should completely uninstall X11VNC and try it with RealVNC. The specially adapted for Raspberry Pi version seems to make a more stable impression.
VNC is uncertain. What is meant is that all data (including passwords) is transmitted in plain text. Therefore you should only use VNC in the local network.
If Raspberry Pi can be reached via port forwarding (DNAT, port forwarding, DMZ) or directly from the Internet, then it can be attacked via the VNC server. In this case, the VNC should be operated exclusively with the parameter “-localhost” and the VNC session should be established via SSH.
Alternative: Remote Desktop via RDP and RealVNC
In the Windows world, VNC is less common. This is called remote desktop support via RDP. If you’re working with Windows clients, setting up an RDP server on Raspberry Pi is a viable alternative to VNC.
The Raspbian image comes with RealVNC as a VNC server, which only needs to be activated with a configuration tool. Then it will start automatically.