WSL 安装 OpenSSH-Server

在 Windows Linux子系统(WSL:ubuntu:20.4 LTS)上安装 OpenSSH-Server 通过 SSH 远程访问

直接通过 SSH 登录时提示错误如下:

shell
1
2
$ ssh akiya@localhost
ssh: connect to host localhost port 22: Connection refused

怀疑是没有安装 OpenSSH-Server,接着便使用命令安装

shell
1
2
3
4
5
$ sudo apt install openssh-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
openssh-server is already the newest version (1:8.2p1-4ubuntu0.1).

根据返回信息提示,WSL 上已经安装有最新版本的 OpenSSH-Server,那么应该是 OpenSSH-server 服务未启动

shell
1
2
$ sudo service ssh restart
sshd: no hostkeys available -- exiting.

启动服务时又提示报错上,根据网上查阅资料,应该是缺少文件。使用如下命令生成 key 文件

shell
1
2
$ sudo ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
$ sudo ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

然后重启 ssh 服务,可以看到启动成功

shell
1
2
$ sudo service ssh restart
* Restarting OpenBSD Secure Shell server sshd [ OK ]

现在我们可以使用 ssh 登录 WSL 了

shell
1
2
3
4
$ ssh akiya@localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is SHA256:IE0uKLebsAS9/FbuQEHq8sCkauJUwkj0hDlpAhtn0C0.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

不过需要使用 SSH Key 登录,不然会报错如下:

shell
1
2
3
4
5
6
# ssh akiya@192.168.1.184
The authenticity of host '192.168.1.184 (192.168.1.184)' can't be established.
RSA key fingerprint is SHA256:kupZMJjigDbmvj9Sj3liyzp3KgVzRViuXO6SESX+l5o.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.184' (RSA) to the list of known hosts.
akiya@192.168.1.184: Permission denied (publickey).

在 WSL 中修改 /etc/ssh/sshd_configPasswordAuthentication 值为 yes。允许使用密码登录

/etc/ssh/sshd_config
1
PasswordAuthentication yes

然后再次重启 ssh

shell
1
$ sudo service ssh restart

现在可以通过远程 ssh 访问 WSL 了

shell
1
2
3
ssh akiya@192.168.1.184
akiya@192.168.1.184's password:
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 4.4.0-18362-Microsoft x86_64)

参考文章:

评论

:D 一言句子获取中...

加载中,最新评论有1分钟缓存...