Skip to content

Instantly share code, notes, and snippets.

@elkuno213
Last active March 31, 2025 08:40
Show Gist options
  • Save elkuno213/4eb921b320c7a8c53ba72ec24e0b48b3 to your computer and use it in GitHub Desktop.
Save elkuno213/4eb921b320c7a8c53ba72ec24e0b48b3 to your computer and use it in GitHub Desktop.
TigerVNC remote setup in Ubuntu

TigerVNC Installer

Installation

# For server
sh -c "$(wget -O- https://gist.githubusercontent.com/elkuno213/4eb921b320c7a8c53ba72ec24e0b48b3/raw/tigervnc_install.sh)" -- server

# For client
sh -c "$(wget -O- https://gist.githubusercontent.com/elkuno213/4eb921b320c7a8c53ba72ec24e0b48b3/raw/tigervnc_install.sh)" -- client

Usage

  • Automatically configures systemd service
  • Default password: password (change in script)
  • Opens port 5901 (configure firewall if needed)

Connection Methods

[Optional] Copy the server's password file into the client:

scp -rp <server-user>@<server-ip>:~/.vnc/passwd $HOME/.vnc/passwd.<server-user>
  1. Direct connection:
vncviewer localhost::5901 -via <server-user>@<server-ip> [-passwd $HOME/.vnc/passwd.<server-user>]
  1. SSH Tunnel:
ssh -Y -L 5901:localhost:5901 <server-user>@<server-ip>             # Terminal 1
vncviewer localhost::5901 [-passwd $HOME/.vnc/passwd.<server-user>] # Terminal 2

Troubleshooting

  • Gray screen: Ensure chmod +x ~/.vnc/xstartup on server
  • Service status: systemctl status vncserver@1
  • Connection issues: Verify SSH tunnel with netstat -tuln | grep 5901

Security

  • Change default password in script (DEFAULT_PASSWORD)
  • Use SSH tunnels instead of opening VNC ports
  • Configure firewall to restrict access to port 5901

Supports: Ubuntu/Debian (apt)

#!/bin/bash
#
# TigerVNC Installer Script
# -----------------------
# This script installs and configures TigerVNC server or client
# based on the provided argument.
#
# Usage: ./tigervnc_install.sh [server|client]
# Constants
DEFAULT_PASSWORD="password"
# Usage information
usage() {
echo "Usage: $0 [server|client]"
echo " server - Install and configure TigerVNC server"
echo " client - Install and configure TigerVNC client"
exit 1
}
# Check if argument is provided
if [ $# -ne 1 ]; then
usage
fi
install_server() {
echo "Installing TigerVNC server..."
# Determine package manager and install tigervnc server
if command -v apt-get &> /dev/null; then
apt-get update
apt-get install -y xfce4 xfce4-goodies
apt-get install -y tigervnc-standalone-server tigervnc-common tigervnc-xorg-extension
else
echo "Unsupported package manager. Please install TigerVNC server manually."
exit 1
fi
# Create necessary directories
mkdir -p $HOME/.vnc
# Set up VNC password
echo "Setting up default VNC password..."
echo "$DEFAULT_PASSWORD" | tigervncpasswd -f > $HOME/.vnc/passwd
chmod 600 $HOME/.vnc/passwd # Restrict permissions to owner only
echo "Default VNC password set successfully."
# Create a basic xstartup file if it doesn't exist
if [ ! -f $HOME/.vnc/xstartup ]; then
cat > $HOME/.vnc/xstartup << 'EOF'
#!/bin/sh
# Start up the standard system desktop
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
/usr/bin/startxfce4
EOF
chmod +x $HOME/.vnc/xstartup
fi
# Create systemd service file for VNC
# Consider whether running as root or regular user
if [ "$(id -u)" -eq 0 ]; then
# For root user
cat > /etc/systemd/system/[email protected] << 'EOF'
[Unit]
Description=TigerVNC service
After=syslog.target network.target
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/root
PIDFile=/root/.vnc/%H:590%i.pid
ExecStartPre=-/bin/sh -c "/usr/bin/tigervncserver -kill :%i > /dev/null 2>&1"
ExecStart=/usr/bin/tigervncserver -fg -depth 24 -geometry 1920x1080 -localhost no :%i
ExecStop=/usr/bin/tigervncserver -kill :%i
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable [email protected]
systemctl start [email protected]
else
# For regular user
mkdir -p $HOME/.config/systemd/user
cat > $HOME/.config/systemd/user/[email protected] << 'EOF'
[Unit]
Description=TigerVNC service
After=syslog.target network.target
[Service]
Type=simple
User=%u
Group=%u
WorkingDirectory=%h
PIDFile=%h/.vnc/%H:590%i.pid
ExecStartPre=-/bin/sh -c "/usr/bin/tigervncserver -kill :%i > /dev/null 2>&1"
ExecStart=/usr/bin/tigervncserver -fg -depth 24 -geometry 1920x1080 -localhost no :%i
ExecStop=/usr/bin/tigervncserver -kill :%i
[Install]
WantedBy=multi-user.target
EOF
systemctl --user daemon-reload
systemctl --user enable [email protected]
systemctl --user start [email protected]
fi
echo "TigerVNC server installed and configured successfully."
echo "The server is now running on display :1 (port 5901)"
echo "Make sure your firewall allows connections to port 5901"
}
install_client() {
echo "Installing TigerVNC client..."
# Determine package manager and install tigervnc client
if command -v apt-get &> /dev/null; then
apt-get update
apt-get install -y tigervnc-viewer
else
echo "Unsupported package manager. Please install TigerVNC client manually."
exit 1
fi
# Create .vnc directory
mkdir -p $HOME/.vnc
echo "TigerVNC client installed successfully."
echo
echo "Configuration Instructions:"
echo "--------------------------"
echo "- Copy the server's password file:"
echo " $ scp -rp <server-user>@<server-ip>:~/.vnc/passwd $HOME/.vnc/passwd.<server-user>"
echo
echo "- Connection methods:"
echo " 1. Direct connection:"
echo " $ vncviewer localhost::5901 -via <server-user>@<server-ip> -passwd $HOME/.vnc/passwd.<server-user>"
echo
echo " 2. SSH tunnel:"
echo " $ ssh -Y -L 5901:localhost:5901 <server-user>@<server-ip>" # Terminal 1
echo " $ vncviewer localhost::5901 -passwd $HOME/.vnc/passwd.<server-user>" # Terminal 2
}
# Main script logic
case "$1" in
server)
install_server
;;
client)
install_client
;;
*)
usage
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment