VNC server - set limit to one session per user

Posted by Pavlo Khmel on Sun 04 February 2024

This example from Rocky Linux 9.

VNC server package: tigervnc-server

Modify /usr/bin/vncserver. Add lines after "# Now start the X VNC Server" and after "# Functions"

. . .
. . .

# Now start the X VNC Server

# Maximum session limit to 1 per user.
&vnclimit();

. . .
. . .

###############################################################################
# Functions
###############################################################################

sub vnclimit {
$countoutput = `ps -u $ENV{USER} | grep -i Xvnc | wc -l`;
if ($countoutput >= 1) {
        print "You are already running a VNC session for $ENV{USER} user. Maximum session is limited to 1 per user!\n";
        print "Run 'vncserver -list' to list the current session\n\n";
        print "Please contact System Administrator for more details.\n";
        exit;
        }
}

. . .
. . .