Skip to main content

VNC

Common

  • Ubuntu 22.04 RDP replaces the legacy VNC as the default remote desktop protocol.

Update VPS

sudo apt update & apt dist-upgrade -y

Security

sudo ufw allow 5901

Scenario

Choose Xfce with Ubuntu 22.04 is the best but cost more ram.

LXDE

Work: Ubuntu server 22.04/20.04 on Linode, VMware

Bug: keyboard input up/down/right/left/tab not work

# Desktop environment.
sudo apt update
sudo apt dist-upgrade
# Select gdm3 if ask.
# autocutsel for copy & paste
sudo apt install -y lxde-core xdg-utils xfonts-100dpi xfonts-75dpi lxterminal autocutsel

# Define Xorg
touch ~/.Xresources

# VNC server.
sudo apt install -y tightvncserver

# Check version
dpkg -l | grep vnc

# Start a VNC server instance.
# Run first time will auto config /.vnc/xstartup
# Would you like to enter a view-only password (y/n)? n
vncserver -localhost
# Or
vncserver :1

# List cookies
xauth -b list

# Change your password or add a view-only password.
vncpasswd

# Stop the VNC server instance
vncserver -kill :1

# ~/.vnc/config
# desktop=sandbox
geometry=1920x1080 # resolution
# localhost # listen local
# alwaysshared
dpi=96

# Configuring the VNC server
cp ~/.vnc/xstartup ~/.vnc/xstartup.bak
chmod +x ~/.vnc/xstartup
vi ~/.vnc/xstartup

~/.vnc/xstartup replace all

#!/bin/sh

xrdb "$HOME/.Xresources"
xsetroot -solid grey
autocutsel -fork # Enable copy & paste
#x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#x-window-manager &
# Fix to make GNOME work
export XKL_XMODMAP_DISABLE=1
/etc/X11/Xsession

lxterminal &
/usr/bin/lxsession -s LXDE &

another

#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
export XKL_XMODMAP_DISABLE=1

#vnc config tool show at the top-left in vnc window
command -v vncconfig && vncconfig -iconic &

# Some dist run /etc/X11/xinit/xinitrc after install vncserver.
#[[ -r /etc/X11/xinit/xinitrc ]] && source /etc/X11/xinit/xinitrc
#/etc/X11/xinit/xinitrc # include exec, if it run correctly, blow code will not run

#x resource if user's .xinitrc running:
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources

# Which GUI
#session=startxfce4 #xfce
session=startlxde #lxde
#session=gnome-session #GNOME
#session='gnome-session --session=gnome-classic'
#session=mate-session #MATE
#session=startdde #DDE(Deepin)
#session=startkde #KDE Plasma
#session=i3 #i3wm

# Copying clipboard content from the remote machine (need install autocutsel)
command -v autocutsel && autocutsel -fork

#exec $session
exec "$session"
#exec /usr/bin/dbus-daemon "$session"

vncserver -kill $DISPLAY
# To (#System Service)

# Delete all ssh.exe if another remote desktop not disconnect.

# Create an SSH connection on your local computer.
ssh -L {Local Port}:127.0.0.1:{Remote Port} -N -f -l {User} {Remote IP}
# Example
ssh -L 59001:127.0.0.1:5901 -N -f -l root 129.112.197.165

# Tabby
# Program
C:\Program Files\PowerShell\7\pwsh.exe
# Arguments
-Command
& ssh -L 59001:127.0.0.1:5901 -N -f -l root 129.112.197.165
# One line
'C:\\Program Files\\PowerShell\\7\\pwsh.exe' -nologo -Command '& ssh -L 59001:127.0.0.1:5901 -N -f -l root your_server_ip'

# Firefox
sudo apt install -y firefox

# Run firefox
firefox

# Client is not authorized to connect to ServerError
sudo XAUTHORITY=$HOME/.Xauthority firefox

# Client is not authorized to connect to ServerError: cannot open display
# Open permission for localhost
xhost +localhost

# xdg-settings: not found
sudo apt-get install xdg-utils

# If it shows :
# Couldn't start Xtightvnc; trying default font path.
# Please set correct fontPath in the vncserver script.
# Couldn't start Xtightvnc process.
# Maybe you can

vncserver -list
vncserver -kill :1
vncserver :1

# Check listen
ps -ef | grep vnc | grep -v grep # e: show all process, -f: show UID,PPIP,C and STIME column
sudo netstat -lnp|grep -i vnc

# list port
ps -ef | grep vnc

# Log
cat ~/.vnc/vm1\:1.log | less

# Xorg bug, tightvncserver not support multi display:
# Xlib: extension "RANDR" missing on display

Xfce

Ubuntu 22.04 work fine.

sudo apt install -y xfce4 xfce4-goodies xorg dbus-x11 x11-xserver-utils

sudo apt install -y tigervnc-standalone-server tigervnc-common

vncserver :1

vncserver -kill :1

touch ~/.vnc/xstartup
chmod u+x ~/.vnc/xstartup
vim ~/.vnc/xstartup

~/.vnc/xstartup

#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec startxfce4
vim ~/.vnc/config

~/.vnc/config

geometry=1920x1084
dpi=96
sudo apt install locales
sudo dpkg-reconfigure locales # All locales

# Default language
vim ~/.bashrc
export LANG=en_US.UTF8

# Chinese font
sudo apt install fonts-wqy-zenhei xfonts-wqy

# Firefox
sudo apt install -y firefox
# For desktop, fix cannot open display:1.0
xhost +

vim ~/.bashrc
alias firefox="XAUTHORITY=$HOME/.Xauthority firefox"
source ~/.bashrc

# pCloud
sudo apt install libfuse2

Improve Color

Config vnc viewer picture quality to improve color depth

Properties -> Opetions -> General -> Picture quality: High

Wayvnc

Ubuntu 22.04

sudo apt-get -y install wayvnc

# Remove wayvnc configuration, data, and all of its dependencies
sudo apt-get -y autoremove --purge wayvnc

System Service

For TigerVNC

vncserver -kill :1

sudo vi /etc/systemd/system/[email protected]

/etc/systemd/system/[email protected]

[Unit]
Description=Start vnc server at startup
After=syslog.target network.target

[Service]
Type=forking
User=root
Group=root
WorkingDirectory=/root

# There have been some changes to systemd
#PIDFile=/root/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 -localhost :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target
# Make the system aware of the new unit file.
sudo systemctl daemon-reload
# Enable the unit file.
# The number 1 after the @ sign defines the display port on which the VNC service will run.
sudo systemctl enable [email protected]

# Start any other systemd service.
sudo systemctl start vncserver@1

# Verify.
sudo systemctl status vncserver@1

Operation

# List session
ps -ef | grep vnc | grep -v grep

# Remove systemd services

systemctl stop $VNC_SERVICE_INSTANCE # Sometimes will not response
systemctl disable $VNC_SERVICE_INSTANCE
ls /etc/systemd/system/vnc*
rm /etc/systemd/system/$VNC_SERVICE
rm /etc/systemd/system/vnc* # and symlinks that might be related
rm /usr/lib/systemd/system/$VNC_SERVICE
rm /usr/lib/systemd/system/vnc* # and symlinks that might be related
systemctl daemon-reload
systemctl reset-failed

Tools

TigerVNC

From TightVNC. The most common server side in Linux.

TurboVNC

Support VirtualGL.

RemoteVNC

Support NAT and firewall pass through.

RealVNC

The best commercial VNC, from vnc inventor.

Troubleshoot

VNC protocol base on X, not support Wayland.

Edit the /etc/gdm3/custom.conf file to disable Wayland.

Delete ssh.exemanually at Windows.