Created
October 22, 2025 07:23
-
-
Save wasimosmanhome/faae37a96e800baa1ad91b928509d50a to your computer and use it in GitHub Desktop.
VNC Installation (Ubuntu)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Define constants | |
| VNC_SERVER_PACKAGE="tightvncserver" | |
| VNC_USER_PORT="5901" | |
| VNC_SERVICE_NAME="vncserver@${VNC_USER_PORT}.service" | |
| USER_NAME=$(whoami) | |
| DISPLAY_NUMBER=":1" | |
| LOCAL_IP="10.0.0.119" | |
| echo "======================================================" | |
| echo " VNC Server Setup Script for Ubuntu" | |
| echo " Target IP: ${LOCAL_IP}" | |
| echo " Display Number: ${DISPLAY_NUMBER}" | |
| echo "======================================================" | |
| # --- 1. CLEANUP PREVIOUS INSTALLATIONS AND RELATED FILES --- | |
| echo "--- 1. Cleaning up previous VNC installations and files... ---" | |
| # Remove common VNC packages (TightVNC, RealVNC, etc.) | |
| sudo apt-get purge -y tigervnc-standalone-server tigervnc-common tightvncserver vnc4server xvnc4viewer 2>/dev/null || echo "No common VNC packages found for purging." | |
| # Remove VNC configuration directory for the current user | |
| rm -rf "$HOME/.vnc" | |
| echo "Removed ${HOME}/.vnc directory." | |
| # Remove TightVNC Server startup script if it exists | |
| sudo rm -f "/etc/init.d/tightvncserver" | |
| echo "Removed legacy startup script." | |
| # Remove systemd service file if it exists | |
| sudo systemctl disable ${VNC_SERVICE_NAME} 2>/dev/null | |
| sudo rm -f "/etc/systemd/system/${VNC_SERVICE_NAME}" | |
| sudo systemctl daemon-reload 2>/dev/null | |
| echo "Removed existing systemd service file." | |
| # --- 2. INSTALL VNC SERVER AND DEPENDENCIES --- | |
| echo "--- 2. Updating package list and installing dependencies... ---" | |
| # Update package list | |
| sudo apt update | |
| # Install TightVNC Server and a desktop environment (XFCE is lightweight and common for VNC) | |
| sudo apt install -y ${VNC_SERVER_PACKAGE} xfce4 xfce4-goodies | |
| echo "Installed ${VNC_SERVER_PACKAGE}, XFCE4, and dependencies." | |
| # --- 3. INITIAL VNC SETUP AND PASSWORD CONFIGURATION --- | |
| echo "--- 3. Configuring VNC password... ---" | |
| # Start the server once to create the initial configuration directory and set the password | |
| # If this is the first run, it will prompt for the password. | |
| vncserver ${DISPLAY_NUMBER} | |
| # Kill the temporary server instance | |
| vncserver -kill ${DISPLAY_NUMBER} 2>/dev/null || echo "Temporary VNC session on ${DISPLAY_NUMBER} killed or was not running." | |
| # --- 4. CONFIGURE XSTARTUP FOR DESKTOP ENVIRONMENT --- | |
| echo "--- 4. Configuring VNC to launch XFCE desktop... ---" | |
| # Backup original xstartup file | |
| mv "$HOME/.vnc/xstartup" "$HOME/.vnc/xstartup.bak" | |
| # Create new xstartup file for XFCE | |
| cat << EOF > "$HOME/.vnc/xstartup" | |
| #!/bin/bash | |
| xrdb "$HOME/.Xresources" | |
| startxfce4 & | |
| EOF | |
| # Make the xstartup script executable | |
| chmod +x "$HOME/.vnc/xstartup" | |
| echo "Created executable xstartup file for XFCE." | |
| # --- 5. CREATE SYSTEMD SERVICE FOR AUTO-START (Launch every time Ubuntu starts up) --- | |
| echo "--- 5. Creating systemd service for VNC autostart... ---" | |
| # Create the service file content | |
| SERVICE_CONFIG=$(cat << EOF | |
| [Unit] | |
| Description=TightVNC Server at ${DISPLAY_NUMBER} | |
| After=syslog.target network.target | |
| [Service] | |
| Type=forking | |
| User=${USER_NAME} | |
| PIDFile=/home/${USER_NAME}/.vnc/%H${DISPLAY_NUMBER}.pid | |
| ExecStartPre=-/usr/bin/vncserver -kill ${DISPLAY_NUMBER} | |
| ExecStart=/usr/bin/vncserver ${DISPLAY_NUMBER} -geometry 1280x800 -depth 24 | |
| ExecStop=/usr/bin/vncserver -kill ${DISPLAY_NUMBER} | |
| [Install] | |
| WantedBy=multi-user.target | |
| EOF | |
| ) | |
| # Write the configuration to the systemd directory | |
| echo "${SERVICE_CONFIG}" | sudo tee "/etc/systemd/system/${VNC_SERVICE_NAME}" > /dev/null | |
| # Reload systemd, enable the service, and start it | |
| sudo systemctl daemon-reload | |
| sudo systemctl enable ${VNC_SERVICE_NAME} | |
| echo "Systemd service ${VNC_SERVICE_NAME} created and enabled for autostart." | |
| # --- 6. CHECK AND DYNAMICALLY RESOLVE STATUS --- | |
| echo "--- 6. Checking VNC Server status and ensuring it is running... ---" | |
| # Start the service | |
| sudo systemctl start ${VNC_SERVICE_NAME} | |
| # Check the status | |
| STATUS_CHECK=$(sudo systemctl is-active ${VNC_SERVICE_NAME}) | |
| if [ "${STATUS_CHECK}" == "active" ]; then | |
| echo "✅ VNC Server is ACTIVE (Running)." | |
| else | |
| echo "❌ VNC Server is INACTIVE. Attempting to restart..." | |
| # Dynamic resolution: If it's not active, try restarting or checking logs. | |
| # The 'systemctl start' above should have covered a simple 'not running' state. | |
| # We re-run 'start' just in case and check again. | |
| sudo systemctl restart ${VNC_SERVICE_NAME} | |
| sleep 5 # Wait for the service to restart | |
| STATUS_CHECK_AFTER_RESTART=$(sudo systemctl is-active ${VNC_SERVICE_NAME}) | |
| if [ "${STATUS_CHECK_AFTER_RESTART}" == "active" ]; then | |
| echo "✅ VNC Server successfully started after dynamic resolution." | |
| else | |
| echo "❌ FAILED to start VNC Server. Check system logs for details:" | |
| echo " (Example: journalctl -xe | grep vncserver)" | |
| exit 1 | |
| fi | |
| fi | |
| # --- 7. PRINT CONNECTION ADDRESS --- | |
| echo "======================================================" | |
| echo " VNC Server Setup Complete! 🎉" | |
| echo "======================================================" | |
| echo "VNC Viewer needs to connect to the following address:" | |
| echo "" | |
| echo " ${LOCAL_IP}${DISPLAY_NUMBER}" | |
| echo " (i.e., IP address 10.0.0.119 and port 5901)" | |
| echo "" | |
| echo "Note: If you need to change your VNC password, run: vncpasswd" | |
| echo "Note: If you cannot connect, ensure your firewall (ufw) allows port ${VNC_USER_PORT}." | |
| echo " (Command: sudo ufw allow ${VNC_USER_PORT}/tcp)" | |
| echo "======================================================" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment