ubuntu开发环境安装配置支持vnc远程访问

宿主机为ubuntu 22.04 jammy, 客户机为lxd container centos stream 9

tigervnc vnc方案

方案一:libvnc.so可以共享物理机屏幕,可以做到vnc登录lightdm(gdm方案有问题,gdm登录后,vnc断连,网上说是因为gdm登录后会kill掉当前xorg,再新建xorg)但是刷新性能有问题,后续可尝试x0vncserver方案

方案二:x0vncserver方案,需要本地物理机gdm登录后,才能vnc登录,未尝试

方案三:就是虚拟桌面方案,下文中lxd和宿主机尝试的方案,受限主要是不能在物理机上登录同一个用户桌面,只能vnc登录和桌面级登录分别两个用户

使用LXD安装centos容器,并配置vnc访问

关闭selinux

1
2
vi /etc/selinux/config
SELINUX=disabled

关闭防火墙

1
2
sudo systemctl stop firewalld
sudo systemctl disable firewalld

强制切换到xorg

1
2
sudo vim /etc/gdm3/custom.conf
WaylandEnable=false

安装tigervnc

1
sudo dnf install tigervnc-server -y

配置vncserver用户:在 /etc/tigervnc/vncserver.users中指定运行VNC的用户和显示编号,vnc连接端口号为5902

1
2
vi /etc/tigervnc/vncserver.users
:2=chenqp

配置vnc密码,注意跟用户密码不是一回事,是在vncview连接时输入的密码。输入vncpasswd

vnc配置产生

方法一:在~/.vnc/目录下新增配置文件config(更推荐)

1
2
3
4
5
session=gnome
geometry=1920x1080
alwaysshared
localhost=no
securitytypes=vncauth,tlsvnc

方法二:在/etc/tigervnc/vncserver-config-defaults配置文件中添加以下几行

1
2
3
session=gnome
geometry=1920x1080
alwaysshared

设置systemd开机启动任务sudo systemctl enable vncserver@:2

重启centossudo reboot

宿主机配置

1
lxc config device add centos vnc-proxy proxy listen=tcp:0.0.0.0:5902 connect=tcp:127.0.0.1:5902

宿主机与客户机共享目录

给宿主机设置成特权容器lxc config set centos security.privileged true

在宿主机上创建共享目录mkdir -p /home/xxx/share

将共享目录设置给客户机lxc config device add centos share-dir disk source=/home/xxx/share path=/mnt/share

宿主机ubuntu自身vnc安装

基本上步骤等同于centos安装,区别在systemd启动开机任务配置sudo systemctl enable tigervncserver@:2,注意端口号要与lxd容器centos的端口号区分开

宿主机笔记本合盖不休眠,背光关闭,WiFi不断连配置

修改笔记本电源配置

1
2
3
4
5
6
7
8
sudo vim /etc/systemd/logind.conf
HandleLidSwitch=ignore
HandleLidSwitchExternalPower=ignore
LidSwitchIgnoreInhibited=no
sudo systemctl restart systemd-logind
gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action blank
gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action blank
gsettings set org.gnome.desktop.session idle-delay 60 #空闲时自动关屏(建议1分钟)

修改networkmanager的WiFi节能配置

1
2
3
4
5
sudo tee /etc/NetworkManager/conf.d/wifi-powersave-off.conf >/dev/null <<EOF
[connection]
wifi.powersave=2
EOF
sudo systemctl restart NetworkManager

修改iwlwifi驱动配置/etc/modprobe.d/iwlwifi.conf

1
2
3
4
5
6
7
8
9
# iwlwifi will dyamically load either iwldvm or iwlmvm depending on the
# microcode file installed on the system. When removing iwlwifi, first
# remove the iwl?vm module and then iwlwifi.
remove iwlwifi \
(/sbin/lsmod | grep -o -e ^iwlmvm -e ^iwldvm -e ^iwlwifi | xargs /sbin/rmmod) \
&& /sbin/modprobe -r mac80211
options iwlwifi 11n_disable=0
options iwlwifi swcrypto=1
options iwlwifi power_save=0

修改WIFI连接,只连2.4G网络。之前WiFi断连的主要原因是,5G频段在移门遮挡后信号就降低10db以上,然后就切换到2.4G,但是后续还会继续尝试连接5G信号,然后循环掉到2.4G,尝试多次后就进入低功耗状态不再尝试连接。也试过固定连接5G频段,但是信号差了以后,WiFi显示连接上,但是网络不通,信号好了以后也不自动恢复。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
sudo nano /etc/NetworkManager/system-connections/xxx.nmconnection
[connection]
id=WiFi的bssid
uuid=86c2f162-2ebd-4b38-ad5f-6e5f2582fa22
type=wifi
interface-name=wlp1s0

[wifi]
mode=infrastructure
ssid=WiFi的bssid
band=bg
bssid=82:xx:xx:xx:xx:xx

[wifi-security]
auth-alg=open
key-mgmt=wpa-psk
psk=WiFi密码

[ipv4]
method=auto

[ipv6]
addr-gen-mode=stable-privacy
method=auto

[proxy]

命令行查看目前WiFi连接的状态iw dev wlp1s0 link,wlp1s0为WiFi接口名

VNC客户端

推荐tigervnc-vncviewer,可以支持分辨率自适应

直接在宿主机上访问lxd容器图形界面的方式

共享宿主机xorg接口的方式

宿主机执行xhost +local:root

配置容器
映射 UID/GID(关键)

1
lxc config set gui-app raw.idmap "both 1000(UID) 1000(GID)"

挂载 X11 套接字(让容器能画到宿主机屏幕)

1
2
3
lxc config device add 容器名 X0 disk \
path=/tmp/.X11-unix/X0($SIPLAY) \
source=/tmp/.X11-unix/X0($DISPLY)

可选:挂载声卡(让容器有声音,没有试过)

1
2
3
lxc config device add gui-app pulse disk \
path=/run/user/1000/pulse \
source=/run/user/1000/pulse

重启生效

1
lxc restart gui-app

容器中安装GUI程序测试

1
lxc exec 容器名 -- apt install gedit -y

运行容器中的GUI程序

1
lxc exec 容器名 -- gedit

使用ssh -x转发x绘图到宿主机,需要容器和宿主机都是xorg效果还行,图形弹出速度没有共享宿主机xorg接口的方式快