You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 24(X11)下启动ElectronJs应用报"Missing X server or $DISPLAY"的解决方法

Ubuntu 24.04 X11环境下Electron启动报错:Missing X server or $DISPLAY

问题场景

在Ubuntu 24.04.1 LTS(X11窗口系统)中启动极简Electron应用(仅main.js和index.html)时,触发错误:

[13117:1104/071616.998450:ERROR:ozone_platform_x11.cc(245)] Missing X server or $DISPLAY

此前曾遇到SUID沙箱错误,通过执行sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=1解决,不确定是否与当前问题关联。尝试在~/.bashrc末尾添加export DISPLAY=127.0.0.1:0.0设置环境变量,但未生效。

极简代码示例:

main.js

const { app, BrowserWindow } = require("electron");

const createWindow = () => {
    const win = new BrowserWindow({
        width: 800,
        height: 600,
    });

    win.loadFile("index.html");
};

app.whenReady().then(() => {
    createWindow();
});

index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Hello World!</title>
    </head>
    <body>
        <div>Hello World from Humans !</div>
    </body>
</html>

排查与解决步骤

1. 确认当前DISPLAY变量值

先在终端执行命令,查看会话中实际的DISPLAY值:

echo $DISPLAY

若输出为空或非:0/:0.0,说明环境变量未正确加载。

2. 临时加载正确的DISPLAY变量

  • 本地终端环境:直接执行以下命令,再重启Electron:
    export DISPLAY=:0
    
  • SSH远程连接场景:
    1. 连接时开启X11转发:ssh -X user@your-server-ip
    2. 确认远程服务器/etc/ssh/sshd_config中已开启X11Forwarding yes

3. 排查沙箱与X11的冲突

之前修改的内核参数可能影响沙箱权限,尝试临时关闭沙箱启动Electron:

electron . --no-sandbox

若能正常启动,说明沙箱配置与X11环境冲突,可选择:

  • 开发环境保留--no-sandbox参数(生产环境不推荐)
  • 修复SUID沙箱权限:找到Electron安装目录下的chrome-sandbox文件,执行:
    sudo chown root:root /path/to/electron-chrome-sandbox
    sudo chmod 4755 /path/to/electron-chrome-sandbox
    
    注:可通过which electron定位Electron安装路径,再查找chrome-sandbox文件。

4. 检查X11服务状态

执行命令确认X11显示管理器是否正常运行:

systemctl status display-manager

若服务未运行,启动服务:

sudo systemctl start display-manager

5. 验证X11基础功能

运行简单X11程序测试环境是否正常:

xeyes

若xeyes无法运行,说明X11环境本身存在问题,需先修复系统X11配置。

内容的提问来源于stack exchange,提问作者Alexandre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 15:00:04