Ubuntu 22.04 - XRDP Enabling Multiple Sessions and Pause/Resume

If you haven't already, please refer to the URL below for how to install and use XRDP in Ubuntu 22.04.

Ubuntu 22.04 - XRDP Installation and Configuration

Table of Contents

Configurations to Enable Multiple Sessions

  1. Set user Window Manager settings in /etc/xrdp/sesman.ini as

    $ sudo vi /etc/xrdp/sesman.ini
    ...
    [Globals]
    …
    EnableUserWindowManager=true
    ; Give in relative path to user's home directory
    UserWindowManager=.vnc/startwm.sh
    ; Give in full path or relative path to /etc/xrdp
    DefaultWindowManager=startwm.sh
    ...
  2. Change the Policy to UBC in /etc/xrdp/sesman.ini (UBC means User, Bit Per Pixel, Connect. A new session will be created upon every connection.)

    $ sudo vi /etc/xrdp/sesman.ini
    …
    [Sessions]
    …
    Policy=UBC
    …
  3. The DBUS setting is required to allow the same user to have multiple sessions. Add the following to the top of the /etc/X11/xint/Xsession file.

    $ which dbus-launch || sudo apt install dbus-x11
    $ sudo vi /etc/X11/Xsession
    #!/usr/bin/bash
    export $(dbus-launch)
    ...

    Note: This command can be executed in startwm.sh (mentioned above), but this setting is necessary for multiple sessions, even when connecting via XDMCP, etc., so it is recommended you set it in the Xsession file that is commonly used with xrdp.

Configurations to Enable Reconnecting to Sessions

  1. In the startwm.sh file created in the path defined in step 1 of Configurations to Enable Multiple Sessions above, add the following content

    $ vi .vnc/startwm.sh
    cp ${HOME}/.vnc/passwd ${HOME}/.vnc/sesman_passwd-${USER}@$(hostname)$(echo "$DISPLAY" | sed  -E "s/.*:([0-9]{1,})\.0/:\1/")
    if [ -f "$HOME/.vnc/startwm.sh" ] && [ -x "$HOME/.vnc/startwm.sh" ]; then
            $HOME/.vnc/startwm.sh
    else
            exec /bin/sh /etc/X11/Xsession
    fi
  2. For each user who will use Xrdp, use the vncpasswd command to create the $HOME/.vnc/passwd file described above.

    $ vncpasswd
    Password: 
    Verify: 
    Would you like to enter a view-only password (y/n)? n 
    A view-only password is not used
  3. With Xrdp, you can either utilize Xorg sessions or Xvnc sessions. If you will only utilize Xvnc sessions, you can skip this step. For Xorg sessions, the following process is required.

    1. Install and configure x11vnc

      $ sudo apt install x11vnc
    2. Set x11vnc to run automatically in /etc/xrdp/startwm.sh.

      $ sudo vi /etc/xrdp/startwm.sh
      x11vnc -repeat -display $DISPLAY -shared -forever -rfbauth $HOME/.vnc/passwd -rfbport 59`printf %02d $(expr match "$DISPLAY" '.*:\([0-9]*\).*')`&
      cp ${HOME}/.vnc/passwd ${HOME}/.vnc/sesman_passwd-${USER}@$(hostname)$(echo "$DISPLAY" | sed  -E "s/.*:([0-9]{1,})\.0/:\1/")
      if [ -f "$HOME/.vnc/startwm.sh" ] && [ -x "$HOME/.vnc/startwm.sh" ]; then
              $HOME/.vnc/startwm.sh
      else
              exec /bin/sh /etc/X11/Xsession
      fi

      Explanation:

      • x11vnc allows you to connect to the Xorg server via vnc.

      • The goal of this command is to make the values of the Xorg display number and the vnc port number the same.

      • -repeat: This option allows for keys to be inputted continuously.

      • -shared: Screen sharing option.

      • -forever: Option to wait for the next connection even if the vnc session is closed.

      • -rfbauth: Specifies the file to use as the connection password for the vnc session.

      • -rfbport: Port number to listen for the vnc connection. The above command links this with the value of $DISPLAY.

Launch a New Xrdp Session and Reconnect

You can choose either Xvnc or Xorg as the session type for the Xrdp session.

Create and pause an Xvnc session (Stays running in the background)

  1. From Xmanager, launch an RDP session. Select Xvnc as the session type.


  2. Enter your username and password (User Password) and initialize the session. Input of the password is required.

  3. Check the $DISPLAY value in a terminal.
    e.g.) :30.0 => 30 (Remember the number 30. The screen number, .0, can be ignored)


  4. To pause a session, close the Xmanager RDP window of Xmanager without logging out. If you log out, the session will terminate.

Reconnect to an existing Xvnc session

  1. From Xmanager, launch the relevant RDP session, and select vnc-any as the session type.

  2. In the Port field, enter the number obtained by adding 5900 to the display number in step 3 above.
    e.g.) 30 => 5930

  3. In the Password field, enter the password determined by the vncpasswd command in step 2 of the Configurations to Enable Reconnecting to Sessions section above (username does not need to be entered).

Create and pause an Xorg session (Stays running in the background)

  1. From Xmanager, launch an RDP session. Select Xorg as the session type.


  2. Enter your username and password (User Password) and initialize the session. Input of the password is required.

  3. Check the $DISPLAY value in a terminal.

    $ echo $DISPLAY
    :13.0
    $ 

    In the example above, the $DISPLAY is :13.0. Previously, we had used the x11vnc command to link the display number (13) and the VNC standby port. Add 5900 to this value. Thus, in this example, VNC will listen at number 5913.

  4. To pause a session, close the Xmanager RDP window of Xmanager without logging out. If you log out, the session will terminate.

Reconnect to an existing Xorg session

  1. From Xmanager, launch the relevant RDP session, and select vnc-any as the session type.

  2. In the Port field, enter the number obtained by adding 5900 to the display number of step 3 above.
    e.g.) 13 => 5913

  3. In the Password field , enter the password determined by the vncpasswd command in step 2 of the Configurations to Enable Reconnecting to Sessions section above (username does not need to be entered).



Example Script to Check an Xrdp Session

#!/bin/bash
echo -e "${USER}'s xrdp sessions:\n"
printf "%s\t%s\t%s\t%-10s%s\n" "DISPLAY" "VNCport" "Depth" "Size" "Type"
ps -ux |grep -E 'Xvnc|Xorg' |grep -v grep | while read -r session
do 
    echo ${session} | awk '{
        display="N/A"
        size="N/A"
        depth="N/A"
        for(i=1; i<=NF; i++) {
            if ( match($i, "(Xorg|Xvnc)") ){
                gsub(".*\/", "", $i)
                type=$i
                i++
                gsub(":", "", $i)
                display=$i
            } else if(match($i, "geometry")){
                i++
                size=$i
            } else if(match($i, "depth")){
                i++
                depth=$i
            }
        }
        printf ":%s\t59%s\t%s\t%-10s%s\n", display, display, depth, size, type
    }';
done;