先查看是否安装了桌面系统
dpkg -l | grep -E "gnome-shell|ubuntu-desktop"
这个结果就表明默认装过
ii ubuntu-desktop 1.450.2 amd64 The Ubuntu desktop system
查看是否安装了vnc
dpkg -l | grep -i vnc
ii libvnc client1:amd64 0.9.12+dfsg-9ubuntu0.3 amd64 API to write one's own VNC server - client library
ii libvnc server1:amd64 0.9.12+dfsg-9ubuntu0.3 amd64 API to write one's own VNC server
ii remmina-plugin-vnc :amd64 1.4.25+dfsg-0ubuntu0.20.04.1 amd64 VNC plugin for Remmina
ii vino 3.22.0-5ubuntu2.2 amd64 VNC server for GNOME
ii x11vnc 0.9.16-3 amd64 VNC server to allow remote access to an existing X session
不能用vino vino 是默认的 问题很大 会黑屏
查看vino进程是否运行
ps aux | grep vino-server
builden+ 1830549 0.0 0.0 12132 652 pts/1 S+ 11:24 0:00 grep --color=auto vino-serve
有这个就没运行
如果运行了
就停止这个服务
# 终止所有旧服务进程
pkill -9 -f Xvfb
pkill -9 -f vino-server
pkill -9 -f gnome-session
pkill -9 -f gnome-shell
# 删除旧配置缓存
rm -rf /tmp/.X11-unix/X0
rm -rf ~/.cache/gnome-shell/
rm -rf ~/.vnc/ # 清理旧 VNC 配置
/usr/lib/xorg/Xorg.wrap: Only console users are allowed to run the X server xinit: giving up xinit: unable to connect to X server: Connection refused xinit: server error 无法获取指向控制台的文件描述符
这个需要安装
这个错误是 Ubuntu 纯命令行环境(无物理控制台 / 显示器)启动 X 桌面的权限限制 ------Xorg 默认只允许 "控制台用户"(直接通过物理键盘 / 显示器登录的用户)启动,而远程 SSH 登录的用户属于非控制台用户,会被权限拦截。
解决核心:用 xvfb(虚拟 X 服务器)绕开物理控制台依赖,无需真实显示器就能启动 GNOME 桌面和 vino,步骤如下:
一、安装虚拟 X 服务器依赖
先安装 xvfb(虚拟显示器)和 xauth(权限认证工具),解决 "无控制台" 问题:
bash
运行
sudo apt update && sudo apt install -y xvfb xauth
安装稳定依赖(仅需一次)
bash
运行
# 更新源 + 安装 x11vnc(VNC 服务)+ Xfce4(轻量桌面)+ 虚拟显示器
sudo apt update && sudo apt install -y x11vnc xvfb xauth xfce4 xfce4-goodies xfce4-terminal -f
创建一键启动脚本(start-vnc.sh,终身可用)
bash
运行
nano start-vnc.sh
粘贴以下内容(密码直接写明文,无需编码,简单易懂):
bash
bash
#!/bin/bash
# 精准清理:只杀当前用户启动的 vino/gnome 残留,不碰系统进程
sudo pkill -9 -f vino-server
pkill -u $USER -9 -f gnome-shell
pkill -u $USER -9 -f gnome-session
sudo pkill -9 -f Xvfb
sudo pkill -9 -f x11vnc
sudo pkill -9 -f xfce4-session
# 定义配置(不变)
VNC_PORT=5900
VNC_PASSWORD="dlxx1234"
DISPLAY_NUM=:0
SCREEN_RES="1920x1080x24"
# 启动虚拟显示器(不变)
Xvfb $DISPLAY_NUM -screen 0 $SCREEN_RES -nolisten tcp -ac -extension GLX &
sleep 5
# 配置环境变量(不变)
export DISPLAY=$DISPLAY_NUM
export XAUTHORITY=$HOME/.Xauthority
xauth add $DISPLAY_NUM . $(mcookie)
# 启动 Xfce4 桌面(不变)
startxfce4 &
sleep 8
# 🌟 修复语法:x11vnc 启动命令(同一行写全参数)
x11vnc -display $DISPLAY_NUM -rfbport $VNC_PORT -passwd $VNC_PASSWORD -forever -bg -quiet -noxdamage
# 验证状态(不变)
echo "=== 🔥 VNC 服务启动成功! ==="
echo "连接地址:$(hostname -I | awk '{print $1}'):$VNC_PORT"
echo "VNC 密码:$VNC_PASSWORD"
echo "安全类型:2(VNC 认证)"
echo -e "\n=== 进程状态 ==="
ps aux | grep -E "Xvfb|x11vnc|xfce4-session" | grep -v grep
echo -e "\n=== 端口监听 ==="
ss -tuln | grep $VNC_PORT
运行
4. 赋予权限并启动(一键搞定)
bash
运行
# 赋予执行权限(仅需一次)
chmod +x start-vnc.sh
# 启动服务(无报错即成功)
./start-vnc.sh
启动成功标准(必看)
执行后输出类似以下内容,说明完全就绪:
plaintext
=== 🔥 VNC 服务启动成功! ===
连接地址:192.168.6.26:5900
VNC 密码:dlxx1234
安全类型:2(VNC 认证)
=== 进程状态 ===
ubuntu 1850000 0.2 0.1 581180 20008 ? Sl 11:00 0:00 Xvfb :0 -screen 0 1920x1080x24 -nolisten tcp -ac -extension GLX
ubuntu 1850050 0.5 0.3 401356 52428 ? Sl 11:00 0:00 x11vnc -display :0 -rfbport 5900 -passwd dlxx1234 -forever -bg -quiet -noxdamage
ubuntu 1850100 0.8 0.5 681356 82428 ? Sl 11:00 0:01 xfce4-session
=== 端口监听 ===
tcp LISTEN 0 5 0.0.0.0:5900 0.0.0.0:*
tcp LISTEN 0 5 [::]:5900 [::]:*
出现 A display manager is a program that provides graphical login capabilities for the X Window System. │ │ │ │ Only one display manager can manage a given X server, but multiple display manager packages are installed. Please select which display manager should run by default. │ │ │ │ Multiple display managers can run simultaneously if they are configured to manage different servers; to achieve this, configure the display managers accordingly, edit each of their init scripts in │ │ /etc/init.d, and disable the check for a default display manager. │ │ │ │ Default display manager gdm3和 lightdm 选哪个
优先选 lightdm
额外优化:设置开机自启(一劳永逸)
bash
运行
# 1. 编辑 crontab 自启配置
crontab -e
# 2. 粘贴以下内容(替换为你的脚本路径,比如 /home/ubuntu/start-vnc.sh)
@reboot sleep 30 && /home/你的用户名/start-vnc.sh >> /home/你的用户名/vnc-log.log 2>&1
# 3. 保存退出(Ctrl+O → 回车 → Ctrl+X)
sleep 30:等待系统完全启动后再运行 VNC;vnc-log.log:自启日志,方便后续排查(可选