編輯配置文件
sudo vim /etc/ssh/sshd_config
Port 8022(因為 Windows 10 的 SSH 端口已經默認被占用,所以我換成了一個新的端口) (去掉前面的 #)ListenAddress 0.0.0.0 UsePrivilegeSeparation no(原來是 yes 改成 no)#如果沒有這句就不需要添加 PermitRootLogin yes(修改成 yes) (在前面加上 #)StrictModes yes PasswordAuthentication yes(原來是 no,改成 yes)
4. 在WSL里啟動ssh服務:
sudo service ssh start
5. 打開Xshell或Xftp:
選擇「新建會話」,新建的會話設置框的「連接」中添加如下內容:
名稱:WSL(這個隨便填) 協議:SSH 主機:127.0.0.1(本機環回接口) 端口號:8022
然后輸入WSL的賬號和密碼
在啟用 ssh 服務時,很可能會看到如下提示:
root@bt:~# /etc/init.d/ssh start
* Starting OpenBSD Secure Shell server sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_dsa_key
有兩個提示 Could not load host key: /etc/ssh/ssh_host_rsa_key 和 Could not load host key: /etc/ssh/ssh_host_dsa_key ,此時如果從客戶端連接到服務器時是不會成功的。其原因是在 SSH 連接協議中需要有 RSA 或 DSA 密鑰的鑒權。 因此,我們可以在服務器端使用 ssh-keygen 程序來生成一對公鑰/私鑰對
運行下面命令:
root@bt:/etc/ssh# ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): #直接回車即可
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
3b:a4:b8:df:a9:15:d1:62:df:d5:d1:41:50:59:4a:96 root@bt
The key's randomart image is:
+--[ RSA 2048]----+
| .***|
| . oE+o|
| + . o .|
| . + . . |
| S . . |
| . o o |
| . . + |
| . o o |
| ..o.o |
+-----------------+
上面 ssh-keygen 命令中,-t 選項表示生成的密鑰所使用的加密類型,這里選擇的是 RSA ;-b 選項表示 bit,后接一個整數,表示加密的位數,該數值越大表示加密的強度越高;-f 選項后接要生成的密鑰文件名。根據 /etc/ssh 目錄下的 sshd_config 配置文件,RSA 密鑰默認識別文件名為 ssh_host_rsa_key 。
命令執行成功后,在 /etc/ssh 下會看到有兩個文件生成:ssh_host_rsa_key 和 ssh_host_rsa_key.pub ,前者是私鑰,后者是公鑰。這樣,當再次用 putty 連接時,首先會提示服務器端的加密指紋已經被修改,如果信任則點 yes 繼續,否則斷開,這里當然是選擇信任 yes 。此后,客戶端將用公鑰對數據進行加密后發往服務器,而服務器收到數據后則用私鑰進行解密。
如果登錄提示密碼不對,需要重新修改root密碼 ,sudo passwd root