xRDP – Easy install xRDP on Ubuntu 18.04,20.04,21.04,21.10 (Script Version 1.3)

Hello World, 

Since Ubuntu 21.10 has been released (October 2021), it’s time for us to update and publish the latest version of our famous xrdp-installer script. As we tend to follow the Ubuntu life cycle,  our script try to include supported version of Ubuntu Operating system.   This new release will include support for Ubuntu 21.10 but will also be merging all the work we have done and tested in the preview version script we have released in June 2021.

The new version of our famous xrdp installation script will be set to 1.3 (i.e. xrdp-installer-1.3.sh).  This script replaces all previous versions.   

So, let’s go ! 

Overview

The xrdp-installer-1.3.sh script ease the installation and post-configuration actions of xRDP on top of Ubuntu Operating System.  However, this new script release introduce many changes that might help installing and configuring xrdp against more Linux distribution and more Desktop Interfaces.   

What’s new in this release (Version 1.3) ?

Most of the changes that are included in Version 1.3 of the script is based on the “preview script version”  released in June 2021.  Let’s see what’s new….

Adding Ubuntu 21.10 as supported OS !

The script xrdp-installer-1.3.sh can be run against all supported Ubuntu Operating System which are listed hereafter

  • Ubuntu 18.04.x
  • Ubuntu 20.04.x
  • Ubuntu 21.04.X
  • Ubuntu 21.10.x

Removed Ubuntu 20.10 as supported OS !

Since Ubuntu 20.10  will reach end of life in July 2021, we have decided to update the script and to remove support for Ubuntu 20.10.  By design, the script will not execute on Ubuntu 20.10 Edition.   If you still want to run the script against Ubuntu 20.10, you can update the script and add the support for this specific version or use a previous version of the script.

Added Debian as supported OS 

Recently, we have released a debian-xrdp-installer-1.0 script version that perform the installation and configuration of xrdp software against Debian Operating system.  This version of the script support Debian 10 and probably Debian 11.  However, instead of creating two versions of the xrdp-installer script, we have decided to merge the Debian script with the main xrdp-installer script.   

So, xrdp-installer script version 1.3 will include and support Debian 10 and Debian 11

Important Notes : 

If you are planning to use xRDP on top of Debian 11, please be sure to read the following post 

We recommend you to use the Custom installation mode on Debian 11 as long as the bug detected in Debian package is not fixed…..

Added Pop!_OS as “Detected” OS 

Since we have been requested to publish some posts around Pop!_OS, we have included support for Pop!_OS operating system (20.04,21.04).  The script will not run against Pop!_OS 20.10. since this version has reached end of life…

Added Budgie Desktop as “Detected” Desktop Interface

Budgie Desktop requires some specific settings in the .xsessionrc file.  The script version 1.3 detect if the budgie Desktop is used.  If this is the case, the script will modify accordingly the /etc/startwm.sh in order to be able to connect to the budgie desktop.  

Detecting Desktop Interface in use  

This version of the script will try to detect the Desktop Interface runing (Gnome,KDE,LXDE,…) and adapt the post configuration actions accordingly.  Since this is still work in progress status, we have not tested all the possible combinations. You can always perform some tests and provide feedback to us. Based on this, we will see if it would be possible to adapt and support more Desktop interface. 

Please note that the script will detect the desktop type at run time and will update the /etc/xrdp/startwm.sh. In other words,  if you have run the script with Gnome Desktop and then you decide to change desktop interface, this change will not be reflected in the remote session.  You will need to either update the /etc/xrdp/startwm.sh to include the correct information.

Sound redirection in Ubuntu 21.10 

In Ubuntu 21.10, pulseaudio version in use is 15.0.  The previous version of the script (1.2.3) throw an error because the way to detect the pulseaudio version does not work on Ubuntu 21.10.  The detection method has been changed to overcome this situation.  However, sound redirection option using code in xrdp-installer-1.2.3.sh will not work either.  Indeed, the pulseaudio package 15.0.x does not use anymore the ./configure approach.  This way of compiling has been removed and replaced by the meson technology and ninja technology. 

snd_xrdp1.3

Click on picture for better resolution

So, the script includes a number of changes that will allow to compile and redirect sound device against Ubuntu 21.10

Gnome Tweaks Tool not installed anymore by default  

The xrdp-installer script version 1.3 will not install anymore by default the application Tweak on systems running Gnome Desktop.  The code is still present in the script but will not run.  The Tweak tool per se is not really needed in order to have xRDP solution working.  This was more for (our) convenience that the original version of the script was including this option.   Moreover, the Tweak application has gone through some important changes.  The package name has changed and the extensions are not managed anymore via Tweaks but via the application Extensions.   So, if a user wants or need to have tweaking tools available on their system, they will have to install them manually….   

Let’s us know if this make sense or not…..

Minor Bug Fixing, Code structure changes, Cleaning up code 

We have found some small bugs or minor issues that have been fixed or improved in this release. This script version includes the necessary code to detect the desktop interface in used while running the script. To detect “supported” OS, the script is now using a function which has been moved at the top of the script.   Finally, since each operating system and/or Desktop interface might have specific settings, there is a new function created that would check if some additional prerequisites are needed before moving forward…  As you can see, there are some changes that have been introduced in this preview version…

Download the Script 

The script can be download at this location  :    xrdp-installer-1.3.zip 

Note : All the xrdp scripts can be found at https://www.c-nergy.be/products.html

How to Use the Script

The xrdp-installer-1.3 script will work in a similar way as the previous versions. The following section will explain how to download, extract, set executable mode and perform the installation using the script.  The script provides some switches that can be used to customize the installation process.

So, let’s see how this would be working…. 

Disclaimer

The script is provided AS IS.  Use it at your own risk !!! 

Prerequisites and assumptions  

The following conditions should be met in order to have the best results while using the script

  • We assume that your machine is connected to internet while performing the installation.  This is needed as additional packages and software needs to be downloaded and installed on your Ubuntu machine

Script version & Supported Ubuntu Version

The xrdp-installer-1.3.sh script will support the following Ubuntu operating system version 

  • Ubuntu 18.04.x
  • Ubuntu 20.04.x
  • Ubuntu 21.04.x
  • Ubuntu 21.04.x
  • Debian 10 
  • Debian 11 (only if you select custom mode installation !!!) 

The xrdp-installer-1.3.sh script can be run against the following Ubuntu flavors but we cannot guarantee that everything will work as expected

  • Kubuntu (20.04 and later)
  • Ubuntu Budgie (20.04 and later)
  • Ubuntu Mate (20.04 and later)
  • Xubuntu (20.04 and later)
  • Lubuntu (20.04 and later)   

Step 1 – Download the script 

To download the script, you have multiple options.   You can from a Terminal issue the following command 

wget https://www.c-nergy.be/downloads/xRDP/xrdp-installer-1.3.zip

You can also simply use your browser and click on the link to download the script :  

Step 2 – unzip the file 

After downloading the zip package containing the file, you will need to unzip it first. To unzip the package, you can use the Terminal console and issue the following command 

unzip xrdp-installer-1.3.zip 

You can also use the GUI and the Nautilus file manager to select the downloaded package, right-click on it and select the option Extract Here

Step 2 – Set Execute Right on the script

Download the xrdp-installer-1.3.sh script to your system, extract content and mark it as executable . To do this, perform the following action in a terminal console

chmod +x  ~/Downloads/xrdp-installer-1.3.sh

Note : Adjust the path where the xrdp-Installer-1.3.sh script to reflect your environment

Step 2 – Run as normal user 

The script needs to be run as a normal user.  The script will start running and will prompt you for password when sudo actions are initiated.  If you run the script as root or using sudo command, the Download folder does not exist for the root user and the script fails to run as expected.   To overcome this situation, the script also check which user is executing the script. If the script is run under sudo or root accounts, a warning message will be displayed and the script will not execute

Step 3 – Switches and parameters 

The xrdp-installer-1.3.sh script can be executed as is (with no parameters or switches). This will perform a standard (and basic) installation of xRDP.  No sound or custom xrdp login script will be made available.  However, remote desktop, drive redirection and clipboard redirection would be available with this installation mode.  

The xrdp-installer-1.3.sh script accept also some additional parameters that will help customize the xrdp installation and enable additional features.  The script provides the following parameters 

--help or -h          => will display a basic help menu
--sound or -s         => will enable sound redirection 
--loginscreen or -l   => will customize the xRDP login screen 
--remove or -r        => will remove the xrdp package 
--custom or -c        => will perform a custom installation (i.e. compiled from sources)

Step 4 – Perform xRDP installation using the script 

The following sections will provide some examples and scenario on how you can use the script….. 

Standard Installation – No Parameter

As mentioned above, you can decide to use the default xrdp package available in the Ubuntu repository and perform a basic installation which will provide you remote desktop capability and will also customize the remote sesssion.  The script will ensure that the same look n’ feel will be maintained when the user logs on locally or remotely. 

