Knowledge Base
Searching in : Article
ID: AR12F00519
Applies to: NoMachine Server
Added on: 2008-12-09
Last Update: 2016-08-10

Automatically locking the screen-saver when disconnecting from a session

This article explains how to lock the screen-saver in an automatic way when the user disconnects his/her NoMachine session.  This requires to set-up a script for locking the screen on the system and configure NoMachine to execute it. 

In some environments, it may be a company's policy to request that each user locks down his/her own screen-saver when leaving the workplace. In case of a NoMachine session, hints below can be used to lock the screen automatically when the user has disconnected his/her session.

 

FOR CONNECTIONS TO THE PHYSICAL DESKTOP

The following script to enable the GNOME lock screen has been written and tested for Ubuntu 15.10 and may require some adjustments to fit other Linux distributions or versions.

1) Create a new file called "screen_lock.sh" in /usr/local/bin/ containing the following:

#!/bin/sh

sessionID=$1
directory="$NXDIR/var/log/node"

sessionPath=$(find "$directory" -regextype posix-egrep -type d \
            -regex "$directory/C-.*$sessionID" 2>/dev/null)
displayNumber=$(grep "shadow=" $sessionPath/options | sed 's/.*shadow=[^:]*:\([^/]*\)\/:.*/\1/')
DISPLAY=":$displayNumber" /usr/bin/gnome-screensaver-command --lock 

exit 0

2) Set permissions:

# chown root:root /usr/local/bin/screen_lock.sh
# chmod 0755 /usr/local/bin/screen_lock.sh
 

3) Uncomment and modify the following key in /usr/NX/etc/node.cfg

UserScriptAfterSessionClose  "/usr/local/bin/screen_lock.sh"
 

This ensures that the GNOME screen will be locked when the user disconnects from the physical desktop.

 


FOR VIRTUAL DESKTOP SESSIONS


1) Create a new file called "screen_lock.sh" in /usr/local/bin/. 

A general content for the custom script to be executed for locking the GNOME screen is:

#!/bin/bash

DISPLAY=":$4" /usr/bin/gnome-screensaver-command --lock

exit 0

 

You may have to modify the command that kicks off the screensaver if you are using something other than gnome.

I.e. for:

(i) KDE4 and KDE5

DISPLAY=":$4" qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock

(ii) KDE3:

DISPLAY=":$4" dcop kdesktop KScreensaverIface lock

 

2) Set permissions:

# chown root:root /usr/local/bin/screen_lock.sh
# chmod 0755 /usr/local/bin/screen_lock.sh


3) For NoMachine 4 or later, uncomment and modify the following keys in /usr/NX/etc/node.cfg:

UserScriptAfterSessionDisconnect "/usr/local/bin/screen_lock.sh"

This ensures that the GNOME screen will be locked when the virtual desktop is disconnected.

 

Example for 1)

The following script to enable the GNOME lock screen has been written and tested for Ubuntu 12.04 and may require some adjustments to fit other Linux distributions or versions.

#!/bin/sh

HOME="/home/$2" 
DISPLAY="$4" 
machineId=`cat /var/lib/dbus/machine-id` 
file="$HOME/.dbus/session-bus/$machineId-$DISPLAY" 

if [ -f "$file" ] ;  then   

  line=`grep "^DBUS_SESSION_BUS_ADDRESS" "$file"`   
  export $line   

  /usr/bin/gnome-screensaver-command --lock  

fi 

exit 0

 



NOTES for NX 3.5.0 to lock screen in case of virtual sessions:

i) Content for the screen_lock.sh script is instead (note that the display parameter is $3 and not $4 as for NoMachine v. 4 and 5 or later):

#!/bin/bash
DISPLAY=":$3" /usr/bin/gnome-screensaver-command --lock
exit 0


ii) The key to be used in node.cfg is:
UserScriptAfterSessionSuspend = "/usr/local/bin/screen_lock.sh"