How to Attach NX Client to an Existing X Session on Ubuntu (NX4/NX3 Solutions)


2 views

Many sysadmins and developers face this scenario daily: You've locked your workstation at the office running Ubuntu with GNOME/KDE, then try to reconnect via NX from home - only to get a fresh session instead of your existing desktop. Here's why this happens and how to fix it.

The core issue stems from how NX handles X11 sessions. By default, NX creates new virtual displays (usually starting at :100) rather than attaching to existing ones. The technical workflow looks like:

# Typical NX session creation
/usr/NX/bin/nxserver --startup
Xvfb :100 -screen 0 1024x768x24 +extension GLX +extension RANDR

This method chains NX through an existing display:

# First install x11vnc if missing
sudo apt install x11vnc

# Run this on your work machine BEFORE leaving
x11vnc -display :0 -auth /var/run/lightdm/root/:0 -forever -shared -rfbauth ~/.vnc/passwd

Then modify your NX client connection:

nxclient --proxy=localhost:5900 --link=lan

For newer NX4 installations:

# Edit /usr/NX/etc/node.cfg
EnableSessionShadowing = "1"
ShadowSessionAuth = "password"

# Then connect using
nxclient --shadow --geometry=1920x1080 --session=:0

If you encounter authentication problems:

# Check Xauthority permissions
chmod 600 ~/.Xauthority
chown $USER:$USER ~/.Xauthority

# Verify display manager settings
grep -i "display-setup-script" /etc/lightdm/lightdm.conf

When attaching to existing sessions, these NX parameters help:

nxclient --cache=16M --shmem=1 --backingstore=1 \
         --image=/usr/NX/share/images/default.jpg \
         --autodpi=1

Many Linux sysadmins face this workflow dilemma: You lock your GNOME/KDE session at the office workstation (say, Ubuntu 22.04 with GDM), then attempt to reconnect via NX from home only to spawn a new X session. This creates two major pain points:

  • Process duplication (Chrome, IDE instances running in both sessions)
  • Resource contention (GPU memory, DB connections)

By default, FreeNX/Nomachine servers create new virtual X sessions because:


# Default NX node configuration
/etc/nxserver/node.conf

# Key parameters affecting session attachment
EnableSessionShadowing = "0"
ShadowSessionPolicy = "new"

To modify this behavior for GDM/LightDM displays:

  1. Configure NX for shadowing:
    
    sudo sed -i 's/EnableSessionShadowing = "0"/EnableSessionShadowing = "1"/' /etc/nxserver/node.conf
    sudo sed -i 's/ShadowSessionPolicy = "new"/ShadowSessionPolicy = "reconnect"/' /etc/nxserver/node.conf
    
  2. Identify target display:
    
    # List active X sessions
    loginctl list-sessions
    # Sample output:
    # SESSION  UID USER   SEAT  TTY 
    #     2 1001 john   seat0 tty2
    

When initiating NX connection from client:


nxclient --attach :0  # For direct X display attachment
nxclient --sessionid=2  # Using systemd-logind session ID

For Xorg servers (not Wayland), ensure proper ACLs:


# Add NX user to X access control list
xhost +SI:localuser:nx

Modern displays may require additional D-Bus permissions in /usr/share/dbus-1/session.conf:


<policy context="default">
  <allow own="org.gnome.ScreenSaver"/>
  <allow send_destination="org.gnome.ScreenSaver"/>
</policy>