To perform a standard installation, you execute the script with no parameters.  Open a Terminal console, browse to the location where the script has been downloaded and issue the following command 

 ./xrdp-installer-1.3.sh

When the xrdp installation package will start, you be requested to enter your password. Provide the password and proceed with the installation. Wait for completion of the script.  The machine will not reboot automatically when done so you can review the actions performed by the script….

Standard Installation with Parameters

The script can be used to perform a standard installation and still passes some parameters to get the most of the xRDP packages and their features.  For all the Ubuntu versions, we could use the following combination of parameters 

./xrdp-installer-1.3.sh -s      (this would enable the sound redirection) 
./xrdp-installer-1.3.sh -l      (this would customize the xrdp login screen -see here)

Obviously, you can combine the switches (any order) to have more features enabled  

./xrdp-installer-1.3.sh -s -l      (this would enable the sound redirection and customize the xrdp login screen)

Important Notes

The script can be run multiple times on the same machine.  If you have run initially the script with no parameters but you decide afterward to enable additional features, the script can be re-executed and the additional features will be enabled.  

Custom Installation – Default Installation 

Custom installation script is really flexible. Custom installation means that the script will compile the xrdp package from the source binaries. The custom installation script always uses the latest xrdp package version available 

To perform a custom installation, you will need at minimum to pass one parameter.  The following command line shows you how to perform a custom installation  

./xrdp-installer-1.3.sh -c     (this would perform a custom installation but will not enabled adv features like sound redirection or custom login screen)

Custom Installation – Additional Options  

If a user wants to enable additional features, you will pass some additional parameters as shown in the following examples 

./xrdp-installer-1.3.sh -c -s     (this would perform a custom install and enable sound redirection)
./xrdp-installer-1.3.sh -c -l     (this would perform a custom install and customize xrdp login screen)
./xrdp-installer-1.3.sh -c -l -s  (this would perform a custom install, enable sound and customize login screen)

Important Notes

The script can be run multiple times on the same machine.  If you have run initially the script with no parameters but you decide afterward to enable additional features, the script can be re-executed and the additional features will be enabled.  

Remove Installation option

This version of the script also ship with the option to remove xRDP package.  The removal option should be only used  if you have uses this version of this script to perform the installation.  To remove the xrdp package, you would simply execute the following command  

./xrdp-installer-1.3.sh -r     (to remove the xrdp packages)

Step 5 – Test your configuration 

After the script has run and after the reboot/shutdown and start process, it’s time to test and see if you can indeed perform a smooth remote connection to your Ubuntu machine.  Start your favorite rdp client and simply enter your credentials and start testing your installation.  If everything is ok, you will see a similar Desktop interface where Gnome Desktop is used and the Dock is visible. 

Known issues and limitations 

Known Issues 

Drive Redirection  

To redirect your drives to your xrdp remote session, you have to ensure that the only option selected in the local resources tab are the Clipboard option and the drive option. Check the screenshot below and ensure that you have the same settings.

xrdp_redir_4

Click on picture for better resolution

xrdp_redir_5

Click on picture for better resolution

When these settings have been set accordingly, you should see the thin client folder populated with redirected drives coming from your Windows machine.

Sound redirection Issue

If the Sound redirection option has been selected, rebooting the Ubuntu system is not enough and the sound redirection might not work.  if the sound redirection option has been selected, you will have to shutdown your Ubuntu machine and then start it again in order to ensure that the sound redirection feature would work as expected. 

Sound redirection is not working as expected on Ubuntu 19.10 and later  The modules are created and copied to the correct location. However, the xrdp sink modules are never loaded in the remote session or randomly loaded in the remote session.   To fix this, you simply need to execute the following command within your remote session and the sound will start working 

pulseaudio -k

Black Screen or disconnected immediately after connecting

As mentioned and explained multiple times,  this situation will happen (or can happen) when the same user account is used concurrently locally and remotely.  In other words,  the problem is related to the fact that the same user account is already logged in locally and a remote connection is attempted at the same time.  With xRDP software solution, a specific user account can be logged on either locally or remotely but not both….  

You can read more information and some workarounds (provided as is) to try to fix the issues in the following posts 

Limitations

Switch between standard and Custom install

This version of the script can be run multiple times on the same machine.  However, because of this new behavior, you cannot perform a standard installation and then decide to perform the custom installation.  Once you have selected your installation mode, you will have to stick to it.  This is kind of checked by the script.  

So, if you have performed a standard installation but afterwards you might want to use the custom installation option, the script will ignore the request.  If you want to move from standard install mode to custom mode (or vice versa), the correct process is 

  • remove xrdp (using the xrdp-installer-1.3.sh -r option) 
  • perform a new installation using your selected installation mode (standard or custom install mode) 

Download the Script 

The script can be download at this location  :    xrdp-installer-1.3.zip 

Note : All the xrdp scripts can be found at https://www.c-nergy.be/products.html

Final Notes

This is it for this post !  

Please if you have time or want to help us in creating the best xrdp installer script, download the script, test it and provide constructive feedback.  If you find a bug or an issue with the script, let us know as well so we can try to fix it.  

Till next time

Annex – The Script Source code 

 

#!/bin/bash
#####################################################################################################
# Script_Name : xrdp-installer-1.3.sh
# Description : Perform xRDP installation on Ubuntu 18.04,20.4,20.10,21.04 and perform
#               additional post configuration to improve end user experience
# Date : October 2021
# written by : Griffon
# WebSite :http://www.c-nergy.be - http://www.c-nergy.be/blog
# Version : 1.3  
# History : 1.3   - Adding support for Ubuntu 21.10 (STR)
#                 - Code modification sound redirection using meson technology (ubuntu 21.10 only so far)
#                 - Adding code to copy config.h file which seems to be missing from xrdp-pulseaudio modules  
#                 - Adding support for Debian (10 and 11) (Best Effort)
#                 - Commenting out installation of Gnome Tweaks. Tweaks is not installed anymore by default
#                 - Added Rules to Detect Budgie-Desktop and postConfig settings
#                 - Added support for Pop!_0S (Best Effort !!!)
#                 - Code Changes to detect Desktop Interface in use  
#                 - Reworked code for xrdp removal option
#                 - Improved Std vs Custom installation detection process
#                 - Added support for Different Desktop Interfaces (More testing needed)
#                 - General Code structure re-worked to add more OS version and Desktop interface support  
#                 - Fixed Minor Bug(s)  
#         : 1.2.3 - Adding support for Ubuntu 21.04
#                 - Removing Support for Ubuntu 16.04.x (End Standard Support)
#                 - Delete xrdp and xorgxrdp folder when remove option selected
#                 - Review remove function to detect hwe package U18.04
#                 - Review, Optimize, Cleanup Code
#         : 1.2.2 - Changing Ubuntu repository from be.archive.ubuntu.com to archive.ubuntu.com
#                 - Bug Fixing - /etc/xrdp/xrdp-installer-check.log not deleted when remove option  
#                   selected in the script - Force Deletion (Thanks to Hiero for this input)    
#                 - Bug Fixing - Grab automatically xrdp/xorgxrdp package version to avoid    
#                   issues when upgrade operation performed (Thanks to Hiero for this input)    
#         : 1.2.1 - Adding Support to Ubuntu 20.10 + Removed support for Ubuntu 19.10
#           1.2   - Adding Support to Ubuntu 20.04 + Removed support for Ubuntu 19.04
#                 - Stricter Check for HWE Package (thanks to Andrej Gantvorg)
#                 - Changed code in checking where to copy image for login screen customization
#                 - Fixed Bug checking SSL group membership
#                 - Updating background color xrdp login screen
#                 - Updating pkgversion to x.13 for checkinstall process
#         : 1.1   - Tackling multiple run of the script
#                 - Improved checkinstall method/check ssl group memberhsip
#                 - Replaced ~/Downloads by a variable                
#         : 1.0   - Added remove option + Final optimization                
#         : 0.9   - updated compile section to use checkinstall
#         : 0.8   - Updated the fix_theme function to add support for Ubuntu 16.04
#         : 0.7   - Updated prereqs function to add support for Ubuntu 16.04
#         : 0.6   - Adding logic to detect Ubuntu version for U16.04
#         : 0.5   - Adding env variable Fix
#         : 0.4   - Adding SSL Fix
#         : 0.3   - Adding custom login screen option  
#         : 0.2   - Adding new code for passing parameters  
#         : 0.1   - Initial Script (merging custom & Std)      
# Disclaimer : Script provided AS IS. Use it at your own risk....
#              You can use this script and distribute it as long as credits are kept
#              in place and unchanged  
####################################################################################################

#---------------------------------------------------#
# Variables and Constants                           #
#---------------------------------------------------#

#--Automating Script versioning
ScriptVer="1.3"

