Skip to content

Instantly share code, notes, and snippets.

@UniDyne
Last active November 30, 2025 00:52
Show Gist options
  • Select an option

  • Save UniDyne/b6ff4aad2f4030f3ca1810d24eded160 to your computer and use it in GitHub Desktop.

Select an option

Save UniDyne/b6ff4aad2f4030f3ca1810d24eded160 to your computer and use it in GitHub Desktop.
Xen / XCP-ng : Forward VM console session to local port for VNC access.

XCP-ng Easy VNC to VM Console

For people that are tired of manually looking up DOM ID, executing socat and establishing their SSH tunnel.

  • Edit connect_vm.sh to add the IP address of your XCP-ng host.
  • Ensure socat is installed on the host. If it isn't, use yum install socat
  • For best results, set up keys for SSH root login to our XCP-ng host.
  • Place the script forward_console.sh on the XCP-ng host, in /root.
  • Place the script connect_vm.sh locally on the computer you will VNC from.
  • Optionally, you may edit connect_vm.sh to launch your preferred VNC viewer.

Essentially, these scripts create an SSH tunnel for VNC to the local Unix socket for the Xen VNC console connection. Because the ID of the connection may change between boot cycles, the script on the host will look up the current DOM ID using the VM name, so you can simply reference the VM name.

UPDATES

  • 2025-11-27: Script has been updated to select a random port between 5000 and 6000. Port numbers no longer need to be set / tracked manually.
  • 2025-11-27: Added sleep line and addtional instructions for launching VNC client.
#!/bin/bash
### This script goes on your local computer (client).
### You should set up ssh keys for root login to your XCP-ng server.
## Usage: ./connect_vm.sh <vm_name>
# Change line below to the IP of your XCP-ng host
host_ip=0.0.0.0
# Check if the required number of arguments is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <vm_name>"
exit 1
fi
vm_name=$1
port=$(( ( $RANDOM % 1000 ) + 5000 ))
ssh -L $port:127.0.0.1:$port root@$host_ip "~/forward_console.sh $vm_name $port"
## If you already have ssh keys set up and want to auto-launch your vnc viewer
## 1. add & to the end of the ssh command above
## 2. uncomment the two lines below
## 3. adjust sleep based on expected latency connecting ssh
## 4. execute your preferred VNC client
# sleep 2
# vncviewer 127.0.0.1:$port
#!/bin/bash
### This script should be saved on the XCP-ng host.
### This is called by the above script via SSH.
### NOTE: You must have socat installed. (yum install socat)
### Depending on XCP-ng version, it may already be installed.
### Usage: forward_console.sh <vm_name> <port>
# Check if the required number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <vm_name> <port>"
exit 1
fi
$vm_name=$1
$port=$2
# check vm-list for the dom id of the given vm
# only need the last line of output
output=$(xe vm-list params=dom-id name-label=$vm_name)
last_line=$(echo "$output" | tail-n 1)
# extract dom id from the output
id=$(echo $last_line | awk '{print $NF}')
if [[ $id =~ ^[0-9]+$ ]]; then
socat TCP-LISTEN:$port UNIX-CONNECT://var/run/xen/vnc-$id
else
echo "No dom id found for vm $vm_name."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment