Here is a simple script that will install and setup VNC on a debian based machines e.g. Debian, Ubuntu, Linux Mint, Armbian SBC, Nas, Laptop or Desktop etc..
Overview
It only requires you to enter your sudo password to install the required packages, and create passwords for a new “vnc” user and a vnc connection password for access from your vnc client machine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#!/bin/bash # Don prince 2021/01/17 echo ######## install packages sudo apt update && sudo apt -y install xorg lightdm xfce4 tango-icon-theme gnome-icon-theme dbus-x11 tightvncserver xfonts-base thunar-volman udisks2 gvfs echo ######## create vnc user sudo adduser vnc sudo gpasswd -a vnc sudo echo ######## start vncserver and set password sudo su - vnc -c "touch ~/.Xresources" sudo su - vnc -c "vncserver :1" ps -ef | grep Xtightvnc sudo su - vnc -c "vncserver -kill :1" echo ######## xstartup script cat <<EOF | sudo tee /home/vnc/.vnc/xstartup #!/bin/sh xrdb $HOME/.Xresources #xsetroot -solid grey #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 #xfce4-session& /etc/X11/Xsession /usr/bin/startxfce4 & EOF sudo chown vnc:vnc /home/vnc/.vnc/xstartup sudo chmod 755 /home/vnc/.vnc/xstartup echo ######## vnc start/stop/restart script cat <<'EOF' | sudo tee /usr/local/bin/tightvncserver #!/bin/bash PATH="$PATH:/usr/bin/" DISPLAY="1" DEPTH="16" GEOMETRY="1024x768" OPTIONS="-depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}" case "$1" in start) /usr/bin/vncserver ${OPTIONS} ;; stop) /usr/bin/vncserver -kill :${DISPLAY} ;; restart) $0 stop $0 start ;; esac exit 0 EOF sudo chown root:root /usr/local/bin/tightvncserver sudo chmod 755 /usr/local/bin/tightvncserver echo ######## vnc systemd service script echo ######## sudo systemctl [start/stop/reload] tightvncserver.service cat <<EOF | sudo tee /lib/systemd/system/tightvncserver.service [Unit] Description=Manage VNC Server [Service] Type=forking ExecStart=/usr/local/bin/tightvncserver start ExecStop=/usr/local/bin/tightvncserver stop ExecReload=/usr/local/bin/tightvncserver restart User=vnc [Install] WantedBy=multi-user.target EOF sudo chown root:root /lib/systemd/system/tightvncserver.service sudo chmod 644 /lib/systemd/system/tightvncserver.service sudo systemctl daemon-reload sudo systemctl start tightvncserver.service |
Running the Script
Extract the script and make executable with “chmod +x”. Then run it, entering the passwords for the VNC user and VNC connection when asked.
You can start, stop or reload the tightvncserver by executing one of the following lines
1 2 3 4 |
sudo systemctl start tightvncserver.service sudo systemctl stop tightvncserver.service sudo systemctl reload tightvncserver.service |
To enable and disable the tightvncserver to autoamatically start at boot time, execute one of the following lines
1 2 |
sudo systemctl enable tightvncserver.service sudo systemctl disable tightvncserver.service |
Connecting to the VNC Host machine
I use Vinagre, a remote desktop viewer for the GNOME desktop to connect to the VNC host machine from my Ubuntu Laptops.
Connect to the VNC Host on port 5901 e.g. 192.168.x,y:5901.
Examples
Below are some results of running the script on various machines
thanks, it worked!
Thank you for this shell script, it has given me a lot of insight and saved me a lot of time.