#--Detecting  OS Version
version=$(lsb_release -sd)
codename=$(lsb_release -sc)
Release=$(lsb_release -sr)

#--Detecting variable related to Desktop interface and Directory path  
DesktopVer="$XDG_CURRENT_DESKTOP"
SessionVer="$GNOME_SHELL_SESSION_MODE"
ConfDir="$XDG_DATA_DIRS"

#Define Dwnload variable to point to ~/Downloads folder of user running the script
Dwnload=$(xdg-user-dir DOWNLOAD)
#Initialzing other variables
modetype="unknown"

#--------------------------------------------------------------------------#
# -----------------------Function Section - DO NOT MODIFY -----------------#
#--------------------------------------------------------------------------#
#---------------------------------------------------#
# Function 0  - check for supported OS version  ....#
#---------------------------------------------------#

check_os()
{
echo
/bin/echo -e "\e[1;33m   |-| Detecting OS version        \e[0m"
case $version in
  *"Ubuntu 18.04"*)
   /bin/echo -e "\e[1;32m       |-| OS Version : $version\e[0m"
   /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

   *"Ubuntu 20.04"*)
   /bin/echo -e "\e[1;32m       |-| OS Version : $version\e[0m"
   /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

   *"Ubuntu 21.04"*)
   /bin/echo -e "\e[1;32m       |-| OS Version : $version\e[0m"
   /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

   *"Ubuntu 21.10"*)
   /bin/echo -e "\e[1;32m       |-| OS Version : $version\e[0m"
   /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

   *"Pop!_OS 20.04"*)
   /bin/echo -e "\e[1;32m       |-| OS Version : $version\e[0m"
   /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

   *"Pop!_OS 21.04"*)
   /bin/echo -e "\e[1;32m       |-| OS Version : $version\e[0m"
   /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

  *"Debian"*)
   /bin/echo -e "\e[1;32m       |-| OS Version  : $version\e[0m"
   /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
   if [[ $Release="11" ]]
   then
    zenity --info --text="You are running Debian 11. Please note that standard mode will not allow you to perform remote connection against Gnome Desktop.  This is a known Debian/xrdp issue. Use custom install mode" --width=500
    exit
   fi
   ;;

  *)
    /bin/echo -e "\e[1;31m  !--------------------------------------------------------------!\e[0m"
    /bin/echo -e "\e[1;31m  ! Your system is not running a supported version !             !\e[0m"
    /bin/echo -e "\e[1;31m  ! The script has been tested only on the following versions    !\e[0m"
    /bin/echo -e "\e[1;31m  ! 18.04.x/20.04.x/21.04/21.10/Debian 10/Pop!_OS                !\e[0m"
    /bin/echo -e "\e[1;31m  ! The script is exiting...                                     !\e[0m"            
    /bin/echo -e "\e[1;31m  !--------------------------------------------------------------!\e[0m"
    echo
    exit
    ;;
esac
echo
}

#---------------------------------------------------#
# Function 1  - check xserver-xorg-core package....
#---------------------------------------------------#

check_hwe()
{
Release=$(lsb_release -sr)
echo
/bin/echo -e "\e[1;33m |-| Detecting xserver-xorg-core package installed \e[0m"
xorg_no_hwe_install_status=$(dpkg-query -W -f ='${Status}\n' xserver-xorg-core 2>/dev/null)
xorg_hwe_install_status=$(dpkg-query -W -f ='${Status}\n' xserver-xorg-core-hwe-$Release2>/dev/null)

if [[ "$xorg_hwe_install_status" =~ \ installed$ ]]
then
# – hwe version is installed on the system
/bin/echo -e "\e[1;32m  |-| xorg package version: xserver-xorg-core-hwe \e[0m"
HWE="yes"
elif [[ "$xorg_no_hwe_install_status" =~ \ installed$ ]]
then
/bin/echo -e "\e[1;32m  |-| xorg package version: xserver-xorg-core \e[0m"
HWE="no"
else
/bin/echo -e "\e[1;31m  |-| Error checking xserver-xorg-core flavour \e[0m"
exit 1
fi
}

#---------------------------------------------------#
# Function 2  - Version specific actions needed....
#---------------------------------------------------#

PrepOS()
{
echo
/bin/echo -e "\e[1;33m   |-| Custom actions based on OS Version....       \e[0m"

if [[ *"$version"* = *"Debian"*  ]]
then
 #Disable cdrom source.list to avoid prompt during install # DEBIAN SPECIFIC
 sudo sed -i 's/deb cdrom:/#deb cdrom:/' /etc/apt/sources.list
 sudo apt-get update
fi

##--Need to use Variables !!!!!
if [[ *"$version"* = *"Debian"*  ]]
then
    CustomPix="griffon_logo_xrdpd.bmp"
    CustomColor="27354D"
else
    CustomPix="griffon_logo_xrdp.bmp"
    CustomColor="4F194C"
fi
}

############################################################################
# INSTALLATION MODE : STANDARD
############################################################################

#---------------------------------------------------#
# Function 3  - Install xRDP Software....
#---------------------------------------------------#

install_xrdp()
{
echo
/bin/echo -e "\e[1;33m   |-| Installing xRDP packages       \e[0m"
echo
if [[ $HWE = "yes" ]] && [[ "$version" = *"Ubuntu 18.04"* ]];
then
    sudo apt-get install xrdp -y
    sudo apt-get install xorgxrdp-hwe-18.04
else
    sudo apt-get install xrdp -y
fi
}

############################################################################
# ADVANCED INSTALLATION MODE : CUSTOM INSTALLATION
############################################################################
#---------------------------------------------------#
# Function 4 - Install Prereqs...
#---------------------------------------------------#

install_prereqs() {
echo
/bin/echo -e "\e[1;33m   |-|Installing prerequisites packages       \e[0m"
echo
#Debian Specific - add in source backport package to download necessary packages - Debian Specific
if [[ *"$version"* = *"Debian"*  ]]
then
sudo apt-get install -y software-properties-common
sudo apt-add-repository -s -y 'deb http://deb.debian.org/debian '$codename'-backports main'
#sudo sed -i 's/deb cdrom:/#deb cdrom:/' /etc/apt/sources.list
sudo apt-get update
#--Needed to be created manually or compilation fails
sudo mkdir /usr/local/lib/xrdp/
fi
#--End Debian Specific --#

#Install packages
sudo apt-get -y install git
sudo apt-get -y install libx11-dev libxfixes-dev libssl-dev libpam0g-dev libtool libjpeg-dev flex bison gettext autoconf libxml-parser-perl libfuse-dev xsltproc libxrandr-dev python3-libxml2 nasm fuse pkg-config git intltool checkinstall
echo
#-- check if no error during Installation of missing packages
if [ $? -eq 0 ]
then
/bin/echo -e "\e[1;33m   |-| Preprequesites installation successfully       \e[0m"
else
echo
echo
/bin/echo -e "\e[1;31m   !---------------------------------------------!\e[0m"
/bin/echo -e "\e[1;31m   !   Error while installing prereqs            !\e[0m"
/bin/echo -e "\e[1;31m   !   The Script is exiting....                 !\e[0m"
/bin/echo -e "\e[1;31m   !---------------------------------------------!\e[0m"
exit
fi

#-- check if hwe stack needed or not
if [ $HWE = "yes" ];
then
    # - xorg-hwe-* to be installed
    /bin/echo -e "\e[1;32m       |-| xorg package version: xserver-xorg-core-hwe-$Release \e[0m"
    sudo apt-get install -y xserver-xorg-dev-hwe-$Release xserver-xorg-core-hwe-$Release    
else
    #-no-hwe
    /bin/echo -e "\e[1;32m       |-| xorg package version: xserver-xorg-core \e[0m"
    echo
    sudo apt-get install -y xserver-xorg-dev xserver-xorg-core
fi
}

#---------------------------------------------------#
# Function 5 - Download XRDP Binaries...
#---------------------------------------------------#

get_binaries() {
echo
/bin/echo -e "\e[1;33m   |-| Downloading xRDP Binaries...Proceeding     \e[0m"
echo
#cd ~/Downloads
Dwnload=$(xdg-user-dir DOWNLOAD)
cd $Dwnload
#Check if xrdp folder already exists.  if yes; delete it and download again in order to get latest version
if [ -d "$Dwnload/xrdp" ]
then
    sudo rm -rf xrdp
fi

#Check if xorgxrdp folder already exists.  if yes; delete it and download again in order to get latest version
if [ -d "$Dwnload/xorgxrdp" ]
then
    sudo rm -rf xorgxrdp
fi

## -- Download the xrdp latest files
echo
/bin/echo -e "\e[1;32m       |-|  Downloading xRDP Binaries.....     \e[0m"
echo
git clone https://github.com/neutrinolabs/xrdp.git
echo
/bin/echo -e "\e[1;32m       |-|  Downloading xorgxrdp Binaries...     \e[0m"
echo
git clone https://github.com/neutrinolabs/xorgxrdp.git
}

#---------------------------------------------------#
# Function 6 - compiling xrdp...
#---------------------------------------------------#

compile_source() {
echo
/bin/echo -e "\e[1;33m   |-| Compiling xRDP Binaries...Proceeding     \e[0m"
echo
#cd ~/Downloads/xrdp
cd $Dwnload/xrdp

#Get the release version automatically
pkgver=$(git describe  --abbrev=0 --tags  | cut -dv -f2)
sudo ./bootstrap
sudo ./configure --enable-fuse --enable-jpeg --enable-rfxcodec
sudo make

#-- check if no error during compilation
if [ $? -eq 0 ]
then
echo
/bin/echo -e "\e[1;32m       |-|  Make Operation Completed successfully....xRDP     \e[0m"
echo
else
echo
/bin/echo -e "\e[1;31m   !---------------------------------------------!\e[0m"
/bin/echo -e "\e[1;31m   !   Error while Executing make                !\e[0m"
/bin/echo -e "\e[1;31m   !   The Script is exiting....                 !\e[0m"
/bin/echo -e "\e[1;31m   !---------------------------------------------!\e[0m"
exit
fi

sudo checkinstall --pkgname=xrdp --pkgversion=$pkgver --pkgrelease=1 --default

#xorgxrdp package compilation
echo
/bin/echo -e "\e[1;32m       |-|  Make Operation Completed successfully....xorgxrdp     \e[0m"
echo
#cd ~/Downloads/xorgxrdp
cd $Dwnload/xorgxrdp
#Get the release version automatically
pkgver=$(git describe  --abbrev=0 --tags  | cut -dv -f2)
sudo ./bootstrap
sudo ./configure
sudo make

# check if no error during compilation
if [ $? -eq 0 ]
then
echo
/bin/echo -e "\e[1;33m   |-| Compilation Completed successfully...Proceeding    \e[0m"
echo
else
echo
/bin/echo -e "\e[1;31m   !---------------------------------------------!\e[0m"
/bin/echo -e "\e[1;31m   !   Error while Executing make                !\e[0m"
/bin/echo -e "\e[1;31m   !   The Script is exiting....                 !\e[0m"
/bin/echo -e "\e[1;31m   !---------------------------------------------!\e[0m"
exit
fi
sudo checkinstall --pkgname=xorgxrdp --pkgversion=1:$pkgver --pkgrelease=1 --default
}

#---------------------------------------------------#
# Function 7 - create services ....
#---------------------------------------------------#

enable_service() {
echo
/bin/echo -e "\e[1;33m   |-| Creating and configuring xRDP services    \e[0m"
echo
sudo systemctl daemon-reload
sudo systemctl enable xrdp.service
sudo systemctl enable xrdp-sesman.service
sudo systemctl start xrdp
}

############################################################################
# COMMON FUNCTIONS - WHATEVER INSTALLATION MODE - Version Specific !!!
############################################################################

#--------------------------------------------------------------------------#
# Function 8 - Install Tweaks Utilty if Gnome desktop used (Optional) .... #
#--------------------------------------------------------------------------#

install_tweak()
{
echo
/bin/echo -e "\e[1;33m   |-| Checking if Tweaks needs to be installed....    \e[0m"
if [[ "$DesktopVer" != *"GNOME"* ]]
then
/bin/echo -e "\e[1;32m       |-|  Gnome Tweaks not needed...Proceeding...     \e[0m"
echo
else
/bin/echo -e "\e[1;32m       |-|  Installing Gnome Tweaks Utility...Proceeding... \e[0m"
echo
    if [[ *"$version"* = *"Ubuntu 21.10"*  ]]
    then
    #if Ubuntu 21.10 - new version of th tool.....
        sudo apt-get install gnome-tweaks -y
        sudo apt-get install gnome-shell-extensions -y  
    else
        sudo apt-get install gnome-tweak-tool -y
    fi
fi
}

#--------------------------------------------------------------------#
# Fucntion 9 - Allow console Access ....(seems optional in u18.04)
#--------------------------------------------------------------------#

allow_console()

{
echo
/bin/echo -e "\e[1;33m   |-| Configuring Allow Console Access...    \e[0m"
echo
# Checking if Xwrapper file exists
if [ -f /etc/X11/Xwrapper.config ]
then
sudo sed -i 's/allowed_users=console/allowed_users=anybody/' /etc/X11/Xwrapper.config
else
    sudo bash -c "cat >/etc/X11/Xwrapper.config" <<EOF
    allowed_users=anybody
EOF
fi
}

#---------------------------------------------------#
# Function 10 - create policies exceptions ....
#---------------------------------------------------#

create_polkit()
{
echo
/bin/echo -e "\e[1;33m   |-| Creating Polkit exceptions rules...    \e[0m"
echo
#All Ubuntu versions,Debian Version, Pop OS version
sudo bash -c "cat >/etc/polkit-1/localauthority/50-local.d/45-allow.colord.pkla" <<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=no
ResultInactive=no
ResultActive=yes
EOF

#Not to apply to Ubuntu 18.04 version but to others....This caused an issue on Ubuntu 18.04

if [[  "$version" !=  *"Ubuntu 18.04"* ]];
then
sudo bash -c "cat >/etc/polkit-1/localauthority/50-local.d/46-allow-update-repo.pkla" <<EOF
[Allow Package Management all Users]
Identity=unix-user:*
Action=org.freedesktop.packagekit.system-sources-refresh;org.freedesktop.packagekit.system-network-proxy-configure
ResultAny=yes
ResultInactive=yes
ResultActive=yes
EOF
fi

#-- KDE Desktop Specific  - can be detected only at run time of the script
if [ "$DesktopVer" = "KDE" ];
then
sudo bash -c "cat >/etc/polkit-1/localauthority/50-local.d/47-allow-networkd.pkla" <<EOF
[Allow Network Control all Users]
Identity=unix-user:*
Action=org.freedesktop.NetworkManager.network-control
ResultAny=no
ResultInactive=no
ResultActive=yes
EOF
fi
}

#---------------------------------------------------#
# Function 11 - Fixing Theme and Extensions ....
#---------------------------------------------------#

fix_theme()
{
echo
/bin/echo -e "\e[1;33m   |-| Fixing Themes and Extensions....    \e[0m"
echo
# Checking if script has run already
if [ -f /etc/xrdp/startwm.sh.griffon ]
then
sudo rm /etc/xrdp/startwm.sh
sudo mv /etc/xrdp/startwm.sh.griffon /etc/xrdp/startwm.sh
fi
#Backup the file before modifying it
sudo cp /etc/xrdp/startwm.sh /etc/xrdp/startwm.sh.griffon
echo

# Custom code for Budgie Desktop
if [[ "$DesktopVer" == *"Budgie"* ]]
then
sudo sed -i "4 a #Improved Look n Feel Method\ncat <<EOF > ~/.xsessionrc\nbudgie-desktop\nexport GNOME_SHELL_SESSION_MODE=$SessionVer\nexport XDG_CURRENT_DESKTOP=$DesktopVer\nexport XDG_DATA_DIRS=$ConfDir\nEOF\n" /etc/xrdp/startwm.sh
else
sudo sed -i "4 a #Improved Look n Feel Method\ncat <<EOF > ~/.xsessionrc\nexport GNOME_SHELL_SESSION_MODE=$SessionVer\nexport XDG_CURRENT_DESKTOP=$DesktopVer\nexport XDG_DATA_DIRS=$ConfDir\nEOF\n" /etc/xrdp/startwm.sh
fi
echo
}

#---------------------------------------------------#
# Function 12 - Enable Sound Redirection ....
#---------------------------------------------------#

enable_sound()
{
echo
/bin/echo -e "\e[1;33m   |-| Enabling Sound Redirection....    \e[0m"
echo
pulsever=$(pulseaudio --version | awk '{print $2}')
/bin/echo -e "\e[1;32m       |-| Install additional packages..     \e[0m"
# Version Specific - adding source and correct pulseaudio version for Debian !!!  
if [[ *"$version"* = *"Debian"*  ]]
then
# Step 0 - Install Some PreReqs
sudo apt-get install libconfig-dev -y
sudo apt-get install git libpulse-dev autoconf m4 intltool build-essential dpkg-dev libtool libsndfile-dev libcap-dev -y libjson-c-dev
sudo apt build-dep pulseaudio -y
/bin/echo -e "\e[1;32m       |-| Download pulseaudio sources files..     \e[0m"

# Step 3 -  Download pulseaudio source in /tmp directory - Debian source repo should be already enabled
cd /tmp
sudo apt source pulseaudio=$pulsever
else
# Step 1 - Enable Source Code Repository
sudo apt-add-repository -s -y 'deb http://archive.ubuntu.com/ubuntu/ '$codename' main restricted'
sudo apt-add-repository -s -y 'deb http://archive.ubuntu.com/ubuntu/ '$codename' restricted universe main multiverse'
sudo apt-add-repository -s -y 'deb http://archive.ubuntu.com/ubuntu/ '$codename'-updates restricted universe main multiverse'
sudo apt-add-repository -s -y 'deb http://archive.ubuntu.com/ubuntu/ '$codename'-backports main restricted universe multiverse'
sudo apt-add-repository -s -y 'deb http://archive.ubuntu.com/ubuntu/ '$codename'-security main restricted universe main multiverse'
sudo apt-get update
# Step 2 - Install Some PreReqs
sudo apt-get install git libpulse-dev autoconf m4 intltool build-essential dpkg-dev libtool libsndfile-dev libcap-dev -y libjson-c-dev
sudo apt build-dep pulseaudio -y
/bin/echo -e "\e[1;32m       |-| Download pulseaudio sources files..     \e[0m"
# Step 3 -  Download pulseaudio source in /tmp directory - Debian source repo should be already enabled
cd /tmp
sudo apt source pulseaudio
fi
/bin/echo -e "\e[1;32m       |-| compile pulseaudio sources files..     \e[0m"

# Step 4 - Compile based on OS version
cd /tmp/pulseaudio-$pulsever*
PulsePath=$(pwd)
if [[ *"$version"* = *"Ubuntu 21.10"*  ]]
then
    #Build - new way using meson and replacing configure old school approach
    sudo meson --prefix=$PulsePath build
    sudo ninja -C build install
else
    sudo ./configure
fi

# step 5 - Download xrdp sound modules
/bin/echo -e "\e[1;32m       |-| Compiling and building xRDP Sound modules...     \e[0m"
sudo git clone https://github.com/neutrinolabs/pulseaudio-module-xrdp.git
cd pulseaudio-module-xrdp

# step 6 - Check if config.h file is missing...if missing..copy file from somewhere
if [ -f $PulsePath/pulseaudio-module-xrdp/config.h ]
then
    /bin/echo -e "\e[1;32m       |-| Config.h present..Proceeding...     \e[0m"
else
    /bin/echo -e "\e[1;32m       |-| Config.h seems missing...Dirty fix applied..     \e[0m"
    sudo cp $PulsePath/build/config.h $PulsePath/pulseaudio-module-xrdp  
fi

# Step 7 - Create xrdp sound modules
    sudo ./bootstrap
    sudo ./configure PULSE_DIR=$PulsePath
    sudo make
    #this will install modules in /usr/lib/pulse* directory
    sudo make install

# Step 8 copy files to correct location (as defined in /etc/xrdp/pulse/default.pa)
if [[ *"$version"* = *"Ubuntu 21.10"*  ]]
then
    /bin/echo -e "\e[1;32m       |-| copy generated files in target folder....     \e[0m"
    cd /usr/lib/pulse*/modules   #new location for the module
    if [ -d /var/lib/xrdp-pulseaudio-installer ]
    then
         sudo install -t "/var/lib/xrdp-pulseaudio-installer" -D -m 644 *.so
    else
        sudo mkdir /var/lib/xrdp-pulseaudio-installer  # this is not created automatically
        sudo install -t "/var/lib/xrdp-pulseaudio-installer" -D -m 644 *.so
        echo
    fi  
else
    /bin/echo -e "\e[1;32m       |-| copy generated files in target folder....     \e[0m"
    cd /tmp/pulseaudio-$pulsever/pulseaudio-module-xrdp/src/.libs
    sudo install -t "/var/lib/xrdp-pulseaudio-installer" -D -m 644 *.so
    echo
fi
}

#---------------------------------------------------#
# Function 13 - Custom xRDP Login Screen ....
#---------------------------------------------------#

custom_login()
{
echo
/bin/echo -e "\e[1;33m   |-| Customizing xRDP login screen       \e[0m"
Dwnload=$(xdg-user-dir DOWNLOAD)
cd $Dwnload

#Check if script has run once...

if [ -f /etc/xrdp/xrdp.ini.griffon ]
then
sudo rm /etc/xrdp/xrdp.ini
sudo mv /etc/xrdp/xrdp.ini.griffon /etc/xrdp/xrdp.ini
fi

#Backup file
sudo cp /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.griffon
#chek if file exists if not - download it....
if [ -f "$CustomPix" ]
then
    /bin/echo -e "\e[1;32m       |-| necessary file already available...skipping   \e[0m"
else
    /bin/echo -e "\e[1;32m       |-| Downloading additional file...: logo_xrdp image   \e[0m"
    wget http://www.c-nergy.be/downloads/"$CustomPix"
fi

#Check where to copy the logo file
if [ -d "/usr/local/share/xrdp" ]
then
    echo
    sudo cp $CustomPix /usr/local/share/xrdp
    sudo sed -i "s/ls_logo_filename=/ls_logo_filename=\/usr\/local\/share\/xrdp\/$CustomPix/g" /etc/xrdp/xrdp.ini
else
    sudo cp $CustomPix /usr/share/xrdp
    sudo sed -i "s/ls_logo_filename=/ls_logo_filename=\/usr\/share\/xrdp\/$CustomPix/g" /etc/xrdp/xrdp.ini
fi

sudo sed -i 's/blue=009cb5/blue=dedede/' /etc/xrdp/xrdp.ini
sudo sed -i 's/#white=ffffff/white=dedede/' /etc/xrdp/xrdp.ini
sudo sed -i 's/#ls_title=My Login Title/ls_title=Remote Desktop for Linux/' /etc/xrdp/xrdp.ini
sudo sed -i "s/ls_top_window_bg_color=009cb5/ls_top_window_bg_color=$CustomColor/" /etc/xrdp/xrdp.ini
sudo sed -i 's/ls_bg_color=dedede/ls_bg_color=ffffff/' /etc/xrdp/xrdp.ini
sudo sed -i 's/ls_logo_x_pos=55/ls_logo_x_pos=0/' /etc/xrdp/xrdp.ini
sudo sed -i 's/ls_logo_y_pos=50/ls_logo_y_pos=5/' /etc/xrdp/xrdp.ini
}

#---------------------------------------------------#
# Function 14 - Fix SSL Minor Issue ....
#---------------------------------------------------#

fix_ssl()
{
echo
/bin/echo -e "\e[1;33m   |-| Fixing SSL Permissions settings...       \e[0m"
echo
if id -Gn xrdp | grep ssl-cert
then
/bin/echo -e "\e[1;32m   !--xrdp already member ssl-cert...Skipping ---!\e[0m"
else
    sudo adduser xrdp ssl-cert
fi
}

#---------------------------------------------------#
# Function 15 - Fixing env variables in XRDP ....
#---------------------------------------------------#

fix_env()
{
echo
/bin/echo -e "\e[1;33m   |-| Fixing xRDP env Variables...       \e[0m"
echo
#Add this line to /etc/pam.d/xrdp-sesman if not present
if grep -Fxq "session required pam_env.so readenv=1 user_readenv=0" /etc/pam.d/xrdp-sesman
   then
            echo "Env settings already set"
   else
        sudo sed -i '1 a session required pam_env.so readenv=1 user_readenv=0' /etc/pam.d/xrdp-sesman
 fi
}

#---------------------------------------------------#
# Function 17 - Removing XRDP Packages ....
#---------------------------------------------------#

remove_xrdp()
{
echo
/bin/echo -e "\e[1;33m   !---------------------------------------------!\e[0m"
/bin/echo -e "\e[1;33m   ! Removing xRDP Packages...                   !\e[0m"
/bin/echo -e "\e[1;33m   !---------------------------------------------!\e[0m"
echo

#remove the xrdplog file created by the script
sudo rm /etc/xrdp/xrdp-installer-check.log

#----remove xrdp package
sudo systemctl stop xrdp
sudo systemctl disable xrdp
sudo apt-get autoremove xrdp -y
sudo apt-get purge xrdp -y

#---remove xorgxrdp
sudo systemctl stop xorgxrdp
sudo systemctl disable xorgxrdp

if [[ $HWE = "yes" ]] && [[ "$version" = *"Ubuntu 18.04"* ]];
then
    sudo apt-get autoremove xorgxrdp-hwe-18.04 -y
    sudo apt-get purge xorgxrdp-hwe-18.04 -y
else
    sudo apt-get autoremove xorgxrdp -y
    sudo apt-get purge xorgxrdp -y
fi

#---Cleanup files
#Remove xrdp folder
if [ -d "$Dwnload/xrdp" ]
then
    sudo rm -rf xrdp
fi

#Remove xorgxrdp folder
if [ -d "$Dwnload/xorgxrdp" ]
then
    sudo rm -rf xorgxrdp
fi

#Remove custom xrdp logo file
if [ -f "$Dwnload/$CustomPix" ]
then
    sudo rm -f  "$Dwnload/$CustomPix"
fi
sudo systemctl daemon-reload
}

sh_credits()
{
echo
/bin/echo -e "\e[1;36m   !----------------------------------------------------------------!\e[0m"
/bin/echo -e "\e[1;36m   ! Installation Completed...Please test your xRDP configuration   !\e[0m"
/bin/echo -e "\e[1;36m   ! If Sound option selected, shutdown your machine completely     !\e[0m"
/bin/echo -e "\e[1;36m   ! start it again to have sound working as expected               !\e[0m"
/bin/echo -e "\e[1;36m   !                                                                !\e[0m"
/bin/echo -e "\e[1;36m   ! Credits : Written by Griffon - October 2021                    !\e[0m"
/bin/echo -e "\e[1;36m   !           www.c-nergy.be -xrdp-installer-v$ScriptVer.sh               !\e[0m"
/bin/echo -e "\e[1;36m   !           ver $ScriptVer                                              !\e[0m"
/bin/echo -e "\e[1;36m   !----------------------------------------------------------------!\e[0m"
echo
}

#---------------------------------------------------#
# SECTION FOR OPTIMIZING CODE USAGE...              #
#---------------------------------------------------#

install_common()
{
#install_tweak  
allow_console
create_polkit
fix_theme
fix_ssl
fix_env
}

install_custom()
{
install_prereqs
get_binaries
compile_source
enable_service
}

#--------------------------------------------------------------------------#
# -----------------------END Function Section             -----------------#
#--------------------------------------------------------------------------#
#--------------------------------------------------------------------------#
#------------                 MAIN SCRIPT SECTION       -------------------#
#--------------------------------------------------------------------------#
#---------------------------------------------------#
# Script Version information Displayed              #
#---------------------------------------------------#

echo
/bin/echo -e "\e[1;36m   !-----------------------------------------------------------------!\e[0m"
/bin/echo -e "\e[1;36m   !   xrdp-installer-$ScriptVer Script                                     !\e[0m"
/bin/echo -e "\e[1;36m   !   Support Ubuntu and Debian Distribution                        !\e[0m"
/bin/echo -e "\e[1;36m   !   Written by Griffon - October 2021 - www.c-nergy.be            !\e[0m"
/bin/echo -e "\e[1;36m   !                                                                 !\e[0m"
/bin/echo -e "\e[1;36m   !   For Help and Syntax, type ./xrdp-installer-$ScriptVer.sh -h          !\e[0m"
/bin/echo -e "\e[1;36m   !                                                                 !\e[0m"
/bin/echo -e "\e[1;36m   !-----------------------------------------------------------------!\e[0m"
echo

#----------------------------------------------------------#
# Step 0 -Detecting if Parameters passed to script ....    #
#----------------------------------------------------------#

for arg in "$@"
do
    #Help Menu Requested
    if [ "$arg" == "--help" ] || [ "$arg" == "-h" ]
    then
                echo "Usage Syntax and Examples"
                echo
                echo " --custom or -c           custom xRDP install (compilation from sources)"
                echo " --loginscreen or -l      customize xRDP login screen"
                echo " --remove or -r           removing xRDP packages"
                echo " --sound or -s            enable sound redirection in xRDP"
                echo
                echo "example                                                      "
                echo    
                echo " ./xrdp-installer-$ScriptVer.sh -c -s  custom install with sound redirection"
                echo " ./xrdp-installer-$ScriptVer.sh -l     standard install with custom login screen"
                echo " ./xrdp-installer-$ScriptVer.sh        standard install no additional features"
                echo
                exit
    fi

    if [ "$arg" == "--sound" ] || [ "$arg" == "-s" ]
    then
        fixSound="yes"              
    fi

    if [ "$arg" == "--loginscreen" ] || [ "$arg" == "-l" ]
    then
        fixlogin="yes"
    fi

    if [ "$arg" == "--custom" ] || [ "$arg" == "-c" ]
    then
        adv="yes"  
    fi

    if [ "$arg" == "--remove" ] || [ "$arg" == "-r" ]
    then
        removal="yes"      
    fi
done

#--------------------------------------------------------------------------------#
#-- Step 0 - Check that the script is run as normal user and not as root....
#-------------------------------------------------------------------------------#

if [[ $EUID -ne 0 ]]; then
    /bin/echo -e "\e[1;36m   !-------------------------------------------------------------!\e[0m"
    /bin/echo -e "\e[1;36m   !  Standard user detected....Proceeding....                   !\e[0m"
    /bin/echo -e "\e[1;36m   !-------------------------------------------------------------!\e[0m"
else
    echo
    /bin/echo -e "\e[1;31m   !-------------------------------------------------------------!\e[0m"
    /bin/echo -e "\e[1;31m   !  Script launched with sudo command. Script will not run...  !\e[0m"
    /bin/echo -e "\e[1;31m   !  Run script a standard user account (no sudo). When needed  !\e[0m"
    /bin/echo -e "\e[1;31m   !  script will be prompted for password during execution      !\e[0m"
    /bin/echo -e "\e[1;31m   !                                                             !\e[0m"
    /bin/echo -e "\e[1;31m   !  Exiting Script - No Install Performed !!!                  !\e[0m"
    /bin/echo -e "\e[1;31m   !-------------------------------------------------------------!\e[0m"
    echo
    #sh_credits
    exit
fi

#---------------------------------------------------#
#-- Step 1 - Try to Detect Ubuntu Version....       #
#---------------------------------------------------#

check_os

#--------------------------------------------------------#
#-- Step 2 - Try to detect if HWE Stack needed or not....#
#--------------------------------------------------------#

check_hwe

#--------------------------------------------------------------------------------#
#-- Step 3 - Check if Removal Option Selected
#--------------------------------------------------------------------------------#

if [ "$removal" = "yes" ];
then
    remove_xrdp
    echo
    sh_credits
    exit
fi

#---------------------------------------------------------#
# Step 4 - Executing the installation & config tasks .... #
#---------------------------------------------------------#

#-- Detect if multiple runs and install mode used.....
echo
/bin/echo -e "\e[1;33m   |-| Checking if script has run at least once...        \e[0m"
if [ -f /etc/xrdp/xrdp-installer-check.log ]
then
    modetype=$(sed -n 1p /etc/xrdp/xrdp-installer-check.log)
    /bin/echo -e "\e[1;32m       |-| Script has already run. Detected mode...: $modetype\e[0m"
else
    /bin/echo -e "\e[1;32m       |-| First run or xrdp-installer-check.log deleted. Detected mode : $modetype        \e[0m"
fi

#---------------------------------------------------------------------------------------
#- Detect Standard vs custom install mode and additional options...
#----------------------------------------------------------------------------------------

    if [ "$adv" = "yes" ];
    then
        echo
        /bin/echo -e "\e[1;33m   |-| custom installation mode detected.        \e[0m"
       
        if [ $modetype = "custom" ];
        then
            /bin/echo -e "\e[1;36m           |-| xrdp already installed - custom mode....skipping xrdp install        \e[0m"
            PrepOS
        else
            /bin/echo -e "\e[1;36m           |-| Proceed custom xrdp installation packages and customization tasks      \e[0m"
            PrepOS
            install_custom
            install_common
    
            #create the file used a detection method
            sudo touch /etc/xrdp/xrdp-installer-check.log
            sudo bash -c 'echo "custom" >/etc/xrdp/xrdp-installer-check.log'
        fi      
    else
        echo
            /bin/echo -e "\e[1;33m   |-| Additional checks Std vs Custom Mode..       \e[0m"

        if [ $modetype = "standard" ];
        then
            /bin/echo -e "\e[1;35m           |-| xrdp already installed - standard mode....skipping install  \e[0m"
            PrepOS
        elif [ $modetype = "custom" ]
        then
            /bin/echo -e "\e[1;35m           |-| Checking for additional parameters"
        else
            /bin/echo -e "\e[1;32m       |-| Proceed standard xrdp installation packages and customization tasks      \e[0m"
            PrepOS
            install_xrdp
            install_common
       
            #create the file
            sudo touch /etc/xrdp/xrdp-installer-check.log
            sudo bash -c 'echo "standard" >/etc/xrdp/xrdp-installer-check.log'
        fi
    fi  #end if Adv option

#---------------------------------------------------------------------------------------
#- Check for Additional Options selected
#----------------------------------------------------------------------------------------

if [ "$fixSound" = "yes" ];
then
        enable_sound      
fi

if [ "$fixlogin" = "yes" ];
then
    echo
    custom_login
fi

#---------------------------------------------------------------------------------------
#- show Credits and finishing script
#---------------------------------------------------------------------------------------

sh_credits
 

