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

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

文章目录

  • Windows上可视化ubuntu程序图形界面
  • [1 vscode远程配置](#1 vscode远程配置)
  • [2 界面导致程序无法运行问题](#2 界面导致程序无法运行问题)
    • [2.1 Xvfb](#2.1 Xvfb)
    • [2.2 X11转发](#2.2 X11转发)
      • [2.2.1 Ubuntu设置](#2.2.1 Ubuntu设置)
      • [2.2.2 Windows安装Xming](#2.2.2 Windows安装Xming)
      • [2.2.3 其余配置](#2.2.3 其余配置)
      • [2.2.4 测试验证](#2.2.4 测试验证)

1 vscode远程配置

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

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

或者用快捷键搜索打开!

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

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

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

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

sh 复制代码
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

sh 复制代码
sudo apt-get install xvfb

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

sh 复制代码
xvfb-run -a ./可执行文件

2.2 X11转发

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

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

Windows:安装Xming,配置并启动

2.2.1 Ubuntu设置

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

yaml 复制代码
#	$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服务:

sh 复制代码
sudo service ssh restart

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

ForwardX11 yes

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

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

sh 复制代码
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 环境变量,

sh 复制代码
export DISPLAY=localhost:0.0		# 0.0是因为下面Xming后面是0.0
txt 复制代码
localhost则是本机分配给虚拟机的地址,win+R,打卡cmd,输入ipifconfig,查看对应的ip即可

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

sh 复制代码
echo $DISPLAY

2.2.4 测试验证

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

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

sh 复制代码
xclock		# 如果没有显示,说明上面DISPLAY没有设置正确

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

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

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

相关推荐
SummerGao.3 分钟前
Windows 快速搭建C++开发环境,安装C++、CMake、QT、Visual Studio、Setup Factory
c++·windows·qt·cmake·visual studio·setup factory
YH_DevJourney38 分钟前
Linux-C/C++《C/8、系统信息与系统资源》
linux·c语言·c++
威哥爱编程1 小时前
Linux驱动开发13个实用案例
linux
去看日出1 小时前
Linux(centos)系统安装部署MySQL8.0数据库(GLIBC版本)
linux·数据库·centos
qq_448941081 小时前
10、k8s对外服务之ingress
linux·容器·kubernetes
weixin_387002151 小时前
Openssl之SM2加解密命令
安全·ubuntu·密码学·ssl·命令模式
Igallta_8136222 小时前
【小游戏】C++控制台版本俄罗斯轮盘赌
c语言·开发语言·c++·windows·游戏·游戏程序
张海森-1688202 小时前
ubuntu20.04重启后不显示共享文件夹
ubuntu
默凉2 小时前
Ubuntu 20.04源码安装opencv 4.5.0
opencv·ubuntu
D-river2 小时前
【如何基于Debian构建Kali Linux】
linux·网络·安全·网络安全