Quick and dirty script to set up a terminal server with xfce

Who needs proprietary OSes and CALs to use a terminal server? Not this guy!

Here’s the guidelines I use when standing up a new terminal server with Xfce, because Gnome operates too slowly and is bloated. This is my file named el7ts201.txt.

# How to install terminal server

sudo su -
sed -i -r -e '/10\.200/s/^[^#]/#/;' -e '/^\&/s/^/#/;' /etc/rsyslog.conf
systemctl restart rsyslog

# install epel
# reference: https://www.tecmint.com/how-to-enable-epel-repository-for-rhel-centos-6-5/
cd
wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install epel-release-latest-7.noarch.rpm

# install xrdp
yum -y groupinstall xfce ; yum -y install xrdp tigervnc-server patch xfce4-whiskermenu-plugin ; yum -y remove gnome-session

# install fonts
yum -y install gnu-free-*-fonts open-sans-fonts libXfont xorg-x11-font*

tf=/usr/lib/firewalld/services/xrdp.xml
touch "${tf}" ; chmod 0644 "${tf}" ; restorecon "${tf}" ; chown root.root "${tf}"
cat <<'EOF' 1> "${tf}"
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>ms-wbt</short>
  <description>Terminal services</description>
  <port protocol="tcp" port="3389"/>
</service>
EOF
firewall-cmd --reload
firewall-cmd --add-service=xrdp --permanent
firewall-cmd --reload

##############################################
# add XFCE to the /etc/X11/xinit/Xclients file
tf=/etc/X11/xinit/Xclients
cp -p "${tf}" "${tf}.$( date "+%s" )"
cat <<'EOFXCLIENTS' 1> "${tf}"
#!/bin/bash

STARTXFCE="$(type -p startxfce4)"

# check to see if the user has a preferred desktop
PREFERRED=XFCE
exec "$STARTXFCE"

# We should also support /etc/X11/xinit/Xclients.d scripts
XCLIENTS_D=/etc/X11/xinit/Xclients.d
if [ "$#" -eq 1 ] && [ -x "$XCLIENTS_D/Xclients.$1.sh" ]; then
    exec -l $SHELL -c "$SSH_AGENT $XCLIENTS_D/Xclients.$1.sh"
fi

# Failsafe.

# these files are left sitting around by TheNextLevel.
rm -f $HOME/Xrootenv.0
EOFXCLIENTS

###############################################
systemctl enable xrdp xrdp-sesman
systemctl start xrdp xrdp-sesman

echo done

# Next steps
# apply valid ssl certificate

Advertisements

List current xvnc sessions in xrdp so you can reconnect to your old one

tl;dr

{ echo "user pid Xdisplay port"; { ps -ef | awk '/Xvnc :[[:digit:]]+/ {print $1,$2,$9}' | while read tu tpid tvnc; do sudo netstat -tlpn | awk -v "tpid=${tpid}" '$0 ~ tpid {print $4;}' | sed -r -e 's/^.*://;' -e "s/^/${tu} ${tpid} ${tvnc} /;" ; done ; } | sort -k3 ; } | column -c4 -t

The story

I connected to a gnome session on a terminal server, and disconnected. I wanted to reconnect to my current session, but apparently I got a new X session. After some research, I learned you can configure xrdp to prompt for the port number so you can get back to the previous session. However, then you have to know what to type in. After doing a manual ps and netstat, I found some useful numbers. What I needed to enter was the tcp port number, so 5919.

The explanation

You can have an entry in the /etc/xrdp/xrdp.ini file like the following block.

[xrdp8]
name=Reconnect
lib=libvnc.so
username=ask
password=ask
ip=127.0.0.1
port=ask

When you connect over RDP, select the “Reconnect” module and type in a port number, which you can find from the output of the oneliner.

{ echo "user pid Xdisplay port"; { ps -ef | awk '/Xvnc :[[:digit:]]+/ {print $1,$2,$9}' | while read tu tpid tvnc; do sudo netstat -tlpn | awk -v "tpid=${tpid}" '$0 ~ tpid {print $4;}' | sed -r -e 's/^.*://;' -e "s/^/${tu} ${tpid} ${tvnc} /;" ; done ; } | sort -k3 ; } | column -c4 -t
user       pid    Xdisplay  port
mjohnso    11448  :17       5917
mjohnso    12939  :18       5918
bgstack15  1219   :19       5919

References

Weblinks

  1. https://askubuntu.com/questions/133343/how-do-i-set-up-xrdp-session-that-reuses-an-existing-session#360835

Xfce on Xrdp on Fedora 26

Introduction

