Skip to content

Instantly share code, notes, and snippets.

@elkuno213
Last active October 23, 2025 15:01
Show Gist options
  • Select an option

  • Save elkuno213/4eb921b320c7a8c53ba72ec24e0b48b3 to your computer and use it in GitHub Desktop.

Select an option

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)" -- <password> server [root|user]

# For client
sh -c "$(wget -O- https://gist.githubusercontent.com/elkuno213/4eb921b320c7a8c53ba72ec24e0b48b3/raw/tigervnc_install.sh)" -- <password> 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 arguments.
#
# Usage: ./install_vnc.sh <password> server [root|user]
# ./install_vnc.sh <password> client
# Usage information
usage() {
echo "Usage: $0 <password> [server|client] [root|user]"
echo " <password> - VNC password to set (first argument, required)"
echo " server - Install and configure TigerVNC server"
echo " root - Install VNC server as root user (requires third argument)"
echo " user - Install VNC server for current user (requires third argument)"
echo " If run with sudo, installs for the user who invoked sudo"
echo " client - Install and configure TigerVNC client"
echo ""
echo "Examples:"
echo " $0 mypassword server user"
echo " $0 mypassword client"
exit 1
}
# Check if minimum arguments are provided
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
usage
fi
# Store password from first argument
VNC_PASSWORD="$1"
# Check if server option requires third argument
if [ "$2" = "server" ] && [ $# -ne 3 ]; then
echo "Error: 'server' option requires a third argument [root|user]"
usage
fi
install_server() {
local install_as="$1"
# Validate the install_as argument
if [ "$install_as" != "root" ] && [ "$install_as" != "user" ]; then
echo "Error: Invalid installation type. Must be 'root' or 'user'."
exit 1
fi
# Determine the actual user and home directory
if [ "$install_as" = "user" ]; then
# Get the actual user (handle sudo case)
if [ -n "$SUDO_USER" ]; then
ACTUAL_USER="$SUDO_USER"
ACTUAL_HOME=$(eval echo ~$SUDO_USER)
ACTUAL_UID=$(id -u $SUDO_USER)
ACTUAL_GID=$(id -g $SUDO_USER)
else
ACTUAL_USER="$USER"
ACTUAL_HOME="$HOME"
ACTUAL_UID=$(id -u)
ACTUAL_GID=$(id -g)
fi
echo "Installing TigerVNC server for user: $ACTUAL_USER"
else
ACTUAL_USER="root"
ACTUAL_HOME="/root"
echo "Installing TigerVNC server as root..."
fi
# 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 $ACTUAL_HOME/.vnc
if [ "$install_as" = "user" ] && [ -n "$SUDO_USER" ]; then
chown -R $ACTUAL_UID:$ACTUAL_GID $ACTUAL_HOME/.vnc
fi
# Set up VNC password
echo "Setting up VNC password..."
echo "$VNC_PASSWORD" | tigervncpasswd -f > $ACTUAL_HOME/.vnc/passwd
chmod 600 $ACTUAL_HOME/.vnc/passwd
if [ "$install_as" = "user" ] && [ -n "$SUDO_USER" ]; then
chown $ACTUAL_UID:$ACTUAL_GID $ACTUAL_HOME/.vnc/passwd
fi
echo "VNC password set successfully."
# Create a basic xstartup file if it doesn't exist
if [ ! -f $ACTUAL_HOME/.vnc/xstartup ]; then
cat > $ACTUAL_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 $ACTUAL_HOME/.vnc/xstartup
if [ "$install_as" = "user" ] && [ -n "$SUDO_USER" ]; then
chown $ACTUAL_UID:$ACTUAL_GID $ACTUAL_HOME/.vnc/xstartup
fi
fi
# Create systemd service file for VNC based on specified installation type
if [ "$install_as" = "root" ]; then
# For root user
cat > /etc/systemd/system/vncserver@.service << '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 vncserver@1.service
systemctl start vncserver@1.service
else
# For regular user
mkdir -p $ACTUAL_HOME/.config/systemd/user
if [ -n "$SUDO_USER" ]; then
chown -R $ACTUAL_UID:$ACTUAL_GID $ACTUAL_HOME/.config
fi
cat > $ACTUAL_HOME/.config/systemd/user/vncserver@.service <<EOF
[Unit]
Description=TigerVNC service
After=syslog.target network.target
[Service]
Type=simple
WorkingDirectory=$ACTUAL_HOME
PIDFile=$ACTUAL_HOME/.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=default.target
EOF
if [ -n "$SUDO_USER" ]; then
chown $ACTUAL_UID:$ACTUAL_GID $ACTUAL_HOME/.config/systemd/user/vncserver@.service
# Run systemctl as the actual user
su - $ACTUAL_USER -c "systemctl --user daemon-reload"
su - $ACTUAL_USER -c "systemctl --user enable vncserver@1.service"
su - $ACTUAL_USER -c "systemctl --user start vncserver@1.service"
else
systemctl --user daemon-reload
systemctl --user enable vncserver@1.service
systemctl --user start vncserver@1.service
fi
fi
echo "TigerVNC server installed and configured successfully for user: $ACTUAL_USER"
echo "The server is now running on display :1 (port 5901)"
echo "VNC password has been set to the provided password"
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 "$2" in
server)
# Validate third argument for server installation
if [ "$3" != "root" ] && [ "$3" != "user" ]; then
echo "Error: Third argument must be 'root' or 'user'"
usage
fi
install_server "$3"
;;
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