「Linux」 X11 转发

Posted by Dawn-K's Blog on May 4, 2022

X11 转发

背景

X11 是 linux 目前使用的图形界面,是 cs 架构,可以在 linux 上运行 server,然后在另一台计算机上运行 client,这样就可以通过网络实现界面的转发。

使用场景有很多,比如虚拟机,云服务器,WSL。

参考资料

linux 配置

linux 安装 xorg 组件

首先要安装好 server 端,也就是要转发的系统。以 archlinux 为例,首先要安装好 xorg

# 安装组件
sudo pacman -S  xorg xorg-xinit xorg-xauth
# 配置权限
touch ~/.Xauthority && chmod 600 ~/.Xauthority
# 启动 xorg,这里可以只使用 xorg-xinit,不过我测试了一下不行,不知道是什么原因 
startx

linux 配置 xorg

  1. 修改 sshd_config
1
2
# 修改配置为下文内容
sudo vim /etc/ssh/sshd_config
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#       $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/local/sbin:/usr/local/bin:/usr/bin

# 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.

Port 22
#AddressFamily any
AddressFamily inet
#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

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

#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 yes

# Change to no to disable s/key passwords
ChallengeResponseAuthentication no

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

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

# 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
#ForwardX11Trusted yes
X11DisplayOffset 10
X11UseLocalhost yes
#PermitTTY yes
#PrintMotd no # pam does that
#PrintLastLog yes
TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

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

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding yes
#       AllowTcpForwarding yes
#       PermitTTY no
#       ForceCommand cvs server

Windows 宿主机配置

安装 Xming

Xming 是用来显示转发后的界面的,并不负责连接,安装的时候只安装本体就行,不用安装 putty 和 xshell 之类的。

下载地址

配置 windows terminal

在 windows terminal 上使用配置文件进行配置。(在此就不赘述 ssh 免密登录了) 注意的是 -XY 参数。-X:开启 X11 转发功能 -Y:开启信任 X11 转发功能。

1
2
3
4
5
6
7
8
9
{
    "bellStyle": "none",
    "commandline": "ssh -XY -p <port> <username>@<ip>",
    "cursorShape": "filledBox",
    "guid": "{f252467b-12f4-4753-b670-2072917c1215}",
    "icon": "D:\\Documents\\logo\\favicon.jpg",
    "name": "Arch",
    "startingDirectory": "%USERPROFILE%"
}

这一步嫌麻烦可以用 putty 进行连接,智能一些,就是太丑了,而且要切换。

使用一些图形应用,比如 chromium 的时候,可以事先在虚拟机的环境变量里加入 DISPLAY=localhost:10 , 这里的 10 是因为前文的 X11DisplayOffset 10 .

putty 的话是在 SSH -> X11 里面,首先 enable X11 转发,然后输入 localhost:10 , 就可以自动添加到本次会话的环境变量中。