Skip to content

Instantly share code, notes, and snippets.

@888iee
Created August 29, 2024 15:19
Show Gist options
  • Save 888iee/ff27a054a69e1a939d6bcc81daa3ea52 to your computer and use it in GitHub Desktop.
Save 888iee/ff27a054a69e1a939d6bcc81daa3ea52 to your computer and use it in GitHub Desktop.
How to create a virtual diplay on X11

This works only for nvidia cards running on x11!

I'm not responsible for any damage this might cause. Do at your own risk

Sometimes you will only have the console accessible, therefore open this document on another device. I tested this with the following versions.

Software Version
Debian 12
Linux kernel 6.9.10
X server 1.21.1.7

Uninstall nvidia drivers

We will install the latest drivers from nvidia but first we need to uninstall previously installed nvidia drivers. Nouveau drivers will be dealt with when running the nvidia installer.

sudo apt-get --purge remove nvidia*

Get latest driver

You will download and run the latest driver installer package from nvidia. You will get asked if you want to block nouveau drivers, which you should accept (This can cause trouble later) and you will be asked if you want to register the kernel module with dkms which I highly recommend. This will automatically rebuild the registered kernel modules when upgrading your kernel. If the install process is aborted, it means that there are either still nvidia drivers installed or it couldn't register with dkms. You will need to cover those first. As for the dkms issue I have no solution. If it want you to sign the kernel module, refer to the documentation (readme in the download section of the installer).

When you reading this at a later stage check this nvidia drivers download section, choose the last version (at the bottom). You can just click the first *.run file to download it. There's also a readme file which I recommend to read if you get into any troubles.

To run this you mustn't have a X-Server running, so either you boot into Rescue Mode or you disable it with systemd.

wget https://download.nvidia.com/XFree86/Linux-x86_64/560.35.03/NVIDIA-Linux-x86_64-560.35.03.run
sudo systemctl isolate multi-user.target
sudo sh NVIDIA-Linux-x86_64-560.35.03.run

Restore X-Server

sudo systemctl isolate graphical.target
sudo systemctl reboot

Create the virtual display

If you don't have a xorg-config at /etc/X11/xorg.conf, run nvidia-xconfig to create one.

Backup xorg config

cd ~
mkdir Backups
cp /etc/X11/xorg.conf ~/Backups/xorg.conf

Decide which port to use

Now you need to decide which unused port on your graphics card you want to use. Which one doesn't matter really but it needs to be unused. Run xrandr to get similar output to this:

Screen 0: minimum 8 x 8, current 7040 x 1440, maximum 32767 x 32767
DP-0 disconnected (normal left inverted right x axis y axis)
DP-1 disconnected (normal left inverted right x axis y axis)
HDMI-0 connected 2560x1440+0+0 (normal left inverted right x axis y axis) 597mm x 336mm
   2560x1440     59.95*+
   2048x1080     60.00    24.00  
   1920x1080     60.00    59.94    50.00  
   1600x1200     60.00  
   1280x1024     75.02    60.02  
   1280x720      60.00    59.94    50.00  
   1152x864      75.00  
   1024x768      75.03    60.00  
   800x600       75.00    60.32  
   720x576       50.00  
   720x480       59.94  
   640x480       75.00    59.94    59.93  
DP-2 connected primary 2560x1440+2560+0 (normal left inverted right x axis y axis) 698mm x 393mm
   2560x1440    144.00*+ 120.00    59.95  
   1920x1080    143.98   119.88    60.00    59.94    50.00  
   1680x1050     59.95  
   1600x900      60.00  
   1440x900      59.89  
   1280x1024     75.02    60.02  
   1280x800      59.81  
   1280x720      60.00    59.94    50.00  
   1152x864      75.00  
   1024x768      75.03    70.07    60.00  
   800x600       75.00    72.19    60.32    56.25  
   720x576       50.00  
   720x480       59.94  
   640x480       75.00    72.81    59.94  
