Skip to content

Instantly share code, notes, and snippets.

@sycomix
Last active June 30, 2025 07:18
Show Gist options
  • Select an option

  • Save sycomix/d1f21c002560729ac90fb2e5580a5217 to your computer and use it in GitHub Desktop.

Select an option

Save sycomix/d1f21c002560729ac90fb2e5580a5217 to your computer and use it in GitHub Desktop.
rdp and vnc unfucker for linux (ubuntu/debian)
#!/bin/bash
# ==============================================================================
#
# Ubuntu xRDP & VNC Troubleshooter
#
# Description:
# This script is designed to diagnose and fix common and advanced issues with
# xRDP and VNC on Ubuntu systems. It checks services, ports, configurations,
# firewall settings, and startup scripts.
#
# Usage:
# Run the script with sudo: sudo ./xrdp_fixer.sh
#
# Author: Gemini
# Version: 1.2
#
# ==============================================================================
# --- Color Definitions ---
C_RESET='\033[0m'
C_RED='\033[0;31m'
C_GREEN='\033[0;32m'
C_YELLOW='\033[0;33m'
C_BLUE='\033[0;34m'
C_CYAN='\033[0;36m'
# --- Helper Functions ---
function print_info() {
echo -e "${C_BLUE}[INFO] $1${C_RESET}"
}
function print_success() {
echo -e "${C_GREEN}[SUCCESS] $1${C_RESET}"
}
function print_warning() {
echo -e "${C_YELLOW}[WARNING] $1${C_RESET}"
}
function print_error() {
echo -e "${C_RED}[ERROR] $1${C_RESET}"
}
function check_root() {
if [[ $EUID -ne 0 ]]; then
print_error "This script must be run as root. Please use 'sudo'."
exit 1
fi
}
function press_enter_to_continue() {
read -p "Press [Enter] to continue..."
}
# --- Diagnostic Functions ---
function check_service_status() {
print_info "Checking status of xrdp and xrdp-sesman services..."
systemctl is-active --quiet xrdp && print_success "xrdp service is active." || print_error "xrdp service is NOT active."
systemctl is-enabled --quiet xrdp && print_success "xrdp service is enabled on boot." || print_warning "xrdp service is NOT enabled on boot."
echo "" # Newline for readability
systemctl is-active --quiet xrdp-sesman && print_success "xrdp-sesman service is active." || print_error "xrdp-sesman service is NOT active."
press_enter_to_continue
}
function check_listening_ports() {
print_info "Checking listening ports..."
print_info "xRDP should be listening on port 3389."
if ss -tlnp | grep -q ':3389'; then
print_success "Port 3389 is open and listening."
print_info "$(ss -tlnp | grep ':3389')"
else
print_error "Port 3389 is NOT listening. xRDP may not be running correctly."
fi
press_enter_to_continue
}
function check_firewall() {
print_info "Checking UFW (Uncomplicated Firewall) status..."
if ! command -v ufw &> /dev/null; then
print_warning "UFW is not installed. Skipping firewall check."
press_enter_to_continue
return
fi
if ufw status | grep -q "Status: active"; then
print_success "UFW is active."
print_info "Checking for a rule allowing port 3389..."
if ufw status | grep -q "3389.*ALLOW"; then
print_success "UFW rule allows traffic on port 3389."
else
print_warning "UFW is active but no rule found for port 3389. This will block connections."
fi
else
print_warning "UFW is inactive. It is not blocking connections, but consider enabling it for security."
fi
press_enter_to_continue
}
function check_desktop_session_file() {
local user_home
if [ -n "$SUDO_USER" ]; then
user_home=$(getent passwd "$SUDO_USER" | cut -d: -f6)
else
user_home=$HOME
fi
print_info "Checking for desktop session file (~/.xsession) for user '$SUDO_USER'..."
local xsession_file="$user_home/.xsession"
if [ -f "$xsession_file" ]; then
print_success "Found ~/.xsession file."
print_info "Contents:"
echo -e "${C_CYAN}"
cat "$xsession_file"
echo -e "${C_RESET}"
else
print_warning "The ~/.xsession file does not exist. This can cause a blank screen on login."
print_info "You can create one from the 'Fix' menu."
fi
press_enter_to_continue
}
function view_xrdp_logs() {
print_info "Displaying the last 20 lines of xrdp-sesman.log..."
print_warning "Look for errors like 'X server for display 10 startup failed' or permission issues."
echo -e "${C_CYAN}--- /var/log/xrdp-sesman.log ---${C_RESET}"
tail -n 20 /var/log/xrdp-sesman.log
echo ""
press_enter_to_continue
}
# --- Fix Functions ---
function fix_install_packages() {
print_info "This will install/reinstall xrdp and a desktop environment."
print_warning "Recommended desktop for xRDP: XFCE4."
read -p "Do you want to install xrdp and xfce4? (y/N): " choice
if [[ "$choice" =~ ^[Yy]$ ]]; then
print_info "Updating package lists..."
apt-get update
print_info "Installing xrdp and xfce4..."
apt-get install --reinstall -y xrdp xfce4 xfce4-goodies
if [ $? -eq 0 ]; then
print_success "Packages installed successfully."
else
print_error "Package installation failed."
fi
else
print_info "Skipping package installation."
fi
press_enter_to_continue
}
function fix_restart_services() {
print_info "Restarting xrdp service..."
systemctl restart xrdp
sleep 1 # Give it a moment
systemctl is-active --quiet xrdp && print_success "xrdp restarted successfully." || print_error "Failed to restart xrdp."
press_enter_to_continue
}
function fix_create_xsession() {
local user_home
if [ -n "$SUDO_USER" ]; then
user_home=$(getent passwd "$SUDO_USER" | cut -d: -f6)
local user_name="$SUDO_USER"
else
print_error "Cannot determine the user. Please run with 'sudo' from a user account."
press_enter_to_continue
return
fi
local xsession_file="$user_home/.xsession"
print_info "This will create an ~/.xsession file for user '$user_name'."
print_warning "This is crucial for telling xRDP which desktop environment to start."
PS3="Select the desktop environment to configure: "
options=("XFCE4 (Recommended)" "MATE" "Cancel")
select opt in "${options[@]}"; do
case $opt in
"XFCE4 (Recommended)")
print_info "Creating .xsession for XFCE4..."
echo "xfce4-session" > "$xsession_file"
break
;;
"MATE")
print_info "Creating .xsession for MATE..."
echo "mate-session" > "$xsession_file"
break
;;
"Cancel")
print_info "Operation cancelled."
return
;;
*) echo "Invalid option $REPLY";;
esac
done
chown "$user_name:$user_name" "$xsession_file"
chmod 744 "$xsession_file"
print_success "File '$xsession_file' created/updated successfully for user '$user_name'."
press_enter_to_continue
}
function fix_firewall_rule() {
if ! command -v ufw &> /dev/null; then
print_warning "UFW is not installed. Nothing to do."
press_enter_to_continue
return
fi
print_info "Adding UFW rule to allow connections on port 3389..."
ufw allow 3389/tcp
if ufw status | grep -q "3389.*ALLOW"; then
print_success "Firewall rule for port 3389 added successfully."
print_info "Reloading firewall to apply changes..."
ufw reload
else
print_error "Failed to add firewall rule."
fi
press_enter_to_continue
}
function fix_polkit() {
print_info "Applying Polkit (PolicyKit) fix for authentication dialogs..."
print_info "This prevents a common issue where the session hangs waiting for a password."
local polkit_file="/etc/polkit-1/localauthority/50-local.d/45-allow-colord.pkla"
mkdir -p /etc/polkit-1/localauthority/50-local.d/
cat > "$polkit_file" <<EOF
[Allow Colord all Users]
Identity=unix-user:*
Action=org.freedesktop.color-manager.create-device;org.freedesktop.color-manager.create-profile;org.freedesktop.color-manager.delete-device;org.freedesktop.color-manager.delete-profile;org.freedesktop.color-manager.modify-device;org.freedesktop.color-manager.modify-profile
ResultAny=yes
ResultInactive=yes
ResultActive=yes
EOF
if [ -f "$polkit_file" ]; then
print_success "Polkit rule created at '$polkit_file'."
else
print_error "Failed to create Polkit rule."
fi
press_enter_to_continue
}
# --- ADVANCED FIXES ---
function fix_startwm() {
local startwm_path="/etc/xrdp/startwm.sh"
print_info "Applying fix to $startwm_path"
print_warning "This is a common solution for blank screens or 'X server could not be started' errors."
if [ -f "$startwm_path" ]; then
local backup_path="${startwm_path}.bak.$(date +%F-%T)"
print_info "Backing up original file to $backup_path"
cp "$startwm_path" "$backup_path"
fi
# Write the new, corrected startwm.sh content
cat > "$startwm_path" <<'EOF'
#!/bin/sh
#
# xrdp X session startup script for Ubuntu
#
# Unset DBUS_SESSION_BUS_ADDRESS and XDG_RUNTIME_DIR to avoid conflicts
unset DBUS_SESSION_BUS_ADDRESS
unset XDG_RUNTIME_DIR
# Source the system-wide environment variables
if [ -f /etc/profile ]; then
. /etc/profile
fi
# Check for the user's .xsession file and run it.
# This is the recommended way to start a session.
if [ -r ~/.xsession ]; then
. ~/.xsession
exit 0
fi
# Fallback for systems that might not have .xsession set up yet.
if [ -f /etc/X11/Xsession ]; then
. /etc/X11/Xsession
exit 0
fi
# A final, basic fallback
startxfce4
exit 0
EOF
print_success "$startwm_path has been updated."
print_info "This fix requires an xRDP service restart to take effect."
press_enter_to_continue
}
# --- Main Menu Logic ---
function main_menu() {
while true; do
clear
echo -e "${C_CYAN}===========================================${C_RESET}"
echo -e "${C_CYAN} Ubuntu xRDP & VNC Troubleshooter ${C_RESET}"
echo -e "${C_CYAN} Version 1.2 ${C_RESET}"
echo -e "${C_CYAN}===========================================${C_RESET}"
echo ""
echo " --- BASIC DIAGNOSTICS ---"
echo " 1) Check Service Status (xrdp, xrdp-sesman)"
echo " 2) Check Listening Ports (3389)"
echo " 3) Check Firewall Status (UFW)"
echo " 4) Check Desktop Session File (~/.xsession)"
echo " 5) View xRDP Logs"
echo ""
echo " --- BASIC FIXES ---"
echo " A) Install/Reinstall xRDP + XFCE4 Desktop"
echo " B) Create/Fix Desktop Session File (~/.xsession)"
echo " C) Add Firewall Rule for xRDP (Port 3389)"
echo " D) Apply Polkit Fix (Prevents Auth Hangs)"
echo ""
echo -e " --- ${C_YELLOW}ADVANCED FIXES${C_RESET} ---"
echo -e " ${C_YELLOW}S) Fix startwm.sh script (HIGHLY RECOMMENDED FIX)${C_RESET}"
echo ""
echo " --- ACTIONS ---"
echo " E) Restart xRDP Service"
echo -e "${C_GREEN} F) RUN ALL RECOMMENDED FIXES${C_RESET}"
echo ""
echo -e "${C_RED} Q) Quit${C_RESET}"
echo ""
read -rp "Select an option: " choice
case $choice in
1) check_service_status ;;
2) check_listening_ports ;;
3) check_firewall ;;
4) check_desktop_session_file ;;
5) view_xrdp_logs ;;
[Aa]) fix_install_packages ;;
[Bb]) fix_create_xsession ;;
[Cc]) fix_firewall_rule ;;
[Dd]) fix_polkit ;;
[Ss]) fix_startwm ;;
[Ee]) fix_restart_services ;;
[Ff])
print_info "Running all recommended fixes..."
fix_install_packages
fix_create_xsession
fix_firewall_rule
fix_polkit
fix_startwm # Added the new advanced fix
fix_restart_services
print_success "All recommended fixes have been applied."
print_info "A system reboot is recommended for all changes to take full effect."
press_enter_to_continue
;;
[Qq])
print_info "Exiting script."
exit 0
;;
*)
print_error "Invalid option. Please try again."
sleep 1
;;
esac
done
}
# --- Script Entry Point ---
check_root
main_menu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment