Ubuntu设置VNC远程桌面

安装SSH任务并启动

sudo apt update

sudo apt install openssh-server

sudo systemctl status ssh.service

确认是否开机自启动

sudo systemctl is-enabled ssh.service

安装vncserver

sudo apt install tigervnc-standalone-server tigervnc-xorg-extension

安装GNOME组件

sudo apt install -y gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal ubuntu-desktop

安装支持VNC与Windows之间复制粘贴

sudo apt install xclip autocutsel

以管理员编辑用户配置文件

sudo vim /etc/tigervnc/vncserver.users

将端口号与用户名对应,如用户andrew使用5902端口,用户lisa使用5903端口的配置如下:

:2=andrew

:3=lisa

给每个用户配置config文件

以为andrew配置为例,先使用用户andrew执行命令vncserver,再执行vncserver -kill :*关闭刚启动的VNC桌面,这样VNC的配置目录(~/.vnc)就建立好了。

接着新建文件/home/andrew/.vnc/config,内容如下:

session=ubuntu

geometry=1920x1080

securitytypes=vncauth,tlsvnc

上面启动的服务, 默认是只监听本地127.0.0.1, 所以从其它机器是无法连接的, 如果需要连接, 可以用这个命令启动

vncserver -localhost no

设置运行的桌面环境

sudo vim ~/.vnc/xstartup

填入以下内容:

#!/bin/sh

Start up the standard system desktop

unset SESSION_MANAGER

unset DBUS_SESSION_BUS_ADDRESS

/usr/bin/gnome-session

-x /etc/vnc/xstartup && exec /etc/vnc/xstartup

-r $HOME/.Xresources && xrdb $HOME/.Xresources

x-window-manager &

添加文件的可执行权限chmod +x xstartup

将 tigervncservice 服务添加到启动, :2根据自己的实例修改

sudo systemctl start tigervncserver@:2.service

sudo systemctl enable tigervncserver@:2.service

查看vnc实例:vncserver -list

另一种方法:

或者在~/.bash_profile中添加:

sudo vim ~/.bash_profile

vncserver :1 -geometry 1920x1080 -depth 24 -localhost no

其中~/.vnc/xstartup中的内容如下:

#!/bin/sh

Start up the standard system desktop

unset SESSION_MANAGER

unset DBUS_SESSION_BUS_ADDRESS

启动 GNOME 桌面环境 (替换原有内容)

/usr/bin/gnome-session &

启动 X Window 管理器(作为备用或辅助)

-x /etc/vnc/xstartup && exec /etc/vnc/xstartup

-r $HOME/.Xresources && xrdb $HOME/.Xresources

x-window-manager &

启动复制粘贴支持工具

autocutsel -fork

添加:sudo vim /etc/rc.local

启动 ps 用户的 VNC 服务器

-l 指定用户名

sudo -u ps /usr/bin/vncserver :1 -geometry 1920x1080 -depth 24 -localhost no &

或者,调用我们之前写的包装脚本

sudo -u ps /home/ps/start-vnc.sh &

exit 0

rc.local方法:

sudo vim /etc/rc.local

#!/bin/bash

这行是必须的,告诉脚本在出错时立即退出

set -e

在这里添加你想要在开机时执行的命令

例如:启动 ps 用户的 VNC 服务器

注意:必须使用 sudo -u username 来指定运行的用户

sudo -u ps /usr/bin/vncserver :1 -geometry 1920x1080 -depth 24 -localhost no &

脚本必须最后以 exit 0 结束

exit 0

sudo chmod +x /etc/rc.local

创建

sudo vim /etc/systemd/system/rc-local-fix.service

Unit

Description=/etc/rc.local Compatibility

Documentation=man:systemd-rc-local-generator(8)

ConditionFileIsExecutable=/etc/rc.local

After=network.target

Service

Type=forking

ExecStart=/etc/rc.local start

TimeoutSec=0

RemainAfterExit=yes

GuessMainPID=no

Install

WantedBy=multi-user.target

sudo systemctl daemon-reload

sudo systemctl enable rc-local-fix.service

sudo systemctl start rc-local-fix.service

sudo systemctl status rc-local-fix.service

可以再套一层:

sudo vim /usr/local/bin/start-ps-vnc.sh

#!/bin/bash

睡眠几秒,确保网络和系统服务在开机时已就绪 (可选,但推荐)

sleep 10

切换到 ps 用户,并启动一个 login shell (-l) 来执行后续的命令

-c 后面是要执行的命令字符串

首先 source 用户的 profile 来获取环境变量

然后执行 vncserver 命令

sudo -u ps bash -lc "source /home/ps/.profile; /usr/bin/vncserver :1 -geometry 1920x1080 -depth 24 -localhost no"

sudo chmod +x /usr/local/bin/start-ps-vnc.sh

修改/etc/rc.local文件,在文件中调用脚本:

调用包装脚本

/usr/local/bin/start-ps-vnc.sh &

一个标准的xstartup文件内容如下:

#!/bin/bash

unset SESSION_MANAGER

unset DBUS_SESSION_BUS_ADDRESS

设置 Ubuntu GNOME 环境变量

export XDG_CURRENT_DESKTOP=ubuntu:GNOME

export DESKTOP_SESSION=ubuntu

export GNOME_SHELL_SESSION_MODE=ubuntu

关闭错误输出(可选,避免日志冗余)

exec > ~/.vnc/startup.log 2>&1

取消 X 权限检查(部分系统需要)

xrdb $HOME/.Xresources

禁用屏幕保护和电源管理(避免黑屏)

xset s off

xset -dpms

xset s noblank

GNOME 桌面(需安装 gnome-session)

if -x /usr/bin/gnome-session ; then

exec gnome-session --session=ubuntu &

fi

autocutsel -fork

相关推荐
阿里云大数据AI技术1 小时前
阿里云 EMR AI 助手正式发布:从问答工具到全栈智能运维助手
运维·人工智能
orion5719 小时前
Missing Semester Class1:course overview and introduction of shell
linux
SkyWalking中文站1 天前
认识 Horizon UI · 6/17:Trace 探索器
运维·监控·自动化运维
用户120487221611 天前
Linux驱动编译与加载
linux·嵌入式
火车叼位1 天前
写给初级开发者:SSL、SSH、HTTPS 与证书体系全解析
运维
用户805533698031 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698031 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
小猿姐2 天前
唯品会大规模数据库云原生实践:基于 KubeBlocks 管理数千实例的统一运维之路
运维·elasticsearch·云原生
七歌杜金房2 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
SkyWalking中文站2 天前
认识 Horizon UI · 5/17:3D 基础设施地图
运维·监控·自动化运维