The Problem
When managing multiple users on a NICE DCV server, creating virtual sessions manually for every user can become repetitive.
I wanted to automate this process so that whenever a user connects to the DCV server:
- If the user already has a DCV session, they should connect to it.
- If the user doesn’t have a session, DCV should automatically create one.
- Users should always connect to their own sessions, not someone else’s.
The idea was similar to using pam_mkhomedir with Samba AD, where a user’s home directory is automatically created during their first login.
The Solution
I used PAM’s pam_exec.so module to execute a custom shell script during DCV authentication.
First, I created a script at:
/tools/dcv/pam-auto-session
Script
#!/bin/bash
user="$PAM_USER"
[ -z "$user" ] && exit 0
if ! /usr/bin/dcv list-sessions | grep -q "owner:$user"; then
session_id="$user"
/usr/bin/dcv create-session \
--type virtual \
--owner "$user" \
--user "$user" \
--init /usr/bin/mate-session \
"$session_id"
fi
exit 0
The script checks the existing DCV sessions for the authenticated username. If no session exists, it creates a new virtual session using that username as the session ID.
PAM Configuration
I added the following entry to the DCV PAM configuration:
auth required pam_exec.so seteuid /tools/dcv/pam-auto-session
I initially tried using the session management group, but the session wasn’t created early enough to affect DCV’s connection behaviour.
Using the auth group allowed the script to execute early enough for session creation.
One important consideration is that the script must execute only after successful authentication. Otherwise, even an incorrect password attempt could trigger session creation.
Architecture
The intended workflow:
User connects to NICE DCV
|
v
PAM Authentication
|
v
Authentication successful?
|
v
Execute PAM Script
|
v
Get authenticated username
|
v
Check DCV sessions
|
+----+----+
| |
Exists? No
| |
| v
| Create virtual
| session
| |
+----+----+
|
v
Connect to user's
own session
The goal is to eliminate manual DCV session creation and provide an automatic, per-user virtual session whenever someone connects to the server.