Windows上可视化ubuntu程序图形界面

Windows上可视化ubuntu程序图形界面

1 vscode远程配置

1 点击左下角SSH进行远程连接,选择添加新的SSH主机

在这里插入图片描述

2 设置主机ip地址,用户名

在这里插入图片描述

或者用快捷键搜索打开!

Host 192.xxx.xxx.1x7
    HostName 192.xxx.xxx.1x7
    User pj
    ForwardX11 yes		# 为了后面可以把ubuntu的程序可视化界面显示到windows,这里添加
    ForwardX11Trusted yes

在这里插入图片描述

3 配置完成,选择主机,输入密码即可!

在这里插入图片描述

2 界面导致程序无法运行问题

  因为要远程执行的程序有opencv可视化界面,直接在windows执行会报错如下!

Unable to init server: Could not connect: Connection refused
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(3.4.16) /home/pj/opencv3.4.16/opencv-3.4.16/modules/highgui/src/window_gtk.cpp:624: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

Aborted (core dumped)

  有两种方法解决:

2.1 Xvfb

  使用虚拟显示环境,如XvfbX virtual framebuffer)来模拟一个显示环境(实际上我们是看不到真实画面的,但是程序可以运行了)。

安装Xvfb

sudo apt-get install xvfb

然后在Xvfb环境中运行你的程序:

xvfb-run -a ./可执行文件

2.2 X11转发

  为了在本地机器上显示虚拟机程序的图形界面,可以使用X11转发

Linux:已经预装X11,检查SSH的相应配置

Windows:安装Xming,配置并启动

2.2.1 Ubuntu设置

  检查虚拟机上的SSH服务器配置文件(通常是 /etc/ssh/sshd_config)中,确保以X11Forwarding等未被注释,并设置为 yes。下面是我虚拟机设置

#	$OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

Include /etc/ssh/sshd_config.d/*.conf

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#PubkeyAuthentication yes

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile	.ssh/authorized_keys .ssh/authorized_keys2

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

# override default of no subsystems
Subsystem	sftp	/usr/lib/openssh/sftp-server

然后重启SSH服务:

sudo service ssh restart

还需要再Windows上的vsocde上的SSH客户端配置文件(通常是 C:/Users/xx/.ssh/config)中,确保包含以下内容(上面远程配置已经提到过)

ForwardX11 yes

如果不是利用vscode,可以直接下面命令进行配置,效果是等价的

X11转发设置:连接时可以使用-X-Y选项,

ssh -Y pj@192.xxx.11x.xxx

2.2.2 Windows安装Xming

  想在Windows可视化,还需要安装Xming下载地址,安装过程直接next即可。主要说下软件的配置

第一步:点击Xlaunch

在这里插入图片描述

第二步

在这里插入图片描述

第三步,一定要勾选No Access Control , 否则后面执行 exportWindows主机上时会提示 No protocol specified

在这里插入图片描述

第四步,点击完成即可

2.2.3 其余配置

  假设这个时候已经通过远程连接成功

① 在SSH会话中,设置 DISPLAY 环境变量,

export DISPLAY=localhost:0.0		# 0.0是因为下面Xming后面是0.0

在这里插入图片描述

localhost则是本机分配给虚拟机的地址,win+R,打卡cmd,输入ipifconfig,查看对应的ip即可

在这里插入图片描述

② 上面设置好 DISPLAY 环境变量后,利用下面命令查看是否设置正确

echo $DISPLAY

2.2.4 测试验证

到此,配置好之后,即可验证

① 测试X11转发是否工作,如果显示了时钟窗口,则说明X11转发配置正确,可以继续运行

xclock		# 如果没有显示,说明上面DISPLAY没有设置正确

在这里插入图片描述

② 检查本地服务器,确保Xming已经启动

xhost +  # 如果成功,则显示如下;如果不成功,那么一般是打开时没有勾选`No Access Control`,见上面2.2.2

在这里插入图片描述

③ 执行一个程序验证,下图所示,已经构建一个连接,程序可视化也能在Windows显示

在这里插入图片描述

### 如何从 Windows 使用远程桌面协议(RDP)连接和控制 Ubuntu 的 GUI 环境 为了使 Windows 能够通过 RDP 协议连接到 Ubuntu 并操作其图形界面,需完成几个必要的配置步骤。 #### 安装 XRDP 服务 XRDP 是一个开源项目,允许用户通过 Microsoft 远程桌面客户端或其他兼容 RDP 的应用程序安全地访问 Linux 桌面环境。在终端中执行以下命令来安装 XRDP: ```bash sudo apt update && sudo apt install xrdp -y ``` 这会自动下载并安装最新版本的 XRDP 及所需依赖项[^4]。 #### 配置防火墙规则 如果启用了 UFW(Uncomplicated Firewall),则需要开放特定端口以便让来自外部网络的数据包能够到达运行中的 XRDP 服务器进程。输入如下指令打开默认使用的 TCP 3389 端口: ```bash sudo ufw allow 3389/tcp ``` 对于其他类型的防火墙管理工具,请参照相应文档调整策略以放通该端口号上的流量。 #### 设置 GDM3 显示管理器 某些情况下,默认显示管理器可能不是最适配的选择;此时可以切换至 GDM3 来提高稳定性与性能表现。使用下面这条语句更改当前系统的登录屏幕处理程序: ```bash sudo systemctl set-default graphical.target sudo dpkg-reconfigure gdm3 ``` 选择 `gdm3` 后按回车键确认修改决定。 #### 修改 GRUB 引导菜单参数 为了让系统启动时加载适合远程访问的画面分辨率选项,在 `/etc/default/grub` 文件里找到含有 `GRUB_CMDLINE_LINUX_DEFAULT=` 字样的那一行,并在其双引号内追加 `nomodeset` 参数保存退出编辑模式之后记得更新 grub 配置使之生效: ```bash sudo nano /etc/default/grub # 将 "quiet splash" 改成 "quiet splash nomodeset" sudo update-grub ``` 重启计算机应用上述变动。 #### 测试连接性 现在可以从任意一台已联网设备上尝试建立新的远程桌面会话了——只需启动「远程桌面连接」实用程序,接着按照提示框内的指示填写目标主机 IP 地址即可发起请求验证整个过程是否顺利无误。 ```powershell mstsc.exe ``` 以上就是在 Windows 中利用自带的功能实现对远端 Ubuntu 实例可视化操控的方法概述。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值