安装完Windows子系统,然后安装ssh服务并启动子系统的ssh服务
$ sudo apt-get install openssh-server
$ sudo service ssh start
$ service ssh status
* sshd is running
查看子系统的ip地址:192.168.148.2
4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:15:5d:3d:41:cf brd ff:ff:ff:ff:ff:ff
inet 192.168.148.2/20 brd 192.168.159.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::215:5dff:fe3d:41cf/64 scope link
valid_lft forever preferred_lft forever
假设你的Windows的ip地址为:192.168.3.24,默认情况下外界是无法直接通过 192.168.148.2 访问子系统 Ubuntu 的,需要在Windows上做一个端口转发、防火墙放行:
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=22 connectaddress=192.168.148.2 connectport=22
netsh advfirewall firewall add rule name=WSL2 dir=in action=allow protocol=TCP localport=22
最后局域网的其他机器就登录啦:
[yzy@yzym ~] ssh goldtools@192.168.3.24
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.10.16.3-microsoft-standard-WSL2 x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Last login: Thu Sep 21 15:54:41 2023 from 192.168.144.1
-bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
goldtools@DESKTOP-GOLDTOOLS:~$
goldtools@DESKTOP-GOLDTOOLS:~$ ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.148.2 netmask 255.255.240.0 broadcast 192.168.159.255
inet6 fe80::215:5dff:fe3d:41cf prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:3d:41:cf txqueuelen 1000 (Ethernet)
RX packets 878 bytes 297442 (297.4 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 630 bytes 93135 (93.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0