Initial commit
This commit is contained in:
23
.github/workflows/shellcheck.yml
vendored
Normal file
23
.github/workflows/shellcheck.yml
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
name: ShellCheck Lint
|
||||
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- 'x11vnc-setup.sh'
|
||||
pull_request:
|
||||
paths:
|
||||
- 'x11vnc-setup.sh'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
shellcheck:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install ShellCheck
|
||||
run: sudo apt-get update && sudo apt-get install -y shellcheck
|
||||
|
||||
- name: Run ShellCheck (strict)
|
||||
run: shellcheck -x -e SC1090,SC1091 -f gcc ./x11vnc-setup.sh
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Dom
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
140
README.md
Normal file
140
README.md
Normal file
@@ -0,0 +1,140 @@
|
||||
[](https://github.com/domomg/x11vnc-setup/actions/workflows/shellcheck.yml)
|
||||
|
||||
# x11vnc-setup
|
||||
A cross-distro Bash script to install and configure x11vnc as a systemd service that attaches to an existing X11 session, just like Windows Remote Desktop or macOS Screen Sharing do.
|
||||
|
||||
Unlike most VNC setups that spin up a separate virtual desktop, this script gives you **real-time access to your actual desktop session**, making it perfect for remote administration of your workstation.
|
||||
|
||||
---
|
||||
|
||||
## Key Features
|
||||
|
||||
- Works across multiple distros:
|
||||
Ubuntu, Debian, Linux Mint, CentOS/RHEL/Rocky, Fedora, Arch Linux, Manjaro, openSUSE
|
||||
- Secure password authentication (stored properly, never echoed)
|
||||
- Systemd integration: Runs in the background and survives reboots
|
||||
- Interactive setup: Guides you through user, port, and password configuration
|
||||
- Hardened service: Systemd protections (`ProtectSystem`, `ProtectHome`, etc.)
|
||||
- Clean and validated: Handles dependencies, checks for common pitfalls
|
||||
|
||||
---
|
||||
|
||||
## Supported Platforms
|
||||
|
||||
Tested and working on:
|
||||
- Debian-based: Ubuntu, Debian, Linux Mint
|
||||
- RHEL-based: CentOS, RHEL, Rocky, AlmaLinux
|
||||
- Fedora
|
||||
- Arch Linux/Manjaro
|
||||
- openSUSE/SLES
|
||||
|
||||
If you’re running something else obscure, install `x11vnc` manually first, then re-run the script.
|
||||
|
||||
---
|
||||
|
||||
## Quick Start
|
||||
|
||||
1. Clone the repository and run the script as root:
|
||||
|
||||
```bash
|
||||
chmod +x x11vnc-setup.sh
|
||||
sudo ./x11vnc-setup.sh
|
||||
```
|
||||
2. Follow the prompts to:
|
||||
|
||||
- Set a VNC password
|
||||
|
||||
- Choose which user to run the service as
|
||||
|
||||
- (Optionally) configure a custom port
|
||||
|
||||
3. The script will:
|
||||
|
||||
- Install `x11vnc` if missing
|
||||
|
||||
- Install `autocutsel` to enable copy-paste support between the remote viewer and the host
|
||||
|
||||
- Configure the systemd service (auto-starts on boot)
|
||||
|
||||
- Show you how to connect, open the firewall, and check logs
|
||||
|
||||
---
|
||||
|
||||
## Notes
|
||||
|
||||
- The VNC password is stored securely and never echoed.
|
||||
|
||||
- The systemd service runs under the chosen user and uses `ProtectSystem`, `ProtectHome`, and other security settings.
|
||||
|
||||
- You can further restrict access with firewall rules or SSH tunnels.
|
||||
|
||||
---
|
||||
|
||||
## Known Limitations
|
||||
|
||||
|
||||
### 1. Wayland is not supported
|
||||
`x11vnc` **only works with X11** (XFCE, Mate, Cinnamon, etc).
|
||||
|
||||
If you’re on Wayland, you probably **don't need this script at all.**
|
||||
|
||||
**GNOME Desktop (either Wayland or X11)**:
|
||||
- Comes with built-in remote desktop support via `gnome-remote-desktop`
|
||||
- Go to `Settings -> Sharing -> Screen Sharing` and enable it
|
||||
- `gnome-remote-desktop` supports both VNC and RDP protocols
|
||||
|
||||
**KDE Plasma (either Wayland or X11)**:
|
||||
- Built-in RDP server support
|
||||
- Go to **System Settings -> Workspace -> Remote Desktop**
|
||||
- Enable RDP server for remote access
|
||||
|
||||
**Other Wayland environments (like Sway)**
|
||||
- This script won't work at all on them
|
||||
- Use Wayland native solutions, such as `wayvnc`
|
||||
|
||||
### 2. The script assumes display `:0`
|
||||
Fine for single-user desktops, but could be wrong for headless or multi-session machines.
|
||||
|
||||
**Solution**: Edit the `DISPLAY` in the systemd service if needed.
|
||||
|
||||
### 3. Logging to `/var/log/x11vnc.log` might fail for non-root users
|
||||
The script directs logs to /var/log/x11vnc.log, but permissions might block non-root users.
|
||||
|
||||
**Solution**: Remove the `-o` flag and rely on `journalctl` logs.
|
||||
|
||||
### 4. Double forking via `-bg` and systemd `Type=forking`
|
||||
The script uses both `-bg` and `Type=forking`. It works, but it’s not elegant.
|
||||
|
||||
**Solution**: Remove `-bg` and rely on systemd's forking behavior, or change `Type=simple`.
|
||||
|
||||
---
|
||||
|
||||
## Uninstallation
|
||||
|
||||
To remove x11vnc completely, use the provided uninstall script:
|
||||
|
||||
1. Clone the repository and run the script as root:
|
||||
|
||||
```bash
|
||||
chmod +x uninstall.sh
|
||||
sudo ./uninstall.sh
|
||||
```
|
||||
2. The script will:
|
||||
|
||||
- Stop and remove the `x11vnc` systemd service
|
||||
|
||||
- Remove all config files and directories
|
||||
|
||||
- Uninstall `x11vnc` and `autocutsel`
|
||||
|
||||
- Provide optional instructions to cleanup the firewall rules (if any)
|
||||
|
||||
---
|
||||
|
||||
License: MIT. Use freely, I'm not responsible if things break or catch fire.
|
||||
|
||||
Author: Dom
|
||||
|
||||
GitHub: @domomg
|
||||
|
||||
Contributions welcome. If you find a bug or need support for another distribution, feel free to open an issue or PR.
|
||||
335
uninstall.sh
Normal file
335
uninstall.sh
Normal file
@@ -0,0 +1,335 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Enhanced x11vnc VNC Server Uninstall Script
|
||||
# Author: Dom (@domomg)
|
||||
# Supports: Ubuntu, Debian, Linux Mint, CentOS/RHEL/Rocky, Fedora, Arch Linux, openSUSE
|
||||
|
||||
set -euo pipefail # Exit on error, undefined vars, pipe failures
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m'
|
||||
VNC_CONFIG_DIR="/etc/x11vnc"
|
||||
X11VNC_SERVICE="/etc/systemd/system/x11vnc.service"
|
||||
AUTOCUTSEL_SERVICE="/etc/systemd/system/autocutsel.service"
|
||||
LOG_FILE="/var/log/x11vnc.log"
|
||||
|
||||
log() {
|
||||
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
|
||||
}
|
||||
error() {
|
||||
echo -e "${RED}[ERROR] ✗${NC} $1" >&2
|
||||
}
|
||||
warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
info() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
success() {
|
||||
echo -e "${GREEN}[SUCCESS] ✓${NC} $1"
|
||||
}
|
||||
check_root() {
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
error "This script must be run as root (use sudo)"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
detect_distro() {
|
||||
if [[ -f /etc/os-release ]]; then
|
||||
source /etc/os-release
|
||||
DISTRO=$ID
|
||||
else
|
||||
error "Cannot detect Linux distribution"
|
||||
exit 1
|
||||
fi
|
||||
info "Detected distribution: $PRETTY_NAME"
|
||||
}
|
||||
|
||||
stop_and_disable_service() {
|
||||
log "Stopping and disabling x11vnc service..."
|
||||
if systemctl is-active --quiet x11vnc.service 2>/dev/null; then
|
||||
if systemctl stop x11vnc.service; then
|
||||
success "x11vnc service stopped"
|
||||
else
|
||||
warning "Failed to stop x11vnc service"
|
||||
fi
|
||||
else
|
||||
info "x11vnc service is not running"
|
||||
fi
|
||||
if systemctl is-enabled --quiet x11vnc.service 2>/dev/null; then
|
||||
if systemctl disable x11vnc.service; then
|
||||
success "x11vnc service disabled"
|
||||
else
|
||||
warning "Failed to disable x11vnc service"
|
||||
fi
|
||||
else
|
||||
info "x11vnc service is not enabled"
|
||||
fi
|
||||
}
|
||||
|
||||
stop_and_disable_autocutsel() {
|
||||
log "Stopping and disabling autocutsel service..."
|
||||
if systemctl is-active --quiet autocutsel.service 2>/dev/null; then
|
||||
if systemctl stop autocutsel.service; then
|
||||
success "autocutsel service stopped"
|
||||
else
|
||||
warning "Failed to stop autocutsel service"
|
||||
fi
|
||||
else
|
||||
info "autocutsel service is not running"
|
||||
fi
|
||||
|
||||
if systemctl is-enabled --quiet autocutsel.service 2>/dev/null; then
|
||||
if systemctl disable autocutsel.service; then
|
||||
success "autocutsel service disabled"
|
||||
else
|
||||
warning "Failed to disable autocutsel service"
|
||||
fi
|
||||
else
|
||||
info "autocutsel service is not enabled"
|
||||
fi
|
||||
}
|
||||
|
||||
remove_service_files() {
|
||||
log "Removing systemd service files..."
|
||||
for svc in "$X11VNC_SERVICE" "$AUTOCUTSEL_SERVICE"; do
|
||||
if [[ -f "$svc" ]]; then
|
||||
rm -f "$svc"
|
||||
success "Removed: $svc"
|
||||
else
|
||||
info "Service file not found: $svc"
|
||||
fi
|
||||
done
|
||||
systemctl daemon-reload
|
||||
}
|
||||
|
||||
|
||||
kill_running_processes() {
|
||||
log "Terminating any running x11vnc and autocutsel processes..."
|
||||
if pgrep -x x11vnc >/dev/null; then
|
||||
pkill -x x11vnc
|
||||
success "x11vnc processes terminated"
|
||||
else
|
||||
info "No running x11vnc processes found"
|
||||
fi
|
||||
if pgrep -x autocutsel >/dev/null; then
|
||||
pkill -x autocutsel
|
||||
success "autocutsel processes terminated"
|
||||
else
|
||||
info "No running autocutsel processes found"
|
||||
fi
|
||||
}
|
||||
|
||||
remove_config_files() {
|
||||
log "Removing configuration files and directories..."
|
||||
if [[ -d "$VNC_CONFIG_DIR" ]]; then
|
||||
rm -rf "$VNC_CONFIG_DIR"
|
||||
success "Configuration directory removed: $VNC_CONFIG_DIR"
|
||||
else
|
||||
info "Configuration directory not found: $VNC_CONFIG_DIR"
|
||||
fi
|
||||
if [[ -f "$LOG_FILE" ]]; then
|
||||
rm -f "$LOG_FILE"
|
||||
success "Log file removed: $LOG_FILE"
|
||||
else
|
||||
info "Log file not found: $LOG_FILE"
|
||||
fi
|
||||
}
|
||||
|
||||
remove_autocutsel_helper_script() {
|
||||
local helper_script="/usr/local/bin/start-autocutsel.sh"
|
||||
log "Removing autocutsel helper script..."
|
||||
if [[ -f "$helper_script" ]]; then
|
||||
rm -f "$helper_script"
|
||||
success "Removed autocutsel helper script: $helper_script"
|
||||
else
|
||||
info "Autocutsel helper script not found: $helper_script"
|
||||
fi
|
||||
}
|
||||
|
||||
uninstall_packages() {
|
||||
log "Uninstalling x11vnc and autocutsel packages..."
|
||||
case $DISTRO in
|
||||
ubuntu|debian|linuxmint)
|
||||
if dpkg -l | grep -q "^ii.*x11vnc"; then
|
||||
apt remove -y x11vnc
|
||||
success "x11vnc package removed"
|
||||
else
|
||||
info "x11vnc package not installed"
|
||||
fi
|
||||
if dpkg -l | grep -q "^ii.*autocutsel"; then
|
||||
apt remove -y autocutsel
|
||||
success "autocutsel package removed"
|
||||
else
|
||||
info "autocutsel package not installed"
|
||||
fi
|
||||
apt autoremove -y
|
||||
;;
|
||||
centos|rhel|rocky|almalinux)
|
||||
if command -v dnf &> /dev/null; then
|
||||
if rpm -q x11vnc &>/dev/null; then
|
||||
dnf remove -y x11vnc
|
||||
success "x11vnc package removed"
|
||||
else
|
||||
info "x11vnc package not installed"
|
||||
fi
|
||||
if rpm -q autocutsel &>/dev/null; then
|
||||
dnf remove -y autocutsel
|
||||
success "autocutsel package removed"
|
||||
else
|
||||
info "autocutsel package not installed"
|
||||
fi
|
||||
dnf autoremove -y
|
||||
else
|
||||
if rpm -q x11vnc &>/dev/null; then
|
||||
yum remove -y x11vnc
|
||||
success "x11vnc package removed"
|
||||
else
|
||||
info "x11vnc package not installed"
|
||||
fi
|
||||
if rpm -q autocutsel &>/dev/null; then
|
||||
yum remove -y autocutsel
|
||||
success "autocutsel package removed"
|
||||
else
|
||||
info "autocutsel package not installed"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
fedora)
|
||||
if rpm -q x11vnc &>/dev/null; then
|
||||
dnf remove -y x11vnc
|
||||
success "x11vnc package removed"
|
||||
else
|
||||
info "x11vnc package not installed"
|
||||
fi
|
||||
if rpm -q autocutsel &>/dev/null; then
|
||||
dnf remove -y autocutsel
|
||||
success "autocutsel package removed"
|
||||
else
|
||||
info "autocutsel package not installed"
|
||||
fi
|
||||
dnf autoremove -y
|
||||
;;
|
||||
arch|manjaro)
|
||||
if pacman -Qi x11vnc &>/dev/null; then
|
||||
pacman -Rs --noconfirm x11vnc
|
||||
success "x11vnc package removed"
|
||||
else
|
||||
info "x11vnc package not installed"
|
||||
fi
|
||||
if pacman -Qi autocutsel &>/dev/null; then
|
||||
pacman -Rs --noconfirm autocutsel
|
||||
success "autocutsel package removed"
|
||||
else
|
||||
info "autocutsel package not installed"
|
||||
fi
|
||||
;;
|
||||
opensuse*|sles)
|
||||
if rpm -q x11vnc &>/dev/null; then
|
||||
zypper remove -y x11vnc
|
||||
success "x11vnc package removed"
|
||||
else
|
||||
info "x11vnc package not installed"
|
||||
fi
|
||||
if rpm -q autocutsel &>/dev/null; then
|
||||
zypper remove -y autocutsel
|
||||
success "autocutsel package removed"
|
||||
else
|
||||
info "autocutsel package not installed"
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
warning "Unsupported distribution: $DISTRO"
|
||||
info "Please manually remove x11vnc and autocutsel packages"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
show_firewall_info() {
|
||||
echo -e "${YELLOW}"
|
||||
echo "FIREWALL CLEANUP:"
|
||||
echo "If you (optionally) opened VNC ports in your firewall, you may want to close them:"
|
||||
echo ""
|
||||
echo "For UFW (Ubuntu/Debian):"
|
||||
echo " sudo ufw delete allow 5900/tcp"
|
||||
echo " (Replace 5900 with your custom port if different)"
|
||||
echo ""
|
||||
echo "For firewalld (CentOS/RHEL/Fedora):"
|
||||
echo " sudo firewall-cmd --permanent --remove-port=5900/tcp"
|
||||
echo " sudo firewall-cmd --reload"
|
||||
echo ""
|
||||
echo "For iptables:"
|
||||
echo " sudo iptables -D INPUT -p tcp --dport 5900 -j ACCEPT"
|
||||
echo -e "${NC}"
|
||||
}
|
||||
|
||||
confirm_uninstall() {
|
||||
echo -e "${YELLOW}"
|
||||
echo "WARNING: This will completely remove x11vnc VNC server and all its configuration!"
|
||||
echo "The following actions will be performed:"
|
||||
echo " - Stop and disable x11vnc systemd service"
|
||||
echo " - Remove systemd service file"
|
||||
echo " - Kill any running x11vnc and autocutsel processes"
|
||||
echo " - Remove configuration directory: $VNC_CONFIG_DIR"
|
||||
echo " - Remove log file: $LOG_FILE"
|
||||
echo " - Uninstall x11vnc and autocutsel packages"
|
||||
echo -e "${NC}"
|
||||
while true; do
|
||||
read -r -p "Are you sure you want to proceed? (yes/no): " confirm
|
||||
echo -e "${NC}"
|
||||
case "$confirm" in
|
||||
yes)
|
||||
break
|
||||
;;
|
||||
no)
|
||||
info "Uninstall cancelled by user"
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
warning "Please type the full word 'yes' or 'no'"
|
||||
echo -e "${RED}"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
main() {
|
||||
echo -e "${YELLOW}"
|
||||
echo "=============================================="
|
||||
echo " Enhanced x11vnc VNC Server Uninstall Script"
|
||||
echo " Multi-distribution support"
|
||||
echo " Author: Dom (@domomg on GitHub)"
|
||||
echo "=============================================="
|
||||
echo -e "${NC}"
|
||||
info "Starting x11vnc uninstallation..."
|
||||
check_root
|
||||
detect_distro
|
||||
confirm_uninstall
|
||||
stop_and_disable_service
|
||||
stop_and_disable_autocutsel
|
||||
remove_service_files
|
||||
remove_autocutsel_helper_script
|
||||
kill_running_processes
|
||||
remove_config_files
|
||||
uninstall_packages
|
||||
show_firewall_info
|
||||
echo -e "${GREEN}"
|
||||
echo "==============================================="
|
||||
echo "x11vnc VNC Server Uninstall Complete!"
|
||||
echo "==============================================="
|
||||
echo ""
|
||||
echo "All x11vnc components have been removed:"
|
||||
echo " -> Systemd service stopped and disabled"
|
||||
echo " -> Service file removed"
|
||||
echo " -> Running processes terminated"
|
||||
echo " -> Configuration files removed"
|
||||
echo " -> Packages uninstalled"
|
||||
echo ""
|
||||
echo "The system has been cleaned up successfully."
|
||||
echo -e "${NC}"
|
||||
success "Uninstallation completed successfully!"
|
||||
}
|
||||
main "$@"
|
||||
388
x11vnc-setup.sh
Normal file
388
x11vnc-setup.sh
Normal file
@@ -0,0 +1,388 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Enhanced x11vnc VNC Server Setup Script
|
||||
# Author: Dom (@domomg on Github)
|
||||
# Supports: Ubuntu, Debian, Linux Mint, CentOS/RHEL/Rocky, Fedora, Arch Linux, openSUSE
|
||||
|
||||
set -euo pipefail # Exit on error, undefined vars, pipe failures
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
VNC_CONFIG_DIR="/etc/x11vnc"
|
||||
VNC_PASSWORD_FILE="$VNC_CONFIG_DIR/vncpwd"
|
||||
SERVICE_FILE="/etc/systemd/system/x11vnc.service"
|
||||
VNC_PORT=5900
|
||||
VNC_USER=""
|
||||
|
||||
log() {
|
||||
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
|
||||
}
|
||||
error() {
|
||||
echo -e "${RED}[ERROR] ✗${NC} $1" >&2
|
||||
}
|
||||
warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
info() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
success() {
|
||||
echo -e "${GREEN}[SUCCESS] ✓${NC} $1"
|
||||
}
|
||||
|
||||
check_root() {
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
error "This script must be run as root (use sudo)"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
detect_distro() {
|
||||
if [[ -f /etc/os-release ]]; then
|
||||
source /etc/os-release
|
||||
DISTRO=$ID
|
||||
else
|
||||
error "Cannot detect Linux distribution"
|
||||
exit 1
|
||||
fi
|
||||
info "Detected distribution: $PRETTY_NAME"
|
||||
}
|
||||
|
||||
install_x11vnc() {
|
||||
log "Installing x11vnc and autocutsel..."
|
||||
case $DISTRO in
|
||||
ubuntu|debian|linuxmint)
|
||||
apt update
|
||||
apt install -y x11vnc autocutsel
|
||||
;;
|
||||
centos|rhel|rocky|almalinux)
|
||||
# Enable EPEL for CentOS/RHEL
|
||||
if command -v dnf &> /dev/null; then
|
||||
dnf install -y epel-release
|
||||
dnf install -y x11vnc autocutsel
|
||||
else
|
||||
yum install -y epel-release
|
||||
yum install -y x11vnc autocutsel
|
||||
fi
|
||||
;;
|
||||
fedora)
|
||||
dnf install -y x11vnc autocutsel
|
||||
;;
|
||||
arch|manjaro)
|
||||
pacman -Sy --noconfirm x11vnc autocutsel
|
||||
;;
|
||||
opensuse*|sles)
|
||||
zypper install -y x11vnc autocutsel
|
||||
;;
|
||||
*)
|
||||
error "Unsupported distribution: $DISTRO"
|
||||
info "Please install x11vnc manually and re-run this script"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
if ! command -v x11vnc &> /dev/null; then
|
||||
error "x11vnc installation failed"
|
||||
exit 1
|
||||
fi
|
||||
success "x11vnc installed successfully"
|
||||
}
|
||||
|
||||
setup_config_dir() {
|
||||
log "Setting up configuration directory..."
|
||||
if [[ ! -d "$VNC_CONFIG_DIR" ]]; then
|
||||
mkdir -p "$VNC_CONFIG_DIR"
|
||||
chmod 755 "$VNC_CONFIG_DIR"
|
||||
fi
|
||||
}
|
||||
|
||||
setup_password() {
|
||||
log "Setting up VNC password..."
|
||||
echo -e "${YELLOW}"
|
||||
echo "VNC Password Setup:"
|
||||
echo "- Password must be 8 characters or less (VNC limitation)"
|
||||
echo "- Choose a strong password for security"
|
||||
echo "- You'll be prompted to confirm the password"
|
||||
echo -e "${NC}"
|
||||
[[ -f "$VNC_PASSWORD_FILE" ]] && rm -f "$VNC_PASSWORD_FILE"
|
||||
if ! x11vnc -storepasswd "$VNC_PASSWORD_FILE"; then
|
||||
error "Failed to set VNC password"
|
||||
exit 1
|
||||
fi
|
||||
chmod 600 "$VNC_PASSWORD_FILE"
|
||||
success "VNC password configured successfully"
|
||||
}
|
||||
|
||||
get_vnc_user() {
|
||||
echo -e "${BLUE}"
|
||||
read -r -p "Enter username to run VNC as (leave empty and press Enter for root): " VNC_USER
|
||||
echo -e "${NC}"
|
||||
if [[ -n "$VNC_USER" ]]; then
|
||||
if ! id "$VNC_USER" &>/dev/null; then
|
||||
error "User '$VNC_USER' does not exist"
|
||||
exit 1
|
||||
fi
|
||||
chown -R "$VNC_USER:$VNC_USER" "$VNC_CONFIG_DIR"
|
||||
info "VNC will run as user: $VNC_USER"
|
||||
else
|
||||
VNC_USER="root"
|
||||
warning "VNC will run as root (not recommended for security)"
|
||||
fi
|
||||
}
|
||||
|
||||
configure_port() {
|
||||
echo -e "${BLUE}"
|
||||
read -r -p "Enter VNC port (leave empty and press Enter for default 5900): " custom_port
|
||||
echo -e "${NC}"
|
||||
if [[ -n "$custom_port" ]]; then
|
||||
if [[ "$custom_port" =~ ^[0-9]+$ ]] && [[ "$custom_port" -ge 1024 ]] && [[ "$custom_port" -le 65535 ]]; then
|
||||
VNC_PORT="$custom_port"
|
||||
info "VNC port set to: $VNC_PORT"
|
||||
else
|
||||
warning "Invalid port. Using default 5900"
|
||||
VNC_PORT=5900
|
||||
fi
|
||||
else
|
||||
info "Using default VNC port: 5900"
|
||||
fi
|
||||
}
|
||||
|
||||
create_systemd_service() {
|
||||
log "Creating systemd service..."
|
||||
local after_target="graphical.target"
|
||||
local wanted_by="graphical.target"
|
||||
local exec_start="/usr/bin/x11vnc"
|
||||
exec_start+=" -auth guess"
|
||||
exec_start+=" -forever"
|
||||
exec_start+=" -noxdamage"
|
||||
exec_start+=" -repeat"
|
||||
exec_start+=" -rfbauth $VNC_PASSWORD_FILE"
|
||||
exec_start+=" -rfbport $VNC_PORT"
|
||||
exec_start+=" -shared"
|
||||
exec_start+=" -display :0"
|
||||
exec_start+=" -bg"
|
||||
exec_start+=" -o /var/log/x11vnc.log"
|
||||
exec_start+=" -noprimary"
|
||||
exec_start+=" -alwaysshared"
|
||||
cat > "$SERVICE_FILE" << EOL
|
||||
[Unit]
|
||||
Description=x11vnc VNC Server
|
||||
Documentation=man:x11vnc(1)
|
||||
After=$after_target
|
||||
Wants=$after_target
|
||||
|
||||
[Service]
|
||||
Type=forking
|
||||
User=$VNC_USER
|
||||
Group=$VNC_USER
|
||||
ExecStart=$exec_start
|
||||
ExecStop=/usr/bin/pkill -f x11vnc
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectHome=read-only
|
||||
ProtectSystem=strict
|
||||
ReadWritePaths=/var/log
|
||||
|
||||
[Install]
|
||||
WantedBy=$wanted_by
|
||||
EOL
|
||||
chmod 644 "$SERVICE_FILE"
|
||||
success "Systemd service created"
|
||||
}
|
||||
|
||||
create_autocutsel_helper_script() {
|
||||
log "Creating autocutsel helper script to run as active user..."
|
||||
cat > /usr/local/bin/start-autocutsel.sh << 'EOF'
|
||||
#!/bin/bash
|
||||
# Find the active graphical user session
|
||||
user=$(loginctl list-sessions --no-legend | awk '{print $1}' | while read session; do
|
||||
state=$(loginctl show-session "$session" -p Active --value)
|
||||
if [[ "$state" == "yes" ]]; then
|
||||
loginctl show-session "$session" -p Name --value
|
||||
break
|
||||
fi
|
||||
done)
|
||||
if [[ -z "$user" ]]; then
|
||||
echo "No active graphical user found" >&2
|
||||
exit 1
|
||||
fi
|
||||
# Set DISPLAY and XAUTHORITY for the user
|
||||
display=:0
|
||||
xauth="/home/$user/.Xauthority"
|
||||
if [[ ! -f "$xauth" ]]; then
|
||||
echo "Xauthority file not found at $xauth" >&2
|
||||
exit 1
|
||||
fi
|
||||
exec sudo -u "$user" DISPLAY=$display XAUTHORITY=$xauth /usr/bin/autocutsel
|
||||
EOF
|
||||
chmod +x /usr/local/bin/start-autocutsel.sh
|
||||
success "Autocutsel helper script created at /usr/local/bin/start-autocutsel.sh"
|
||||
}
|
||||
|
||||
create_autocutsel_service() {
|
||||
log "Creating autocutsel systemd service..."
|
||||
local service_path="/etc/systemd/system/autocutsel.service"
|
||||
cat > "$service_path" << EOL
|
||||
[Unit]
|
||||
Description=autocutsel clipboard sync
|
||||
After=graphical.target
|
||||
Wants=graphical.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/usr/local/bin/start-autocutsel.sh
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
|
||||
[Install]
|
||||
WantedBy=graphical.target
|
||||
EOL
|
||||
chmod 644 "$service_path"
|
||||
success "autocutsel systemd service created"
|
||||
}
|
||||
|
||||
enable_service() {
|
||||
log "Enabling and starting x11vnc service..."
|
||||
systemctl daemon-reload
|
||||
if systemctl enable x11vnc.service; then
|
||||
success "Service enabled successfully"
|
||||
else
|
||||
error "Failed to enable service"
|
||||
exit 1
|
||||
fi
|
||||
if systemctl start x11vnc.service; then
|
||||
success "Service started successfully"
|
||||
else
|
||||
error "Failed to start service"
|
||||
info "Check service status with: systemctl status x11vnc.service"
|
||||
info "Check logs with: journalctl -u x11vnc.service -f"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log "Enabling and starting autocutsel service..."
|
||||
systemctl daemon-reload
|
||||
if systemctl enable autocutsel.service; then
|
||||
success "Service enabled successfully"
|
||||
else
|
||||
error "Failed to enable service"
|
||||
exit 1
|
||||
fi
|
||||
if systemctl start autocutsel.service; then
|
||||
success "Service started successfully"
|
||||
else
|
||||
error "Failed to start service"
|
||||
info "Check service status with: systemctl status autocutsel.service"
|
||||
info "Check logs with: journalctl -u autocutsel.service -f"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
check_service_status() {
|
||||
log "Checking service status..."
|
||||
if systemctl is-active --quiet x11vnc.service; then
|
||||
success "x11vnc service is running"
|
||||
if ss -tlnp | grep -q ":$VNC_PORT "; then
|
||||
success "VNC server is listening on port $VNC_PORT"
|
||||
else
|
||||
warning "VNC server may not be listening on port $VNC_PORT"
|
||||
fi
|
||||
else
|
||||
error "x11vnc service is not running"
|
||||
info "Check status with: systemctl status x11vnc.service"
|
||||
fi
|
||||
}
|
||||
|
||||
show_firewall_info() {
|
||||
echo -e "${YELLOW}"
|
||||
echo "FIREWALL CONFIGURATION:"
|
||||
echo "You may need to open port $VNC_PORT in your firewall:"
|
||||
echo ""
|
||||
echo "For UFW (Ubuntu/Debian):"
|
||||
echo " sudo ufw allow $VNC_PORT/tcp"
|
||||
echo ""
|
||||
echo "For firewalld (CentOS/RHEL/Fedora):"
|
||||
echo " sudo firewall-cmd --permanent --add-port=$VNC_PORT/tcp"
|
||||
echo " sudo firewall-cmd --reload"
|
||||
echo ""
|
||||
echo "For iptables:"
|
||||
echo " sudo iptables -A INPUT -p tcp --dport $VNC_PORT -j ACCEPT"
|
||||
echo -e "${NC}"
|
||||
}
|
||||
|
||||
show_connection_info() {
|
||||
local ip_address
|
||||
ip_address=$(hostname -I | awk '{print $1}')
|
||||
echo -e "${GREEN}"
|
||||
echo "==============================================="
|
||||
echo "x11vnc VNC Server Setup Complete!"
|
||||
echo "==============================================="
|
||||
echo ""
|
||||
echo "Connection Information:"
|
||||
echo " Server IP: $ip_address"
|
||||
echo " VNC Port: $VNC_PORT"
|
||||
echo " Running as: $VNC_USER"
|
||||
echo ""
|
||||
echo "Connect using a VNC client:"
|
||||
echo " Address: $ip_address:$VNC_PORT"
|
||||
echo " Or: $ip_address::$VNC_PORT"
|
||||
echo ""
|
||||
echo "Service Management:"
|
||||
echo " Status: systemctl status x11vnc.service"
|
||||
echo " Stop: systemctl stop x11vnc.service"
|
||||
echo " Start: systemctl start x11vnc.service"
|
||||
echo " Logs: journalctl -u x11vnc.service -f"
|
||||
echo ""
|
||||
echo "Configuration files:"
|
||||
echo " Service: $SERVICE_FILE"
|
||||
echo " Password: $VNC_PASSWORD_FILE"
|
||||
echo " Log: /var/log/x11vnc.log"
|
||||
echo -e "${NC}"
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
if [[ -f "$SERVICE_FILE" ]]; then
|
||||
systemctl stop x11vnc.service 2>/dev/null || true
|
||||
systemctl disable x11vnc.service 2>/dev/null || true
|
||||
rm -f "$SERVICE_FILE"
|
||||
fi
|
||||
[[ -d "$VNC_CONFIG_DIR" ]] && rm -rf "$VNC_CONFIG_DIR"
|
||||
error "Installation interrupted and cleaned up"
|
||||
exit 1
|
||||
}
|
||||
|
||||
main() {
|
||||
trap cleanup INT TERM
|
||||
echo -e "${YELLOW}"
|
||||
echo "=============================================="
|
||||
echo " Enhanced x11vnc VNC Server Setup Script"
|
||||
echo " Multi-distribution support with security"
|
||||
echo " Author: Dom (@domomg on GitHub)"
|
||||
echo "=============================================="
|
||||
echo -e "${NC}"
|
||||
info "Starting x11vnc installation and configuration..."
|
||||
check_root
|
||||
detect_distro
|
||||
echo -e "${BLUE}"
|
||||
read -r -p "Press Enter to continue with installation..." </dev/tty
|
||||
echo -e "${NC}"
|
||||
install_x11vnc
|
||||
setup_config_dir
|
||||
get_vnc_user
|
||||
configure_port
|
||||
setup_password
|
||||
create_systemd_service
|
||||
create_autocutsel_helper_script
|
||||
create_autocutsel_service
|
||||
enable_service
|
||||
check_service_status
|
||||
show_firewall_info
|
||||
show_connection_info
|
||||
success "Installation completed successfully!"
|
||||
}
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user