Loading

从远程主机使用ssh连接WSL2

使用WSL2中系统为:Ubuntu 20.04.6 LTS on Windows 10 x86_64

一、WSL2中ssh配置

1.1 重装ssh服务

先执行:

sudo apt remove openssh-server

再重新安装:

sudo apt install openssh-server

1.2 修改ssh配置信息

执行:

sudo vim /etc/ssh/sshd_config

之后编辑配置文件,可以直接替换如下内容:

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

# ssh服务监听端口
Port 2222
#AddressFamily any
# ssh服务监听地址
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
# ssh服务允许远程root用户登入
PermitRootLogin yes
#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!
# ssh服务允许使用用户名密码方式登入
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

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

1.3 重启ssh服务

上述配置保存之后执行:

sudo service ssh restart

可以通过 sudo service ssh status 看到ssh服务状态:

xxxx:~$ sudo service ssh status
* ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-08-30 14:00:01 CST; 17min ago
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 1826 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 1827 (sshd)
      Tasks: 1 (limit: 23635)
     Memory: 2.6M
     CGroup: /system.slice/ssh.service
             `-1827 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups

Aug 30 14:00:01 mz-rog systemd[1]: Stopping OpenBSD Secure Shell server...
Aug 30 14:00:01 mz-rog systemd[1]: ssh.service: Succeeded.
Aug 30 14:00:01 mz-rog systemd[1]: Stopped OpenBSD Secure Shell server.
Aug 30 14:00:01 mz-rog systemd[1]: Starting OpenBSD Secure Shell server...
Aug 30 14:00:01 mz-rog sshd[1827]: Server listening on 0.0.0.0 port 2222.
Aug 30 14:00:01 mz-rog systemd[1]: Started OpenBSD Secure Shell server.
Aug 30 14:05:23 mz-rog sshd[1861]: Accepted password for xxxx from 127.0.0.1 port 38014 ssh2
Aug 30 14:05:23 mz-rog sshd[1861]: pam_unix(sshd:session): session opened for user xxxx by (uid=0)

1.3 本地连接WSL

上述文件保存之后,本机已经可以通过ssh root@localhost -p 2222连接到WSL

二、配置远程连接

2.1 查看wsl的ip地址

首先安装ifconfig工具
执行:

sudo apt install net-tools

通过执行ifconfig查看IP地址,eth0:中172.17.45.15为wsl地址:

xxxx:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.45.15  netmask 255.255.240.0  broadcast 172.17.47.255
        inet6 fe80::215:5dff:fef6:518f  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:f6:51:8f  txqueuelen 1000  (Ethernet)
        RX packets 404  bytes 459441 (459.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 150  bytes 23864 (23.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 326  bytes 45224 (45.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 326  bytes 45224 (45.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2.2 将端口转发到WSL

在Power Shell(管理员)下执行命令:

netsh interface portproxy add v4tov4 listenport=2222 listenaddress=0.0.0.0 connectport=2222 connectaddress=localhost

2.3 设置Windows防火墙入站规则

在Power Shell(管理员)下执行命令:

netsh advfirewall firewall add rule name=WSL2 dir=in action=allow protocol=TCP localport=2222

上述命令执行完后,可以通过远程设备ssh访问wsl

ssh root@机器ip -p 2222

可以通过如下命令查看所有 portproxy,在Power Shell(管理员)下执行命令:

netsh interface portproxy show v4tov4

可以看到:

PS C:\Application\WSL> netsh interface portproxy show v4tov4

侦听 ipv4:                 连接到 ipv4:

地址            端口        地址            端口
--------------- ----------  --------------- ----------
0.0.0.0         2222        localhost       2222
0.0.0.0         80          172.17.45.15    80
0.0.0.0         443         172.17.45.15    443
0.0.0.0         10000       172.17.45.15    10000
0.0.0.0         3000        172.17.45.15    3000
0.0.0.0         5000        172.17.45.15    5000

0.0.0.0 2222 localhost 2222 即是转发到wsl的端口设置

posted @ 2024-08-27 18:50  还会再见吗燕子  阅读(246)  评论(1)    收藏  举报