在 Linux 文字介面或 SSH 連線中執行 xhost + 時,常遇到 unable to open display 錯誤。根本原因是 X Server 未啟動、DISPLAY 環境變數未正確設定、或 X11 Forwarding 未開啟。本文提供三種經驗證可行的解決方案。
適用於無實體螢幕的伺服器環境,透過 VNC 建立虛擬 X Server。
[root@localhost ~]# vncserver
New 'localhost:1 (root)' desktop is localhost:1
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/localhost:1.log
export DISPLAY=localhost:1
[root@localhost ~]# xhost +
access control disabled, clients can connect from any host

此法解決 90% 以上無圖形介面環境下的 xhost 需求 (如 Oracle 安裝前置作業)。
適用於已有圖形介面 (GNOME/KDE) 運行的本機或已開啟 X11 Forwarding 的 SSH 連線。
xdpyinfo | grep "name of display"
# 輸出範例:name of display: 127.0.0.1:1.0
export DISPLAY=:1.0 # 或 export DISPLAY=127.0.0.1:1.0
xhost +
# 成功顯示:access control disabled, clients can connect from any host
若 xhost + 仍失敗,嘗試 export DISPLAY=:0 (第 0 號顯示器) 再試。

name of display: 127.0.0.1:1.0su - oracleexport DISPLAY=127.0.0.1:1.0 (或 :1.0)xdpyinfo 能輸出資訊即成功適用於從 Windows 透過 SSH 連線 Linux,並需在遠端執行圖形化安裝程式 (如 Oracle Universal Installer)。
DISPLAY=localhost:10.0 等。xhost + 或圖形化安裝程式。






/etc/ssh/sshd_config 需確認 X11Forwarding yes 且 X11UseLocalhost no (視情況),修改後 service sshd restart。xauth (yum install xauth),否則 X11 Forwarding 會失效。DISPLAY,建議每次新增終端機重新匯出。| 場景 | 推薦方案 | 關鍵指令 |
|---|---|---|
| 無圖形介面伺服器 (Oracle 安裝等) | VNC Server | vncserver → export DISPLAY=localhost:1 → xhost + |
| 本機有桌面 / SSH 已開 X11 Forwarding | 查詢現有 Display | xdpyinfo → export DISPLAY=:n.m → xhost + |
| Windows 客戶端需遠端圖形化操作 | Xshell + Xmanager/VcXsrv | 開啟 SSH X11 Forwarding → 直接執行圖形程式 |
依據實際環境選擇對應方案,即可徹底解決 xhost: unable to open display 問題。