はじめに
本記事は、ヘッドレス環境でGPUを利用したいユーザーを対象とする。
Ubuntuで、NVIDIA GPUを利用時の仮想ディスプレイの設定方法を紹介する。
物理ディスプレイが接続されていない環境では、NVIDIA GPU上でXサーバーが起動せず、OpenGLやCUDAの一部機能が利用できない場合がある。
本記事では、ヘッドレス環境でもGPUを利用可能にするための仮想ディスプレイ設定方法を紹介する。VNCやDocker環境内で仮想ディスプレイを使いたい場合に有用である。
動作確認環境
- Ubuntu 22.04 x86_64
- NVIDIA GPU
- Driver Version: 550.144.03 / 555.58.02
- X11(GDM)
事前設定
本設定はX11環境を前提としている。
Waylandではxorg.confが使用されないため、/etc/gdm3/custom.confでWaylandを無効化する必要がある。
Waylandを無効化する。
sudo vi /etc/gdm3/custom.conf
以下の項目のコメントを外す。
# WaylandEnable=false
WaylandEnable=false
※変更後は再ログインまたは再起動が必要。
方法
単一GPUの場合と複数GPUの場合について紹介する。まずは、単純な単一GPUの場合について紹介し、その後、複数GPUの場合を紹介する。
単一GPUの場合
X11の設定ファイル(/etc/X11/xorg.conf)を修正する。
ファイルが存在しない場合は以下のコマンドを実行しファイルを作成する。
※既存のxorg.confがある場合は上書きされるため注意
sudo nvidia-xconfig -a
ファイルのバックアップを取っておく。
sudo cp /etc/X11/xorg.conf /etc/X11/xorg.conf.back
GPUのバスIDを確認する。
nvidia-xconfig --query-gpu-info
以下のような結果が出る。
Number of GPUs: 2
GPU #0:
Name : NVIDIA RTX A6000
UUID : GPU-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
PCI BusID : PCI:71:0:0
Number of Display Devices: 0
GPU #1:
Name : NVIDIA RTX A6000
UUID : GPU-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
PCI BusID : PCI:163:0:0
Number of Display Devices: 0
利用したいGPUのBusIDをメモしておく。
ファイルを修正する。
sudo vi /etc/X11/xorg.conf
Section "ServerLayout"
Identifier "Layout0"
Screen 0 "Screen0"
InputDevice "Keyboard0" "CoreKeyboard"
InputDevice "Mouse0" "CorePointer"
EndSection
Section "Files"
EndSection
Section "InputDevice"
# generated from default
Identifier "Mouse0"
Driver "mouse"
Option "Protocol" "auto"
Option "Device" "/dev/psaux"
Option "Emulate3Buttons" "no"
Option "ZAxisMapping" "4 5"
EndSection
Section "InputDevice"
# generated from default
Identifier "Keyboard0"
Driver "kbd"
EndSection
Section "Monitor"
Identifier "Monitor0"
VendorName "Unknown"
ModelName "Unknown"
HorizSync 28.0 - 80.0
VertRefresh 48.0 - 75.0
Option "DPMS"
EndSection
Section "Device"
Identifier "Device0"
Driver "nvidia"
VendorName "NVIDIA Corporation"
BusID "PCI:xx:x:x"
EndSection
Section "Screen"
Identifier "Screen0"
Device "Device0"
Monitor "Monitor0"
DefaultDepth 24
Option "UseDisplayDevice" "DP-0"
Option "ConnectedMonitor" "DP-0"
Option "HardDPMS" "False"
Option "AllowEmptyInitialConfiguration" "True"
SubSection "Display"
Depth 24
Virtual 1920 1080
EndSubSection
EndSection
- Section "Device"
- BusID "PCI:xx:0:0"
- GPUのBusIDを指定する(例:PCI:71:0:0)
- BusID "PCI:xx:0:0"
- Section "Screen"
- Option
- "UseDisplayDevice" / "ConnectedMonitor"
- DP-0はGPUの出力ポート名(仮想的に使用される場合もある)
- "HardDPMS" "False"
- Headlessでパフォーマンスを改善するためのオプション
- "AllowEmptyInitialConfiguration" "True"
- ディスプレイ未接続でも X を起動できるようにする設定
- "UseDisplayDevice" / "ConnectedMonitor"
- SubSection "Display"
- Virtual 1920 1080
- 仮想ディスプレイの解像度(幅 高さ)
- ※Virtualを指定しない場合、解像度が正しく設定されないことがある
- Virtual 1920 1080
- Option
X11(GDM)を再起動する。
sudo systemctl restart gdm
X11の状態を確認する。
sudo systemctl status gdm
以下のようにActive: active (running)となっていればOK。
● gdm.service - GNOME Display Manager
Loaded: loaded (/lib/systemd/system/gdm.service; static)
Active: active (running) since Sun 2026-04-19 23:44:00 JST; 1 week 2 days ago
Main PID: 2637105 (gdm3)
Tasks: 3 (limit: 154036)
Memory: 4.0M
CPU: 279ms
CGroup: /system.slice/gdm.service
└─2637105 /usr/sbin/gdm3
X11が起動しているか確認する。
nvidia-smi
Processesに、指定したGPUでXサーバー(/usr/lib/xorg/Xorg)が起動したら成功。
+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| 0 N/A N/A 1139475 G /usr/lib/xorg/Xorg 4MiB |
複数GPUの場合
複数GPUが存在し、各GPUに仮想ディスプレイを割り当てる方法を紹介する。ここでは、同一のXサーバー上で、2枚のGPUそれぞれにDevice、Monitor、Screenを定義し、各GPUに仮想ディスプレイを割り当てる方法を紹介する。
単一GPUの場合と同様に、GPUのIDを確認し、X11の設定ファイルを作成および修正する。
簡単にするため、2枚のGPUの場合の例を以下に示す。
単一GPUとの違いは以下となる。
- Monitor を複数定義する
- Device を複数定義する
- Screen を複数定義する
- ServerLayout で Screen を並べる
Section "ServerLayout"
Identifier "Layout0"
Screen 0 "Screen0" 0 0
Screen 1 "Screen1" RightOf "Screen0"
InputDevice "Keyboard0" "CoreKeyboard"
InputDevice "Mouse0" "CorePointer"
EndSection
Section "Files"
EndSection
Section "InputDevice"
# generated from default
Identifier "Mouse0"
Driver "mouse"
Option "Protocol" "auto"
Option "Device" "/dev/psaux"
Option "Emulate3Buttons" "no"
Option "ZAxisMapping" "4 5"
EndSection
Section "InputDevice"
# generated from default
Identifier "Keyboard0"
Driver "kbd"
EndSection
Section "Monitor"
Identifier "Monitor0"
VendorName "Unknown"
ModelName "Unknown"
HorizSync 28.0 - 80.0
VertRefresh 48.0 - 75.0
Option "DPMS"
EndSection
Section "Monitor"
Identifier "Monitor1"
VendorName "Unknown"
ModelName "Unknown"
Option "DPMS"
EndSection
Section "Device"
Identifier "Device0"
Driver "nvidia"
VendorName "NVIDIA Corporation"
BusID "PCI:xx:0:0"
EndSection
Section "Device"
Identifier "Device1"
Driver "nvidia"
VendorName "NVIDIA Corporation"
BusID "PCI:yy:0:0"
EndSection
Section "Screen"
Identifier "Screen0"
Device "Device0"
Monitor "Monitor0"
DefaultDepth 24
Option "UseDisplayDevice" "DP-0"
Option "ConnectedMonitor" "DP-0"
Option "HardDPMS" "False"
Option "AllowEmptyInitialConfiguration" "True"
SubSection "Display"
Depth 24
Virtual 1920 1080
EndSubSection
EndSection
Section "Screen"
Identifier "Screen1"
Device "Device1"
Monitor "Monitor1"
DefaultDepth 24
Option "UseDisplayDevice" "DP-0"
Option "ConnectedMonitor" "DP-0"
Option "HardDPMS" "False"
Option "AllowEmptyInitialConfiguration" "True"
SubSection "Display"
Depth 24
Virtual 1920 1080
EndSubSection
EndSection
- Section "Device"
- 2枚のGPU IDを確認し、それぞれ別のDeviceとして設定する
- Section "Screen"
- 2枚の"Screen"を定義して、
Deviceに各GPUの"Device"名("Device 0"、"Device 1")を設定する - DP-0はGPUごとに存在するため、複数GPUでも同じDP-0を指定して問題ない
- 2枚の"Screen"を定義して、
X11(GDM)を再起動する。
sudo systemctl restart gdm
X11の状態を確認する。
sudo systemctl status gdm
以下のようにActive: active (running)となっていればOK。
● gdm.service - GNOME Display Manager
Loaded: loaded (/lib/systemd/system/gdm.service; static)
Active: active (running) since Sun 2026-04-19 23:44:00 JST; 1 week 2 days ago
Main PID: 2637105 (gdm3)
Tasks: 3 (limit: 154036)
Memory: 4.0M
CPU: 279ms
CGroup: /system.slice/gdm.service
└─2637105 /usr/sbin/gdm3
X11が起動しているか確認する。
nvidia-smi
Processesに、指定したGPUでXサーバー(/usr/lib/xorg/Xorg)が起動したら成功。
+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| 0 N/A N/A 2637141 G /usr/lib/xorg/Xorg 47MiB |
| 1 N/A N/A 2637141 G /usr/lib/xorg/Xorg 39MiB |
VNCなどで、DISPLAY環境変数を設定する場合、
- Screen0の場合
export DISPLAY=:0.0 - Screen1の場合
export DISPLAY=:0.1
となる。
よくある問題
-
Xorgが起動しない
→ /var/log/Xorg.0.log を確認 -
nvidia-smiにXorgが出ない
→ BusIDの設定ミスの可能性※BusIDは環境によって10進数/16進数の違いに注意する。うまくいかない場合は
lspciコマンドで確認する。 -
gdmが起動しない
→ Wayland設定を確認
まとめ
Ubuntuで、NVIDIA GPUを利用時の仮想ディスプレイの設定方法を紹介した。VNCやDocker環境内で仮想ディスプレイを使いたい場合に有用である。
参考