DP-3 disconnected (normal left inverted right x axis y axis)
DP-4 disconnected (normal left inverted right x axis y axis)
DP-5 disconnected (normal left inverted right x axis y axis)
USB-C-0 disconnected (normal left inverted right x axis y axis)

I chose USB-C-0 because I don't have any usb-c monitors (yet). You may also need the names of your connected display.

Add virtual display

You need to add your virtual display to the xorg.conf. Open the file with sudo nano /etc/X11/xorg.conf and search for Section "Screen". Inside this section you need to add the Options below but with your parameters. In MetaMode you need to add the display name (e.g. DP-2), its resolution and where the monitor should start.

For whatever reason (I knew it at some point) the monitors are rendered from top to bottom and left to right, like an upside down grid. This means the first monitor (with a res of 2560x1440) will start at +0+0 and the second (with a res of 1920x1080) at +2560+0 In ConnectedMonitor you need to add all your monitors, physical and virtual, if not you won't see anything!

    Option         "MetaMode" "DP-2: 2560x1440_144 +0+0, HDMI-0: 2560x1440_60 +2560+0, USB-C-0: +5120+0"
    Option         "ConnectedMonitor" "DP-2, HDMI-0, USB-C-0"
    Option         "UseEDIDFreqs" "False"
    Option         "ModeValidation" "NoDFPNativeResolutionCheck,NoVirtualSizeCheck,NoMaxPClkCheck,NoHorizSyncCheck,NoVertRefreshCheck,NoWidthAlignmentCheck AllowNonEdidModes"

To apply this config you will restart the display-manager. But be aware if you have a black screen, you need to reset the config. you might be able to switch to terminal mode (ctrl+alt+f2), if not use the rescue mode like before.

sudo systemctl restart display-manager

Now you should be able to run xrandr --listmonitors and see your physical and virtual monitors. Right click your desktop and click configure display settings. Try changing the resolution of your virtual display to your liking. If this doesn't work, you may proceed with 'Output configuration'

Output configuration

You need to generate resolution parameters, which I don't exactly know what they do. I suppose they normally come from the connected monitor. The command below will generate those and add them to xrandr. Here you can choose the resolution and Hz of your virtual display (although I didn't test anything else than 60Hz). To adjust just change the 3 numbers after cvt.

xrandr --newmode $(cvt 1920 1080 60 | sed -n '2 p' | cut -d ' ' -f 2-)
# xrandr --newmode $(cvt <my-custom-resolution+Hz> | sed -n '2 p' | cut -d ' ' -f 2-)
xrandr --addmode <display connector/USB-C-0> "<my-custom-resolution+Hz>"
xrandr --output  <display connector/USB-C-0> --mode "<my-custom-resolution+Hz>"

Sunshine configuration

Almost there! When starting sunshine read its logfile and search for "Detecting monitors". Remember the number of the virtual monitors. Open the sunshine web page -> Configuration -> Audio/Video and enter the number under Monitor number. Now only the virtual monitor will be streamed even when you have the other on. Go to General and click on Add below Command Preparations. config.do_cmd

xrandr --output <virtual monitor> --primary

config.undo_cmd

xrandr --output <main monitor> --primary

This will set the primary monitor to your virtual one when you connect via moonlight and undo it. Now games should also open on your virtual monitor when streaming and on your main display when playing directly on your computer.

I also added another command xrandr --output USB-C-0 --mode 1920x1080 because for some reason, the resolution of my virtual display reset sometimes

Sources

https://download.nvidia.com/XFree86/Linux-x86_64/560.35.03/README/

https://www.linuxtechi.com/boot-debian-rescue-emergency-mode/

https://debianforum.de/forum/viewtopic.php?t=173006

dianariyanto/virtual-display-linux#9

https://unix.stackexchange.com/questions/559918/how-to-add-virtual-monitor-with-nvidia-proprietary-driver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment