Skip to content

Instantly share code, notes, and snippets.

@zigforge
Last active September 30, 2024 12:26
Show Gist options
  • Select an option

  • Save zigforge/e599c0875b82c8daf5f93aac1e0d3b36 to your computer and use it in GitHub Desktop.

Select an option

Save zigforge/e599c0875b82c8daf5f93aac1e0d3b36 to your computer and use it in GitHub Desktop.
rdp_setup.sh (IPv6)
#!/bin/bash
# Usage: sudo ./enhanced_rdp_setup.sh
set -e
# Error handling
handle_error() {
echo "An error occurred on line $1"
exit 1
}
trap 'handle_error $LINENO' ERR
# Logging
exec > >(tee -i /var/log/rdp_setup.log)
exec 2>&1
echo "Starting RDP setup at $(date)"
# Check if script is run as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
check_ubuntu_version() {
if [[ $(lsb_release -rs) < "20.04" ]]; then
echo "This script requires Ubuntu 20.04 or newer"
exit 1
fi
}
update_system() {
echo "Updating system..."
apt-get update && apt-get upgrade -y
apt-get install -y net-tools netfilter-persistent telnet
}
install_ubuntu_desktop() {
echo "Installing Ubuntu Desktop and xRDP..."
apt-get install -y ubuntu-desktop xrdp ubuntu-restricted-extras
ubuntu-drivers autoinstall
systemctl enable xrdp
systemctl start xrdp
}
configure_xrdp() {
echo "Configuring xRDP..."
adduser xrdp ssl-cert
echo "exec gnome-session" >> /etc/xrdp/startwm.sh
# Ensure xRDP is listening on all interfaces
sed -i 's/port=3389/port=0.0.0.0:3389/g' /etc/xrdp/xrdp.ini
systemctl restart xrdp
}
install_xrdp_pa() {
echo "Installing PulseAudio support for xRDP..."
apt-get install -y git libpulse-dev autoconf m4 intltool build-essential dpkg-dev libtool libsndfile1-dev libspeexdsp-dev libudev-dev pulseaudio
cp /etc/apt/sources.list /etc/apt/sources.list.u2ad
sed -Ei 's/^# deb-src /deb-src /' /etc/apt/sources.list
apt-get update -y
apt build-dep pulseaudio -y
cd /tmp
apt source pulseaudio
pulsever=$(pulseaudio --version | awk '{print $2}')
cd /tmp/pulseaudio-$pulsever
./configure
git clone https://github.com/neutrinolabs/pulseaudio-module-xrdp.git
cd pulseaudio-module-xrdp
./bootstrap
./configure PULSE_DIR="/tmp/pulseaudio-$pulsever"
make
cd /tmp/pulseaudio-$pulsever/pulseaudio-module-xrdp/src/.libs
install -t "/var/lib/xrdp-pulseaudio-installer" -D -m 644 *.so
systemctl restart xrdp
fix_pa_systemd_issue
}
fix_pa_systemd_issue() {
echo "Fixing PulseAudio systemd issue..."
for user in /home/*; do
username=$(basename "$user")
if [ -d "$user" ]; then
echo "Configuring PulseAudio for user: $username"
mkdir -p "$user/.config/systemd/user/"
ln -s /dev/null "$user/.config/systemd/user/pulseaudio.service"
mkdir -p "$user/.config/autostart/"
cat <<EOF | tee "$user/.config/autostart/pulseaudio.desktop"
[Desktop Entry]
Type=Application
Exec=pulseaudio
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=pulseaudio
Name=pulseaudio
Comment[en_US]=pulseaudio
Comment=pulseaudio
EOF
chown -R "$username:$username" "$user/.config/"
chmod -R 755 "$user/.config/"
fi
done
}
remove_iptables_rules() {
echo "Removing iptables rules..."
iptables -F
netfilter-persistent save
echo "iptables rules have been flushed and saved."
}
verify_rdp_setup() {
echo "Verifying RDP setup..."
echo "Checking xRDP service status:"
systemctl status xrdp
echo "Checking RDP port status:"
netstat -nat | grep 3389
echo "Testing local RDP connection:"
telnet localhost 3389
echo "Current iptables rules:"
iptables -L
echo "Network interface information:"
ip addr
}
get_username() {
while true; do
read -p "Enter the username for the new user: " username
if [[ "$username" =~ ^[a-z_][a-z0-9_-]*$ ]]; then
echo "$username"
return 0
else
echo "Invalid username. Use only lowercase letters, numbers, underscores, and hyphens. Start with a letter or underscore."
fi
done
}
create_user() {
NEW_USERNAME=$(get_username)
echo "Creating new user: $NEW_USERNAME"
adduser --gecos "" "$NEW_USERNAME"
usermod -aG sudo "$NEW_USERNAME"
echo "$NEW_USERNAME ALL=(ALL:ALL) /usr/sbin/reboot, /usr/sbin/poweroff" | EDITOR='tee -a' visudo -f /etc/sudoers.d/$NEW_USERNAME
chmod 0440 /etc/sudoers.d/$NEW_USERNAME
usermod -aG ssl-cert "$NEW_USERNAME"
echo "User $NEW_USERNAME has been created and configured for RDP access."
echo "Please ensure you set a strong password for this user."
echo "$NEW_USERNAME"
}
configure_rdp_for_user() {
username="$1"
su - "$username" -c 'echo "gnome-session" > ~/.xsession'
su - "$username" -c 'chmod +x ~/.xsession'
su - "$username" -c 'gsettings set org.gnome.desktop.interface enable-animations false'
su - "$username" -c 'gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type "nothing"'
}
main() {
check_ubuntu_version
update_system
install_ubuntu_desktop
configure_xrdp
install_xrdp_pa
remove_iptables_rules
verify_rdp_setup
new_users=()
while true; do
new_user=$(create_user)
new_users+=("$new_user")
configure_rdp_for_user "$new_user"
read -p "Create another user? (y/n) " another
[[ $another =~ ^[Nn]$ ]] && break
done
echo "System setup complete!"
echo "Ubuntu Desktop and xRDP have been installed and configured."
echo "iptables rules have been removed to allow external RDP connections."
echo "The following users have been created and configured for RDP access:"
for user in "${new_users[@]}"; do
echo "- $user"
done
echo "You can now connect to your Ubuntu desktop using an RDP client on port 3389."
echo "Use the IP address of this machine and the username/password you just created."
echo "Rebooting system to apply all changes..."
reboot
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment