xRDP – Easy install xRDP on Ubuntu 18.04,20.04,21.10,22.04 (Script Version 1.4)

  The script hereafter is outdated and has been replaced by a newer version.  Please Check the Download page for latest version. 

Hello World,

As you probably know by now,  Ubuntu 22.04 LTS has been released (April 2022, 21) and it’s time for us to provide an updated version of our famous xrdp-installer script.   Ubuntu 22.04 has introduced some changes that might impact the way xRDP installation behave.  This new release could be seen as an important maintenance release since we are including support for Ubuntu 22.04 (LTS Release). We have also introduced a new piece of code that might be useful in really specific scenario.    

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

Please note that most of the tests have been done on Ubuntu 22.04 with Gnome Desktop Interface and Debian 11.  

So, let’s go ! 

Overview

The xrdp-installer-1.4.sh script ease the installation and post-configuration actions of xRDP on top of Ubuntu Operating System but also against Debian Distribution.  Let’s what’s new in this release…. 

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

Adding Ubuntu 22.04 as supported OS !

The script xrdp-installer-1.4.sh can be run against all supported Ubuntu Operating System.  Version 1.4 has been updated and it will detect Ubuntu 22.04 as a supported Operating system. The script has been mainly tested against Ubuntu Gnome desktop.  However, the script tries to detect the Desktop interface in use and might be able to work against some other desktop interface such as Budgie, Plasma, Linux Mint, Gnome…(Best effort !)

Removed Ubuntu 21.04 as supported OS !

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

Debian 11 – Enforcing custom installation 

Since there is a known issue with Debian 11 and xrdp package shipping with it (please read  xRDP – Detected issues with Debian 11 – Oh No ! Something has gone wrong….), we have adapted and improved the script in order to “force” the Custom installation mode on Debian 11.  We have improved the warning message in this version and it should be providing a better user experience for Debian 11 users… If you try the standard mode install, the script will quit and clearly mention that you need to run custom mode…

Added Pop!_OS 21.10 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) in the previous versions of the script.  The script version 1.4.  has been updated and it should also detect Pop!OS 21.10 as a “detected OS” and the script will run accordingly…  

Detecting Desktop Interface in use  (Best effort !!)

As mentioned earlier, the script was originally designed to work against Ubuntu running the Ubuntu Desktop interface.  As requested by more and more users, the script tries to “detect” other Desktop interfaces such as budgie, Plasma, Gnome,… and adapt the post configuration accordingly. This is still work in progress status as 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. 

Important ! 

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 update the /etc/xrdp/startwm.sh manually to include the correct information or possibly re-run the script so the file will be updated accordingly (to be tested).

Detecting script execution from SSH Session (Experimental !)

This is  a new and experimental part in the script.  With the previous script, we have seen a lot of user complaining that the script is not providing expected results when run from an ssh session.  This is because when inside an ssh session, the variable we are using to detect the desktop interface, are not populated and the post configuration actions are simply failing. To overcome this situation, we have added an additional check to see if the script is run from an ssh session.  If this is the case; again the script will try to detect the desktop interface to be used.  If more desktop interfaces are detected, a menu will be presented to the user and he will be able to specify the desktop interface to be used in the remote session 

Please note that this is quick and dirty approach and we have to test it more intensively. However, we wanted to include it in this version as a peak preview….

Sound redirection in Ubuntu 22.04 

In Ubuntu 21.10 and Ubuntu 22.04, PulseAudio Version provided is the version 15.0 or later.   Because PulseAudio 15.0 is not using the autoconf tools anymore but use more “modern” meson approach, the sound redirection code has been re-written in the script version 1.3.   

xRDP team has recently updated the sound redirection compilation process and they are providing handy scripts to build the necessary modules.  We have tested the procedure and all in all this is working.   However, we have noticed that the process fails against some OSes (like Pos!_OS) when using the convenience script provided by he xRDP Team.  

In our script version 1.4, we have decided not to use the xrdp convenience script and we are using our “custom” code to compile xrdp sound modules.  The script should be able to detect PulseAudio version and perform either old school compilation or use the new meson technology.  

Gnome Tweaks Tool not installed anymore by default  

The xrdp-installer script version 1.4 will not install anymore by default the application Tweak on systems.  Only systems running a Gnome based desktop will get the Tweak application installed.  

The Tweak application might be less needed in Ubuntu 22.04 since the Ubuntu is offering more and more tools to manage OS appearance.  However, since we are still supporting Ubuntu 18.04, Tweak tool might still be needed   

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.  

Download the Script 

The script can be download at this location  :    xrdp-installer-1.4.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.4 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.4.sh script will support the following Ubuntu operating system version assuming that Gnome Desktop is the default Desktop interface

  • Ubuntu 18.04.x
  • Ubuntu 20.04.x
  • Ubuntu 21.10.x
  • Ubuntu 22.04.x
  • Debian 10 
  • Debian 11 (only if you select custom mode installation !!!) 
  • Pop!_OS (Best Effort)

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

  • 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)   

Note :  If you are running any of these distributions and you want to help us improving the support on those, please download and test the script.  Do no hesitate to share your findings so we can try to incorporate them in the next release 

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.4.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.4.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.4.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.4.sh

Note : Adjust the path where the xrdp-Installer-1.4.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.4.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.4.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.4.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.4.sh -s      (this would enable the sound redirection) 
./xrdp-installer-1.4.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.4.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.4.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.4.sh -c -s     (this would perform a custom install and enable sound redirection)
./xrdp-installer-1.4.sh -c -l     (this would perform a custom install and customize xrdp login screen)
./xrdp-installer-1.4.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.4.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, Fixed Issues and limitations 

Known Issues 

Drive Redirection  Issue (and fixed in xRDP 0.9.19 !)

If you are running xrdp version 0.9.19 or later, you do not need to check your redirection settings in your rdp client.  The version 0.9.19 has fixed the issue and drive redirection is working as expected now. 

However, If you are using a version lower than 0.9.19,  you still have this drive redirection issue. 

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 (fixed!)

Sound redirection seems to work much more better if you are using pulseaudio version 15.0 or later.   In Ubuntu 22.04, the sound redirection issue seems to be solved.  If you reboot your machine after installing xrdp modules for sound, you will have sound available immediately.  We have compared also sound redirection between Ubuntu 20.04 and 22.04 and the quality and experience is much better in Ubuntu 22.04.  Again, we think that the new pulseaudio version brings this improvements. 

The issue were sound redirection was not working/starting automatically has been solved. The xRDP team has released an updated version of the source software and now, there is a script located in /etc/xdg/autostart that will ensure that the modules are loaded when desktop is loaded

xrdp-sound_1.4

Click on Picture for better resolution

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….  

In the past, we have provided some workarounds that could be used to allow multiple connections for the same user.  However, in Ubuntu 22.04, we have encountered some additional issues and again we strongly recommend not using these workarounds…..

Some workarounds (provided as is) to try to fix the issues are available in the following posts 

Use at your own risk !!!

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.4.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.4.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.4.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 : April 2022
# written by : Griffon
# WebSite :http://www.c-nergy.be - http://www.c-nergy.be/blog
# Version : 1.4  
# History : 1.4   - Re-write sound section (since meson is used)
#                 - Added Pop!Os 21.10 as Detected system (Best Effort !!!)
#                 - Detect when install from ssh session - (Experimental)
#                 - Adding Support Ubuntu 22.04
#                 - Removing Support for Ubuntu 21.04 (End Standard Support)
#                 - Improved Debian detection and warning about std vs custom install
#         : 1.3   - Adding support for Ubuntu 21.10 (STR)
#                 - Code modification sound redirection using meson technology (ubuntu 21.10 only so far)
#                 - Adding support for Debian (10 and 11) (Best Effort)
#                 - 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.4"

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

#Define Dwnload variable to point to ~/Downloads folder of user running the script
Dwnload=$(xdg-user-dir DOWNLOAD)

#Initialzing other variables
modetype="unknown"

#---------------------------------------------------------#
# Initial checks and Validation Process ....              #
#---------------------------------------------------------#

#-- 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

#--Detecting variable related to Desktop interface and Directory path (Experimental)
if [[ *"$XDG_SESSION_TYPE"* = *"tty"*  ]]
then
    ##-- Detect if installation done via ssh connections
    /bin/echo -e "\e[1;32m       |-| Detected Installation via ssh.... \e[0m"
    echo
    # Need new code to display DE Option available
    /bin/echo -e "\e[1;33m  !--------------------------------------------------------------!\e[0m"
    /bin/echo -e "\e[1;33m  ! Your are using the xrdp-installer script via ssh connection  !\e[0m"
    /bin/echo -e "\e[1;33m  ! You might need to create your ~/.xsessionrc file manually    !\e[0m"
    /bin/echo -e "\e[1;33m  !                                                              !\e[0m"
    /bin/echo -e "\e[1;33m  ! The script will proceed....but might not work !!             !\e[0m"            
    /bin/echo -e "\e[1;33m  !--------------------------------------------------------------!\e[0m"
    echo
    cnt=$(ls /usr/share/xsessions |  wc -l)
    echo$cnt
    if [ "$cnt" -gt "1" ]
    then
        PS3='Please specify which DE you are using...: '
        desk=($(ls /usr/share/xsessions | cut -d "." -f 1))
  
    select menu in "${desk[@]}";
    do
    echo -e "\nyou picked $menu ($REPLY)"
    break;
    done
  else
    menu=$desk
    echo "Desktop seems to be based on....: " $menu
    fi

    case $menu in
    "ubuntu")
    DesktopVer="ubuntu:GNOME"
    SessionVer="ubuntu"
    ConfDir="/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop"
    /bin/echo -e "\e[1;32m       |-| Session         : $SessionVer\e[0m"
    /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

    "gnome")
    DesktopVer=""
    SessionVer=""
    /bin/echo -e "\e[1;32m       |-| Session         : $SessionVer\e[0m"
    /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

    "budgie-desktop")
    DesktopVer="Budgie:GNOME"
    /bin/echo -e "\e[1;32m       |-| Session         : $SessionVer\e[0m"
    /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;
    "plasma")

    DesktopVer="KDE"
    SessionVer=""
    /bin/echo -e "\e[1;32m       |-| Session         : $SessionVer\e[0m"
    /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
        ;;

    "pop")
    DesktopVer="pop:GNOME"
    SessionVer="pop"
    /bin/echo -e "\e[1;32m       |-| Session         : $SessionVer\e[0m"
    /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

   "mate")
    DesktopVer="MATE"
    SessionVer=""  
    /bin/echo -e "\e[1;32m       |-| Session         : $SessionVer\e[0m"
    /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

    "cinnamon2d")
    DesktopVer="X-Cinnamon"
    SessionVer=""  
    /bin/echo -e "\e[1;32m       |-| Session         : $SessionVer\e[0m"
    /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

    "cinnamon2")
    DesktopVer="X-Cinnamon"
    SessionVer=""  
    /bin/echo -e "\e[1;32m       |-| Session         : $SessionVer\e[0m"
    /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

    "xfce")
    DesktopVer="XFCE"
    SessionVer=""  
    /bin/echo -e "\e[1;32m       |-| Session         : $SessionVer\e[0m"
    /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;
    
    "lxqt")
    DesktopVer="LXQt"
    SessionVer=""  
    /bin/echo -e "\e[1;32m       |-| Session         : $SessionVer\e[0m"
    /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;
  
    "LXDE")
    DesktopVer="LXDE"
    SessionVer=""  
    /bin/echo -e "\e[1;32m       |-| Session         : $SessionVer\e[0m"
    /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;  
    *)

    /bin/echo -e "\e[1;31m  !--------------------------------------------------------------!\e[0m"
    /bin/echo -e "\e[1;31m  ! Unable to detect a supported OS Version & Desktop interface  !\e[0m"
    /bin/echo -e "\e[1;31m  ! The script has been tested only on specific versions         !\e[0m"
    /bin/echo -e "\e[1;31m  !                                                              !\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
else
    ##-- Installation is performed via an existing Desktop Interface...Trying to detect it....
    DesktopVer="$XDG_CURRENT_DESKTOP"
    SessionVer="$GNOME_SHELL_SESSION_MODE"
    ConfDir="$XDG_DATA_DIRS"
fi

#--------------------------------------------------------------------------#
# -----------------------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.10"*)
   /bin/echo -e "\e[1;32m       |-| OS Version : $version\e[0m"
   /bin/echo -e "\e[1;32m       |-| Desktop Version : $DesktopVer\e[0m"
    ;;

    *"Ubuntu 22.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 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"
   ;;

   *"Pop!_OS 21.10"*)
   /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" ]] && [[ -z "$adv"  ]]
   then
        #Check if Custom Install already performed...if yes, enable sound
        if [[ $modetype = "custom" ]] && [[ $fixSound = "yes" ]]
        then
            /bin/echo -e "\e[1;32m       |-| Install Mode (Debian)   : Custom...Proceeding\e[0m"
            /bin/echo -e "\e[1;32m       |-| Enabling Sound (Debian) : .........Proceeding\e[0m"
        else    
            /bin/echo -e "\e[1;31m  !--------------------------------------------------------------!\e[0m"
            /bin/echo -e "\e[1;31m  ! You are running Debian 11 ! Please note that standard Mode   !\e[0m"
            /bin/echo -e "\e[1;31m  ! will not allow you to perform remote connection against      !\e[0m"
            /bin/echo -e "\e[1;31m  ! Gnome Desktop. This is a known Debian/xRDP issue             !\e[0m"
            /bin/echo -e "\e[1;31m  ! Use custom install mode                                      !\e[0m"
            /bin/echo -e "\e[1;31m  !                                                              !\e[0m"            
            /bin/echo -e "\e[1;31m  ! The script is exiting...                                     !\e[0m"            
            /bin/echo -e "\e[1;31m  !--------------------------------------------------------------!\e[0m"
            echo
            exit
        fi  
   else
     /bin/echo -e "\e[1;32m       |-| Install Mode (Debian)  : Check Done...Proceeding\e[0m"
   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  ! Ubuntu 18.04.x/20.04.x/22.04/21.10/Debian 10/11              !\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()
{
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"

#Debian Specific - add in source backport package to download necessary packages - Debian Specific
if [[ *"$version"* = *"Debian"*  ]]
then
sudo sed -i 's/deb cdrom:/#deb cdrom:/' /etc/apt/sources.list
sudo apt-get update
sudo apt-get install -y software-properties-common
sudo apt-add-repository -s -y 'deb http://deb.debian.org/debian '$codename'-backports main'
sudo apt-get update

#--Needed to be created manually or compilation fails
sudo mkdir /usr/local/lib/xrdp/
fi

#--End Debian Specific --#
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
#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

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
    sudo apt-get install gnome-tweak-tool -y
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 12 - 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
/bin/echo -e "\e[1;32m          |-| Install dev tools used to compile sound modules..     \e[0m"
echo
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
else
# Step 1 - Enable Source Code Repository
/bin/echo -e "\e[1;32m          |-| Adding Source Code Repository..     \e[0m"
echo
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
fi
echo
/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
apt source pulseaudio
/bin/echo -e "\e[1;32m       |-| Compile pulseaudio sources files..     \e[0m"

# Step 4 - Compile PulseAudio based on OS version & PulseAudio Version
cd /tmp/pulseaudio-$pulsever*
PulsePath=$(pwd)
cd "$PulsePath"
    if [ -x ./configure ]; then
        #if pulseaudio version <15.0, then autotools will be used (legacy)
        ./configure
    elif [ -f ./meson.build ]; then
          #if pulseaudio version >15.0, then meson tools will be used (new)
        sudo meson --prefix=$PulsePath build
        sudo ninja -C build install
    fi

# step 5 - Create xrdp sound modules
cd /tmp
/bin/echo -e "\e[1;32m       |-| Compiling and building xRDP Sound modules...     \e[0m"
git clone https://github.com/neutrinolabs/pulseaudio-module-xrdp.git
cd pulseaudio-module-xrdp
./bootstrap
./configure PULSE_DIR=$PulsePath
make

#this will install modules in /usr/lib/pulse* directory
sudo make install
}

#---------------------------------------------------#
# 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 - April 2022                      !\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 - April 2022  -  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 ./debian-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
    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

#---------------------------------------------------------------------------------------
#- 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

 

29 thoughts on “xRDP – Easy install xRDP on Ubuntu 18.04,20.04,21.10,22.04 (Script Version 1.4)

  1. Thank you for this. For Ubuntu Mate 22.04, Mate was not detected so I forced the install by copy/pasting from the Mate portion of the case statement to the default. After a reboot I was good to go (although I do tend to get a Mate applet crash upon connection, but that’s just Mate things).

  2. @Ray,

    Thank you for visiting our blog and providing positive feedback… Thank you for letting us know that MATE is not detected….we are planning to review soon the script and adapt it accordingly
    We have decided to release version 1.4 almost at the same time as Ubuntu 22.04 but we still need to perform some additional testing and validation with it… Work in Progress

    Till next time
    See ya

  3. @Ray,

    Thank you for spotting the issue with MATE desktop… After quickly checking, if you run the script for the Desktop interface, everything works fine. The problem only occurs when trying to install via SSH… There was a missing line in the script. The script has been updated. If you have time, you could test again and provide feedback to us
    Well spotted 🙂

    Till next time
    See ya

  4. Thank you for the script – I’ve run it on my Ubuntu 22.04LTS system and when I try and log in over RDP I get the “Oh no, something went wrong screen”.

    it’s 22.04 server with sddm and xfce4 installed. I get a GUI on the console (and I’ve double checked to make sure I wasn’t logged in there), but no dice over RDP.

  5. Thanks you for your work on xrdp!
    gnome-tweak-tool seems to have been renamed to gnome-tweaks in 22.04.

  6. @Skwee,

    Thank you for visiting our blog and providing valuable information…. Damn, they have changed the name again….:) Thanks for letting us know, we will update the script asap and take into account your findings
    Again, thank you for letting us know so we can fix it and improve it as we go….
    Till next time
    See ya

  7. @Graham,

    Thank you for visiting our blog and providing feedback… Ok, What you have might be one of the combination we never really tried 🙁

    First things first, can you share the content of your ~/.xsessionrc file ?
    How did you perform the install, directly from your desktop interface of via ssh access ?
    Then, we will need to check xrdp and sddm combination to see how it works….

    waiting for your feedback
    Till next time
    See ya

  8. Hi Griffon,
    thank you for the script.
    I had xrdp working earlier without the script and Budgie desktop, but I messed around and started from scratch with a clean 22.04 server installation and installed the “xubuntu-desktop” package.
    First I tried the “xubuntu” option of the script, but it said that it cannot find it.
    Then I tried “Xfce” and the script went fine.
    But now I’m also getting the “Oh no! Something has gone wrong.” screen during logon.
    In my opinion this has something to do with the installation options. I have plain Ubuntu, not Xubuntu. Instead I installed the “xubuntu-desktop” package and not the “xfce4” package, but the script configured everything for Xfce.
    The content of the ~/.xsessionrc is this:

    export GNOME_SHELL_SESSION_MODE=
    export XDG_CURRENT_DESKTOP=XFCE
    export XDG_DATA_DIRS=

    I replaced XFCE with xubuntu, which brought me a bit further.
    The error did not occur, but I get a teal screen with a mouse pointer instead.

    Thanks for your help!
    Ciao
    Alex

  9. @Alex,

    Thank you for visiting our blog and sharing your findings… Ok, we have quickly checked your configuration…and we have been able to reproduce the issue. Your configuration is something we never looked at before…But the positive point is that we can improve a a step further our script 🙂

    To fix your issue, you have to perform the following actions (we will do it from Terminal console)

    Open the /etc/xrdp/startwm.sh file with admin rights (

    sudo nano /etc/xrdp/startwm.sh

    )
    You will see something like this….

    #!/bin/sh
    # xrdp X session start script (c) 2015, 2017, 2021 mirabilos
    # published under The MirOS Licence

    #Improved Look n Feel Method
    cat < ~/.xsessionrc
    export GNOME_SHELL_SESSION_MODE=
    export XDG_CURRENT_DESKTOP=XFCE
    export XDG_DATA_DIRS=/usr/share/xfce4:/usr/share/xubuntu:/usr/share/gnome:/usr/>
    EOF
    (...)

    So to fix your issue, just add after the EOF, add the following line and save the file

    #Add this line to get xfce working
    echo xfce4-session > ~/.xsession

    So, end result should look like after your modification

    #!/bin/sh
    # xrdp X session start script (c) 2015, 2017, 2021 mirabilos
    # published under The MirOS Licence

    #Improved Look n Feel Method
    cat < ~/.xsessionrc
    export GNOME_SHELL_SESSION_MODE=
    export XDG_CURRENT_DESKTOP=XFCE
    export XDG_DATA_DIRS=/usr/share/xfce4:/usr/share/xubuntu:/usr/share/gnome:/usr/>
    EOF
    #Add this line to get xfce working
    echo xfce4-session > ~/.xsession

    (…)

    Let us know if this is working for you and we will update the script to take into account this situation

    Hope the info is clear enough and that would fix your issue
    Till next time
    See ya

  10. Hi Griffon,
    thank you for the fix. It is clear enough, don’t worry.
    I added the line and restarted xrdp.
    Unfortunately it does not work for now. The session shows a black screen, then closes itself.
    The file .xsession-errors shows these last lines:
    ——————
    dbus-update-activation-environment: setting XDG_DATA_DIRS=
    dbus-update-activation-environment: setting CLUTTER_IM_MODULE=ibus
    xfce4-session: GNOME compatibility is enabled and gnome-keyring-daemon is found on the system. Skipping gpg/ssh-agent startup.
    /home/master/.xsession: line 1: 26968 Segmentation fault (core dumped) xfce4-session

    (xfwm4:26979): xfwm4-WARNING **: 11:44:03.618: GLX version 0 is too old, GLX support disabled.
    ————————————

    Regardless of the core dump, the XDG_DATA_DIRS variable is empty in my /etc/xrdp/startwm.sh.
    Does this matter?

    I’m also curious about the “xfce4-session”, as Ubuntu differs between plain Xfce (xfce4-session) and Xubuntu-extended Xfce (xubuntu-session).
    Anyway, I tried both and both fail. 😉

    Many Thanks
    Alex

  11. @Alex,

    Thank you for the feedback… we have tried on a brand new installation. We have deployed Ubuntu Server, then we have installed xubuntu-desktop. We have then run the script and adapted startwm.sh accordingly and the xfce session started with no problem.

    You have to ensure that the XDG_DATA_DIRS is populated in /etc/xrdp/startwm.sh
    Need to be populated like this….(see below)
    XDG_DATA_DIRS=/usr/share/xfce4:/usr/share/xubuntu:/usr/share/gnome:/usr/local/share:/usr/share:/var/lib/snapd/desktop:/usr/share

    Double-check also that ~/.xsession file… it should only contains one line
    xfce4-session

    Note
    The difference between xubuntu-desktop and xfce4 is that xubuntu-desktop comes with xfce4 + some default settings + some extra packages and tools.

    Hope this help
    Please provide feedback so we can adjust the script accordingly 🙂
    Till next time
    See ya

  12. Hi Griffon,

    thanks for your script!

    In version 1.4 the custom parameter seems to be broken.

    During git checkout of xrdp something is missing with libpainter and rfxcodec.

    Compile gives an error message.

    When I switch to Downloads/xrdp and do a manual bootstrap, it tells me, that libpainter and rfxcodec folders are empty.

    After manual bootstrap and re-running your script, all changes in xrdp are destroyed by repulling the whole folder.

    Can you find the circumstance I’m describing here? 🙂

    Thanks again for your cool work!

    JERKBALL

  13. Alright, now it works. Great! 🙂

    It must have been the empty path for XDG_DATA_DIRS.
    I have no idea why this happened, as I installed xubuntu-desktop first before using the script.
    But I’m happy it works now.
    Thanks for the awesome support.

    Ciao
    Alex

  14. @Alex,

    Ah Good to hear that this is working for you…. Based on your feedback we will review/rework the script in order to take these situations into account… Initially, the script was supposed to run only on Ubuntu + Gnome. As we are adding more and more desktop interfaces, we are discovering some additional tweaks that needs to be integrated into the script…. a minor version might be published soon

    Thank you for sharing and testing the script….
    Till next time
    See ya

  15. @JerkBall,

    Thank you for visiting our blog and sharing your experience… It seems you are not the only one bumping into this situation…. We think we know why this is happening and related to the Git download…. We will publish soon a nez version minor (1.4.1) that will tackle this situation and introduce some improvements in the ssh support (which is experimental in the version 1.4)
    To solve your issue, you can try to add the option --recursive in the git clone command used to download xrdp and xorgxrdp packages

    Till next time
    See ya

  16. Hello!
    First of all, thank you! I have been using your script for years. I am just a regular user and trying to arrange RDP before your script has appeared was a real maddness.
    I use Ubuntu as VM on hyper-v.
    Today I’v installed brand new Ubuntu 22 as VM and tried your script. It really worked since the beginning but after reloading VM sound redirection stopped working at all. -r Does not help

  17. I found out the reason. Pulseaudio is not started automatically. Instead of pulseaudio -k, now you just have to print pulseaudio

  18. @Someone 🙂

    Thank you for visiting our blog and sharing your experience….We didn’t encounter this issue so far but we will have a look and see if we can reproduce the issue
    thanks for providing the fx as well

    Till next time
    see ya

  19. @Griffon

    I found this was the same problem I just posted about in the Script v1.3 comment section.

    Executing:

    xrdp-installer-1.4.sh -s -c # Fails in multiple areas
    xrdp-installer-1.4.sh -s # succeeds

    So again I think since the “-c” option uses the Latest XRDP source code from NeutrinoLabs to compile/make etc I think this may be a NeutrinoLabs problem with their latest Source code.

    I believe this because using ONLY the “-s” option will just use the XRDP and XORGXRDP already in the Ubuntu Repositories… those apparently don’t have the BUGS that the Latest XRDP Source code must now have.

  20. @Griffon
    Just some more FYI..

    Starting the script with “-c” as I mentioned in previous posts fails.

    I noticed that there was a FATAL error when GCC tries to compile “conftest.c” (maybe its configtest.c).

    The Fatal error is pointed out to be that one of the GCC command options only used a single Dash (-) instead a double Dash (–)

    I’m not sure if that GCC command is part of cenergy’s Script or NeutrinoLab’s XRDP code??

  21. @Brian,

    Thank you for notifying us and sharing your findings…. We were busy with some projects and we will have a look at the script… The -c switch worked well when tested against ubuntu 22.04 beta version. So, indeed, might need to check the xrdp/xorgxrdp repo as well… will take us a few days and we will come back to you

    Again, thanks for letting us know

    Till next time
    See ya

  22. I can’t seem to type in the “Authentification Required” screens in Ubuntu 22.04. It’s a full fresh install from ISO, nothing custom.

    Has anyone else had this issue or found a solve?

  23. @ngkrich,

    thank you for visiting our blog and sharing your experience. So, can you provide a screenshot of the authentication Required issue. Can you also explain how you have performed the installation (standard installation or custom mode)
    For which actions are you requested authentication Required. What do you mean I can’t seem to type in the Authentication required…. Do you get prompted multiple times, or totally blocked.. have you tried to press cancel button

    waiting for your feedback
    Till next time
    See ya

  24. In using the 1.4 version of the xRDP installer script I’m finding that sound redirection does not work on any of my versions of Ubuntu 22.04 either in VirtualBox or in a laptop. After installation of the 1.4 version installer script all sound is gone upon reboot, both from remote desktop connection and normal logon. Sound can be started by using the command ‘pulseaudio –start’ and this will work for both remote desktop connection and normal logon. Also if you add ‘/usr/bin/pulseaudio’ to the Ubuntu Startup Applications then sound will work for both remote desktop connections and normal login. Thought you might like to know about this. Thanks for all your work on RDP with Ubuntu.

  25. @Mike,

    Thank you for visiting our blog and providing your feedback and experience. We are looking at multiple issues with version 1.4 script 🙂 … We will add the sound redirection as well…. We hope to provide an updated version in a few weeks… Apparently, there are some changes in the xRDP code and Ubuntu 22.04 has been released recently….so we are trying to catch up and try to fix issues detected so far…. Please feel free to download the future version and test it if you have time and report back any issues you might find

    Thanks for letting us know about your findings
    Till Next time
    See ya

  26. Hello,

    Regarding xfce4/xubuntu, “startxfce4” is better than “xfce4-session”
    This can be obtained from the value of EXEC= in /usr/share/xsessions/xfce4.desktop or /usr/share/xsessions/xubuntu.desktop
    To execute the value of EXEC in .xsession works most Ubuntu/Debian base desktop environment.

    I tried to replace line 686 of xrdp-installer-1.4.sh (in fix_theme()) with following five line code.
    This works many DEs including Budgie, Linux MINT20 cinnamon and Pop!_OS 22.04 LTS.

    if [ -z “$menu” ]; then menu=$XDG_SESSION_DESKTOP; fi
    DesktopPath=”/usr/share/xsessions/\\\\\${XDG_SESSION_DESKTOP}.desktop”
    XSESSIONRCscript=”if [ -z \”\\\\\$XDG_SESSION_DESKTOP\” ]\nthen\nexport XDG_SESSION_DESKTOP=$menu\nexport DESKTOP_SESSION=\\\\\$XDG_SESSION_DESKTOP\nexport GDMSESSION=\\\\\$XDG_SESSION_DESKTOP\nexport XDG_CURRENT_DESKTOP=\\\\\$(sed -e ‘s/;/:/g’ -n -e ‘s/^DesktopNames=//p’ ${DesktopPath})\nfi”
    XSESSIONscript=”exec \\\\\$(sed -n -e ‘s/^Exec=//p’ ${DesktopPath})”
    sudo sed -i “4 a #Improved Look n Feel Method\ncat < ~/.xsessionrc\n${XSESSIONRCscript}\nEOF\ncat < ~/.xsession\n${XSESSIONscript}\nEOF\n” /etc/xrdp/startwm.sh

  27. @Hiero,

    Thank you for visiting our blog and sharing your findings and experience. We are working on trying to improve the script (version 1.4) since minor issues have been discovered. We will look into your code and might be integrated in the script in the next release

    thank you for sharing ideas and findings
    Till next time
    See ya

  28. I did simple tests using this code for many DEs, via ssh and local.
    ex. ubuntu, gnome-flashback-metacity, gnome, xfce, xubuntu, lxqt,Lubuntu, budgie, plasma…
    I home this will help.

  29. @Hiero,

    Thank you for sharing your findings and sharing your code… We are working on the version 1.4.2 of the script. We will test your code and see if this could indeed replace our old piece of code. Much appreciated…
    Thank you for performing all these tests…this is really help us since we have less and less time to perform complete testing process

    Stay Tuned
    Till next time
    See ya

Leave a Reply