This post will demonstrate how to set up a Fedora installation to act as a terminal server that serves desktops over RDP. This will allow Windows computers to connect to it, as well as a GNU/Linux system running xfreerdp clients.
These instructions should be easy to follow on any RHEL-based distros, and easily adapted to others. The copy-paste segments have been tested on Fedora 26 but the accompanying instructions should guide you in deploying a terminal server on a broad range of GNU/Linux environments.

How to set up xrdp on Fedora

Ensure your desktop environment is installed. For CentOS 7 that would be something similar to yum groupinstall “Server with gui” or yum groupinstall xfce.
Install the components necessary for rdp and vnc. When the rdp server allows a user to connect, the user connects to a X windows session in VNC.

yum -y install xrdp tigervnc-server

Set up a firewall rule for the RDP port.

tf=/lib/firewalld/services/xrdp.xml; touch "${tf}"; chmod 0644 "${tf}"
cat <<EOF >"${tf}"
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>Remote Desktop Protocol (RDP)</short>
  <description>A RDP service that serves X desktop sessions. Using this allows a Windows client to connect using the built-in mstsc utility!</description>
  <port protocol="tcp" port="3389"/>
</service>
EOF
firewall-cmd --reload
firewall-cmd --permanent --add-service=xrdp
firewall-cmd --reload

Running xfce or any arbitrary DE

On Fedora, for xfce specifically you need to make sure that X clients are told to run xfce. The vnc services will try to start a window manager, but as of the time of this writing xfce is not included in the list, so it will fail out (or run gnome, if it’s present).
The file to inspect is /etc/X11/xinit/Xclients, or the per-user settings at ~/.Xclients. For the etc file, it should be fairly obvious how to add to it. Additionally, make sure the file is executable!

For Fedora 26 and xfce, you can inspect this patch file, and then either run the patch statement or make the changes yourself manually.

# for all users
tf=/etc/X11/xinit/Xclients.patch; touch "${tf}"; chmod 0644 "${tf}";
cat <<'EOFPATCH' >"${tf}"
--- /etc/X11/xinit/Xclients	2017-02-12 00:38:18.000000000 -0500
+++ /etc/X11/xinit/Xclients.new	2017-10-14 08:12:34.340524791 -0400
@@ -12,6 +12,7 @@
 MSESSION="$(type -p mate-session)"
 STARTKDE="$(type -p startkde)"
 STARTLXDE="$(type -p startlxde)"
+STARTXFCE="$(type -p startxfce4)"
 
 # check to see if the user has a preferred desktop
 PREFERRED=
@@ -25,6 +26,8 @@
 	PREFERRED="$STARTKDE"
     elif [ "$DESKTOP" = "LXDE" ]; then
 	PREFERRED="$STARTLXDE"
+    elif [ "$DESKTOP" = "XFCE" ]; then
+	PREFERRED="$STARTXFCE"
     fi
 fi
 
@@ -44,6 +47,9 @@
 elif [ -n "$STARTLXDE" ]; then
     # if neither GNOME nor KDE then LXDE
     exec "$STARTLXDE"
+elif [ -n "$STARTXFCE" ]; then
+    # if none of the above, try XFCE.
+    exec "$STARTXFCE"
 fi
 
 # We should also support /etc/X11/xinit/Xclients.d scripts
EOFPATCH
patch -p1 /etc/X11/xinit/Xclients < /etc/X11/xinit/Xclients.patch

And for the individual users (I have yet to test to see if the normal order of resolution of the X files lets this actually override it for the user):

tf=~/.Xclients; touch "${tf}"; chmod 0700 "${tf}";
cat <<EOF > "${tf}"
exec /usr/bin/startxfce4
EOF

And that’s it! A sample xfreerdp statement to connect to this service could be:

xfreerdp /sec-rdp /cert-tofu /size:1520x820 /bpp:16 /v:192.168.1.76 -z /disp /audio-mode:0 /wallpaper /themes /u:bgstack15
Screenshot of session manager login screen in vnc in xrdp
Login screen after connecting to xrdp service

References

Weblinks

  1. Using the patch utility https://docs.moodle.org/dev/How_to_create_a_patch#Creating_a_patch_using_diff
  2. update file /etc/X11/xinit/Xclients https://docs-old.fedoraproject.org/en-US/Fedora/13/html/Deployment_Guide/s1-x-runlevels.html
  3. https://forum.xfce.org/viewtopic.php?id=8261
  4. General xrdp guide https://hostpresto.com/community/tutorials/using-a-desktop-environment-on-a-centos-7-vps/
  5. Notes about cinnamon on CentOS 7 https://www.techbrown.com/install-cinnamon-2-6-on-fedora-22-centos-7-rhel-7.shtml
  6. xrdp on centos 7 http://idroot.net/tutorials/how-to-install-xrdp-on-centos-7/
  7. http://www.itzgeek.com/how-tos/linux/centos-how-tos/install-xrdp-on-centos-7-rhel-7.html