SSH 遠(yuǎn)程登錄 WSL

飽受 Teamviewer/向日葵 之苦后,還是決定操作一番

安裝 openssh-server

# 刪除然后重新裝ssh
# 并不知道為什么,但好多人這么搞

sudo apt-get remove openssh-server
sudo apt-get install openssh-server

修改 sshd_config 文件

sudo vi /etc/ssh/sshd_config
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::


# 更改端口,防沖突
# 監(jiān)聽地址去掉注釋
Port 2222
#AddressFamily any
ListenAddress 0.0.0.0
#ListenAddress ::

PasswordAuthentication no --> yes
PermitEmptyPasswords no --> yes

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

PermitRootLogin

#PermitRootLogin prohibit-password

# 改成??

PermitRootLogin yes

重啟 ssh server

sudo service ssh restart
# * Starting OpenBSD Secure Shell server sshd

# 查看 ssh server 狀態(tài)
service ssh status
# * sshd is running

查看 IP

ifconfig
#zsh: command not found: ifconfig

# 如果出現(xiàn) command not found
sudo apt install net-tools

ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.20.32.234  netmask 255.255.128.0  broadcast 10.20.127.255
        inet6 2001:da8:201d:1101:d76c:f9ab:64e3:8cec  prefixlen 128  scopeid 0x0<global>
        inet6 fe80::a532:2905:ea6f:f886  prefixlen 64  scopeid 0xfd<compat,link,site,host>
        ether 6c:4b:90:06:8b:2d  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 1500
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0xfe<compat,link,site,host>
        loop  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

也可以:

ip a

查看 hostname, username

hostname
#DESKTOP-19JBVQS

whoami
#shakesbeer

設(shè)置防火墻

不知道為什么 Windows Defender 下面的選項(xiàng)是灰色的點(diǎn)不開....
只好從控制面板進(jìn)

See --> 用 ssh 從外部連接到寢室個(gè)人電腦的 Ubuntu 系統(tǒng)或 win10 下的 WSL--Linux 子系統(tǒng)

或者在 cmd 中

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

查看公網(wǎng) IP

http://www.net.cn/static/customercare/yourip.asp
好像并不需要

端口映射

管理員身份打開 CMD

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

# listenport=[win10端口] listenaddress=0.0.0.0 connectport=[虛擬機(jī)的端口] connectaddress=[虛擬機(jī)的ip]

listenport = 這一步修改的 PORT
listenaddress = 0.0.0.0
connectport = Win10 默認(rèn)的2222
connectaddress= WSL 中 ifconfig 查看到的

兩個(gè) port 應(yīng)該是一樣的

netsh interface portproxy show all

SSH 登錄

本地登錄

IP 為 前面 ifconfig 查到的本地 IP

ssh shakesbeer@127.0.0.1

Finalshell 設(shè)置長這樣


遠(yuǎn)程登錄

IP 為公網(wǎng) win10 電腦 IP,寫這篇帖子時(shí)很神奇地 WSL 和電腦 IP 是一致的,后來也許 win10 晚上自動(dòng)偷偷更新重啟,導(dǎo)致 WSL IP 變了,糾結(jié)了一周都無法再遠(yuǎn)程登錄,看了這個(gè)視頻后終于悟了,需要對 WSL 的 IP 進(jìn)行映射,在其他機(jī)器上則需要 ssh 到 win 10 電腦的 IP。

在另一臺(tái)機(jī)器上:

ssh shakesbeer@10.20.32.234 # win10 ip

Note

看別人的帖子說每次 WSL 打開后,IP 會(huì)變,所以需要一個(gè)固定的 WSL IP,等有空再弄,
wsl---ssh遠(yuǎn)程連接及ip映射配置詳解 - 知乎

  • wsl “固定IP”配置

Ref.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容