47 thoughts on “xRDP – Easy install xRDP on Ubuntu 18.04,20.04,21.04,21.10 (Script Version 1.3)

  1. @sdking,

    Thank you for visiting our blog and providing feedback… We can have a look into your request…No guarantee it will be added… We might consider to add it in the next “major” release of the script (probably released at the same time as Ubuntu 22.04). This will be based on best effort….We will let you know..

    Till next time
    See ya

  2. Thank you for continuing to provide this very useful script. I know you’ve done some work on pulseaudio, but I’m still needing to run ‘pulseaudio –start’ to get sound working whenever I log in. Is this (still) normal?

  3. @Jason,

    Thank you for visiting our blog and providing feedback. yes, at the moment, the fact that pulseaudio is not starting automatically is “normal”. We have been working on a possible fix and we think we have found something that could work and would be acceptable for most of the scenarios. A quick and dirty fix will be made available in the next minor version of the script…. work is still in progress….When we are happy with the solution, it will be made available to public and we will wait for some feedback

    Hope this help
    Stay Tuned
    Till next time
    See ya

  4. Thank you for all your work on getting Linux / XRDP and Active Directory working. Your scripts have been invaluable in getting a server up and running over night.

  5. @PCW,

    Thank you for visiting our blog and providing feedback. We are super happy to see that our work is useful. Really cool to see that this was helpful to you 🙂
    Till next time
    See ya

  6. Hi – thanks for this. I’m struggling with getting a from my Mac to the Ubuntu server. I get an authentication window – if i authenticate correctly no window. If i authenticate incorrectly, I get a connection log window and then authentication request to an xrdp window, which disapears if i authenticate correctly.

    There’s no errors in the xrdp.log, but i do get a warning: [WARN ] local keymap file for 0x00000809 found and doesn’t match built in keymap, using local keymap file

    I hope someone might be able to point me in the right direction

  7. @John,
    Thank you for visiting our blog and sharing your experience..
    You have installed Ubuntu Server => can you confirm that you have installed a Desktop interface on top of it ? by default UBuntu server does not ship with any GUI desktop interface
    Then, can you ensure that no one is logged on the Ubuntu Server. Black screen or disconnection can be due to the fact that the same user is connected locally and remotely on the same Ubuntu machine
    Finally, we know that there used to be some issues with Mac and xRDP…but need to check if still there

    Hope this help
    Till next time
    See ya

  8. Hey! Thanks for the script!
    Could you please explain how to set up multiple sessions for a user now?
    The file (“/etc/xrdp/startwm.sh”) now contains no line (” test -x /etc/X11/Xsession && exec /etc/X11/Xsession”): (((

  9. @Pashka;

    thank you for visiting our blog and sharing your experience… Not sure we understand the question here…
    If you want to have UserA logged on locally and logged via xRDP at the same time, please read the following post https://c-nergy.be/blog/?p=16698. UserA will be able to access local session and remote session…

    If you have mutliple users that needs to access the ubuntu remotely; by default, xRDP will allow multiple users to connect to the system via remote desktop

    Hope this help
    Till next time
    See ya

  10. Thank you for your hard work on this challenging task!

    I am feeling regret for my choice of using LinuxMint!

    Do you have any tips or guidance on how to port this over to LinuxMint?

  11. @John,

    Thank you for visiting our blog and providing feedback. As mentioned, the script has been designed specifically for Ubuntu but we are slowly adding more distributions as possible candidates… We could try to add cinnamon in the next iteration. You can try to update the /etc/xrdp/startwm.sh file so the cinnamon session is loaded accordingly

    Till next time
    See ya

  12. Thanks so much for all your work on this!! I would second a vote to add Linux Mint to the next iteration.

    Thanks!

    James

  13. @James Miller;

    Thank you for visiting our blog and providing feedback. We will see if we could add Linux Mint in the next iteration but this would be best effort as there are too many desktops to be fully tested and at the moment we have really little resource to perform that. We will give a try and see how far we can go

    Till next time
    See ya

  14. It cost me quite lot of time to debug so I post here in case others has same problem:

    The script is problematic if you run the script in SSH.
    E.g
    DesktopVer=”$XDG_CURRENT_DESKTOP”
    SessionVer=”$GNOME_SHELL_SESSION_MODE”
    ConfDir=”$XDG_DATA_DIRS”

    Both `DesktopVer` and `SessionVer` will be blank in SSH session, which result an incorrect `.xsessionrc` file get generated when `startwm.sh` is executed :
    export GNOME_SHELL_SESSION_MODE=
    export XDG_CURRENT_DESKTOP=

    You will be unable to login either via xrdp or normal login -> you are stuck. Even after remove xrdp.

    Solution:
    – Remove ~/.xsessionrc, then you will able to login to you PC, run the install script within your login session.

    @Griffon: Possible fix/update:
    – Skip fix_theme() if the script could not get GNOME_SHELL_SESSION_MODE and XDG_CURRENT_DESKTOP
    – Remove ~/.xsessionrc after use, if possible.

    Thanks for the script, anyway. It would also be better to put it to github.

  15. @Peter,

    Thank you for visiting our blog and providing feedback. As background info; version 1.3 is the first version of the script trying to detect the Desktop Interface in used….as we are trying to make the script useable on more Distributions. Indeed, we overlooked the possibility to run the script through an SSH session…So; thank you for bringing this point to our attention. In the next release of the script, we will try to tackle the issue you just discovered and see if we can indeed improve the way the script run

    Thank you for the feedback
    Stay tuned
    Till next time
    See ya

  16. Hi.
    Installed this script succesfully, i can connect with remote desktop but when I entered my credentials, the session is gone. (Remote Desktop is closing) So I see an login window and when I inter my credentials and continue, the Remote Desktop is crashing or something..?

  17. @GiannoezNL,

    Thank you for visiting our blog and sharing your experience. IF you are using Ubuntu + Gnome Desktop, we would assume that you are logged on locally on your system and you are trying to perform a remote connection using exactly the same account. If this is the case, you will indeed see this behavior. Either your rdp connection closes immediately or you just got a black screen.

    What you can check is the content of the file located at ~/.xsessionrc and possible share the content here, you should see something like this (if you are using Ubuntu + Gnome)

    export GNOME_SHELL_SESSION_MODE=ubuntu
    export XDG_CURRENT_DESKTOP=ubuntu:GNOME
    export XDG_DATA_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg

    To further help you, would you mind provide the following info

    OS version
    Dekstop interface you are using
    did you perform a standard install or custom installation ?

    You can also share the logs located under /var/log/xrdp-sesman.log and /var/log/xrdp.log
    waiting for your feedback
    Hope this help
    Till next time

  18. Thank you Griffon & Jason H.
    After installed xrdp-installer-1.3 on Ubuntu 21.10 , the sound driver missing, either local login or xrdp login. according to Jason H’s mention, I add ‘pulseaudio –start’ into Startup Application, the sound resume normal both local or xrdp login.
    Thanks for your wonderful work.

  19. @Zlaimw,

    Thank you for visiting our blog and providing feedback and findings…Indeed, there is still a known issue with sound and xRDP. We will try to tackle this issue in the next iteration of the script
    Till next time
    See ya

  20. >GiannoezNL says:
    >January 19, 2022 at 10:15 pm
    >Hi.
    >Installed this script succesfully, i can connect with remote desktop but when I entered my credentials, the >session is gone. (Remote Desktop is closing) So I see an login window and when I inter my credentials and >continue, the Remote Desktop is crashing or something..?

    I have faced this same problem too and finally gave up (Ubuntu 20.04 LTS). I think that it’s related to Nvidia Proprietary Driver I installed, can’t just get it work no matter what. xRPD was working earlier with Nouveau but it is no option as my system has stability issues with it. So I have to stick with VNC 🙁

  21. Hi Griffon,

    Thanks for your effort. I also have an issue with the script installed over SSH.
    I’m installing into Ubuntu 20.04 minimal + Gnome Descktop.
    ~/.xsessionrc doesn’t exist in my case at all.
    I have no option to login using graphical interface.
    Vote for fix to installation over SSH

    Thanks,
    Mikhail

  22. @Mikhail,

    Thank you for visiting our blog and providing some feedback. indeed, the script was not designed to run within an ssh session. SSH session does not allow the script to detect the Desktop interface and indeed can cause issues when trying to access the desktop remotely. We are working on a possible solution to that problem…We might need to come first with a quick fix and later on with a better solution

    Thank you for sharing your findings
    Till next time
    See ya

  23. @Jovi,

    Thank you for providing some feedback. Indeed, xRDP and Nvidia does not play well along. this is a known issue and the quick workaround is indeed to use the Nouveau Driver.
    The team behind xRDP has made some work in the past to have the software working with nVidia but we had no time to checl and follow on their progress….
    So, yes maybe vnc is your best option for the moment

    Till next time
    See ya

  24. Good afternoon.
    Could you tell me how to disable the clipboard and disk forwarding? I mean on the server side.
    Thank you.

  25. @Pavel,

    Thank you for visiting our blog and sharing your comments. To disable clipboard and drive redirection, you would need to follow these steps

    Step 1 – Open the xrdp.ini file using this command
    sudo nano /etc/xrdp/xrdp.ini

    Step 2 – find the [Channels] section in the file (usually at bottom of the file…
    Change value to false for the cliprdr (clibpoard redirection) and rdpdr (drive redirection). So, your file should look like this


    [Channels]
    ; Channel names not listed here will be blocked by XRDP.
    ; You can block any channel by setting its value to false.
    ; IMPORTANT! All channels are not supported in all use
    ; cases even if you set all values to true.
    ; You can override these settings on each session type
    ; These settings are only used if allow_channels=true
    rdpdr=false
    rdpsnd=true
    drdynvc=true
    cliprdr=false
    rail=true
    xrdpvr=true
    tcutils=true

    Then restart your xrdp service to use the new settings by issuing the command
    sudo systemclt restart xrdp

    hope this help
    till next time
    See ya

  26. how to make primary template profile ( desktop shortcut) that’s all my authenticated AD users
    will get the same profile
    thanks!

  27. @drlamer,

    Not sure to understand your question…but if you want that each user get a shortcut on their desktop, you might want to have a look at the /etc/skel approach… This might help you but honestly have no idea if this would work for AD authentication…. Not sure we can help here (at least not right away)….maybe someone else can provide some info

    Hope this help
    Till next time
    See ya

  28. Thank you Griffon ,
    let me rephrase the question 🙂

    Is there a way to execute a bash script (login.sh) whenever users session is established ?
    the bash script suppose to run \ deploy Firefox settings + certificate to users ~home folder
    I’m using Ubuntu 21.10 with active directory authentication

    Thanks!

  29. @drlamer,

    Thank you for your feedback :). So, you basically want to run some login scripts on your Ubuntu box. The most common way to have a login script at logon would be to use the startup application interface. There you can specify the location of the script that you would like to execute at login… We never test it with AD login process but we would expect that this would be working… Startup is a gui application that can be accessed through the gnome desktop activities, type in the search box startup, and you will find the application
    The startup application will create a shortcut under ~/.config/autostart…
    If you want to run a system wide script (for all users), you can try to place your file under /etc/xdg/autostart

    Hope this help
    Till next time

  30. @Brian,

    Thank you for your feedback and all the inputs and good tips you already provided 🙂 We were not aware of the possibility to use the x264… We will have a look and see if we can integrate it in the next version of the script
    The script will be released in April 2022 in order to include support for Ubuntu 22.04… We might provide some peview version if time allows it

    Keep in touch
    Till next time
    See ya

  31. Hello. RemoteFX is no longer working after latest patches to 21.10.
    And so RDP is incredibly sluggish.
    Can anyone else reproduce this?

  32. @Jon,

    Thank you for visiting our blog and providing some feedback…We are more and more focusing on Ubuntu 22.04 and we have not seen bad performance with xRDP.. on the contrary, seems even better than ever
    If time permit, we will update our ubuntu 21.10 and see if indeed something has changed ? If anyone has more info about this issue; please share

    Till next time
    See ya

  33. Hey Griffon,

    xrdp-installer-1.3.sh: This is a lifesaver for me!! It redefines the fact in a really good tool stays relevant no matter how old it becomes. Think of PSTools from Sysinternals. No wonder, I consider your script a secret recipe in my open-source cookery!

    Thanks for all the good work!! Please notify me when version 1.4 is out!

  34. @Giri,

    Thank you for visiting our blog and providing feedback. Really appreciate feedback and we are happy to see that our small little script can be helpful and fulfil its objectives : making xrdp install simpler 🙂
    We are working on version 1.4 indeed and should be released end of April 2022

    Till next time
    See ya

  35. Congratulations, very nice script. I finally have Ubuntu Mate with xrdp and sound.
    Thank you very much.

  36. @Enric,

    Thank you for visiting our blog and providing good feedback. Happy to see that the script can be used successfully against Ubuntu MATE edition. Thank you for sharing your positive experience with us

    Till next time
    See ya

  37. Thanks a lot for this script! Really saved me a lot of time when I switched my server to ubuntu!
    I couldn’t really find anything with the same issue. I just upgraded to 22.04 and I can’t connect to xorg over xrdp anymore, xvnc works just fine though.
    Is this a known issue or did anybody else experience this?

  38. @Robin,
    Thank you for visiting our blog and sharing your findings… Not sure we fully understand the question 🙂

    So, you had a Ubuntu 20.04 with xRDP and XVNC running fine => right ?
    You have performed the upgrade to Ubuntu 22.04 and know the xRDP connection not working anymore => right ?
    This operation usually does not block or prevent xRDP connection. As a first guess, I would check if you are using the screen sharing feature of Ubuntu 22.04… if this is the case, disable the RDP option and you should be able to connect

    More deep investigation steps,
    – Can you check that xrdp is still installed (xrdp -v)
    – Is the service running (sudo systemctl status xrdp)
    – Is teh xorgxrdp package still installed (dpkg -l | grep xorgxrdp)
    – Check your logs (/var/log/xrdp.log or /var/log/xrdp-sesman.log)
    What the error message you are getting ?

    Hope this help
    Waiting for some feedback
    Till next time

  39. Yes! Sorry I should’ve been more specific. I’m actually running XFCE. All services are installed and running fine.
    When choosing Xorg on the login screen (is this sesman?) it stays black for a while after timing out and kicking me back to the login screen. When choosing Xvnc it works fine though.
    I just noticed the xorgxrdp.11.log said that it failed because the permission for /dev/tty0 was denied which I fixied by adding the xrdp user to the tty group.

    Now theres another error though.

    I pasted the logs of my recent login attempt here:
    https://pastebin.com/ivifAwEV

  40. @Robin;

    Ok… Usually black screen and timeout means that you have the same user account logged on locally and you are trying to login remotely as well. Please ensure that the no-one is logged on your Ubuntu machine before trying the xRDP connection. xVNC is working because this limitation does not exists with VNC software…

    Check also that the ~/.xsessionrc file exists and if the issue persists, share the content of the file

    Hope this help

    Till next time
    See ya

  41. @Griffon

    Thanks for helping me out. That makes sense. I made sure nobody is logged in (not even ssh) but usually it’s running headless anyway and the gpu drivers are blocked on boot.

    I pasted the recent full log here:
    https://pastebin.com/wjxYjvUC

    As you can see it tries to access the videocard at /dev/dri/card0 but again permission is denied. I don’t even understand why it would need that? Somethings really odd here. Maybe I should just do a clean install 🙁

  42. @Robin,

    Ok…Checking your logs… The first thing we have noticed is the .xsessionrc which is set to
    # .xsessionrc
    export GNOME_SHELL_SESSION_MODE=
    export XDG_CURRENT_DESKTOP=
    export XDG_DATA_DIRS=

    This is not good an this might be your issue….

    So, to validate this assumption, let’s try to go back to a simpler configuration….
    Edit your /etc/xrdp/startwm.sh (you need admin rights/sudo rights) file and at the top you should see something like
    cat < ~/.xsessionrc
    export GNOME_SHELL_SESSION_MODE=
    export XDG_CURRENT_DESKTOP=
    export XDG_DATA_DIRS=
    EOF
    Comments these lines
    add this line at the end of the file
    echo “xfce4-session” > ~/.xsession
    save it

    then delete the ./xsessionrc file for the user that needs to perform the remote desktop connection
    restart the xrdp service
    logout
    and try again

    Question from us
    Which Ubuntu distribution are you running ? is this Ubuntu standard edition with Gnome Desktop and you have installed an additional xfce Desktop interface or you are running xubuntu Edition

    Waiting for some feedback
    Till next time
    see ya

  43. @Griffon

    Yea that seemed odd to me too. I just used the script to uninstall it completely and reinstalled with v1.4.
    Now it’s working perfectly fine again! I’m not sure what happened but somewhere along the line I must’ve messed it up.
    I started on ubuntu server and just added xfce after a while. I thought about it and I think at some point the sound stopped working and I probably ran the script again from ssh or tty directly. Maybe that was the cause of the issue.

    Thank you so much for your help, really appreciate it and so glad it’s working all fine now!

  44. @Robin,
    Ah ok good to know it’s fixed… Indeed, it might be because you ran the script from tty/ssh console. previous version of the script was not checking if ssh was used.. in version 1.4, we have tried to add some code to detect if ssh is used and the script will try to perform the install…

    Happy to see that everything is back to normal 🙂
    Till next time
    See ya

  45. @Griffon
    I’ve used this script many times in the past on Ubuntu 20.04 successfully using “-s -c”
    However, trying this again this week the Make fails.

    I found executing the script with NO options works.
    I found executing the script with ONLY the “-s” option works.
    But executing the script and specifying the “-c” option … the Make fails.

    Since the “-c” option pulls & uses the latest source code for XRDP from NeutrinoLabs I’ve gotta assume
    their latest source is the cause for failure.

Leave a Reply