VNC Client Configuration (TigerVNC)
Linux VNC Connection Service:
Please save the following script to reconnect vnc
Step 1/3: shell script to retrying - copy the following script to /usr/local/bin/vnc-reconnect.sh
please give execute permission as given below
chmod +x /usr/local/bin/vnc-reconnect.sh
Step 2/3: service daemon configuration
please create the file - /etc/systemd/system/vnc-reconnect.service and copy the following contents
[Unit]
Description=VNC Reconnect Service
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/vnc-reconnect.sh
Restart=always
RestartSec=10
User=<username>
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/<username>/.Xauthority
[Install]
WantedBy=multi-user.target
Step 3/3: after doing both 1/2 and 2/2 steps; please run the following:
sudo systemctl daemon-reload
sudo systemctl enable vnc-reconnect.service
sudo systemctl start vnc-reconnect.service
sudo systemctl status vnc-reconnect.service
For checking logs:
logs
journalctl -u vnc-reconnect.service -b
If you want to configure at server side (optional) : VPN Auto-Reconnect
Verify VPN Auto-Reconnect: Ensure your OpenVPN client on your device is configured to automatically reconnect. In PfSense, check the OpenVPN client settings for options like "Keepalive" or "Reconnect on failure." Set a low keepalive interval (e.g., 10 60) to detect and recover from disconnections quickly.Use a VNC Client with Auto-Reconnect: TigerVNC's default client may not support automatic reconnection natively. Consider using a VNC client that supports auto-reconnect, such as:RealVNC Viewer: Has a built-in reconnect option.Remmina(if available on your platform): Supports reconnection attempts for VNC. Alternatively, you can script TigerVNC to retry connections.
Scripting Auto-Reconnect for TigerVNC: Create a shell script to monitor and reconnect the VNC session. Below is an example script for Linux-based systems (adapt for your client device if needed):#!/bin/bash VNC_SERVER="192.168.x.x:5901" VNC_CLIENT="vncviewer" RETRY_INTERVAL=10 while true; do ping -c 1 -W 2 $VNC_SERVER > /dev/null 2>&1 if [ $? -eq 0 ]; then echo "VPN is up, attempting VNC connection..." $VNC_CLIENT $VNC_SERVER echo "VNC disconnected, retrying in $RETRY_INTERVAL seconds..." else echo "VPN is down, waiting for connection..." fi sleep $RETRY_INTERVAL doneSave this as vnc_reconnect.sh, make it executable (chmod +x vnc_reconnect.sh), and run it (./vnc_reconnect.sh).Adjust VNC_SERVER to your server's IP and port.The script checks if the VPN is connected by pinging the VNC server and attempts to reconnect if the connection drops.
Run the Script in the Background (just for testing - temporary):Use a terminal multiplexer like tmux or screen to keep the script running:tmux new -s vnc_session './vnc_reconnect.sh'Detach from the session (Ctrl+B, D in tmux) to let it run in the background.
Optimize VNC Server Settings:On the CentOS 7.9 VNC server, ensure the TigerVNC server is running persistently. Edit the VNC service configuration (/etc/systemd/system/vncserver@.service) to restart on failure:[Service] ... Restart=always RestartSec=10Reload systemd and restart the service:sudo systemctl daemon-reload sudo systemctl restart vncserver@:1
Handle Mobile Hotspot Stability:If possible, use a mobile device with better signal stability or a dual-SIM phone to switch networks automatically.Configure your VPN client to prioritize faster reconnection by reducing the ping-restart timeout in the OpenVPN configuration (e.g., ping-restart 10).
Test and Monitor:Test the script by simulating network drops(e.g., disable/enable the mobile hotspot).Monitor logs on the VNC server(/var/log/vncserver*.log)and client for errors.