解决无法ssh命令登录wsl问题

本地主机ssh登录wsl报错被拒绝访问

C:\Users\jiangcheng> ssh root@xxx.xx.xxx.xx -p 22 ssh: connect to host xxx.xx.xxx.xx port 22: Connection refused

解决步骤如下:

1,解决密码不对的问题

wsl默认用户名root的密码是随机的,需要重新修改

root@jiangcheng01:~# sudo passwd New password: Retype new password: passwd: password updated successfully

修改之后,密码可以使用了。

2、查看系统ip 使用命令 ifconfig 报错,根据提示安装 net-tools

root@jiangcheng01:~/common/remote/dev01# ifconfig Command 'ifconfig' not found, but can be installed with: apt install net-tools root@jiangcheng01:~/common/remote/dev01# apt install net-tools Reading package lists... Done Building dependency tree... Done Reading state information... Done The following NEW packages will be installed: net-tools 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 192 kB of archives. After this operation, 856 kB of additional disk space will be used. Get:1 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy/main amd64 net-tools amd64 1.60+git20180626.aebd88e-1ubuntu2 [192 kB] Fetched 192 kB in 0s (662 kB/s) Selecting previously unselected package net-tools. (Reading database ... 24148 files and directories currently installed.) Preparing to unpack .../net-tools_1.60+git20180626.aebd88e-1ubuntu2_amd64.deb ... Unpacking net-tools (1.60+git20180626.aebd88e-1ubuntu2) ... Setting up net-tools (1.60+git20180626.aebd88e-1ubuntu2) ... Processing triggers for man-db (2.10.2-1) ... root@jiangcheng01:~/common/remote/dev01# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet xxx.xx.xxx.xxx netmask xxx.xx.xxx.xxx broadcast xxx.xx.xxx.xxx inet6 fe80::215:5dff:fe23:20ff prefixlen 64 scopeid 0x20<link> ether 00:15:5d:23:20:ff txqueuelen 1000 (Ethernet) RX packets 1585 bytes 379560 (379.5 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 257 bytes 31076 (31.0 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 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

2,解决ssh无法连接的问题

先查看ssh服务是否启动

root@jiangcheng01:~# ps -ef|grep ssh root 836 14 0 09:52 pts/0 00:00:00 ssh -x -a -oClearAllForwardings=yes -oHostKeyAlgorithms=+ssh-dss -2 htrd@172.16.163.99 -s sftp root 837 14 0 09:52 ? 00:00:00 sshfs -oHostKeyAlgorithms=+ssh-dss htrd@172.16.163.99:/home/htrd /root/common/remote/dev01 root 987 962 0 10:39 pts/0 00:00:00 grep --color=auto ssh root@jiangcheng01:~# ssh -V OpenSSH_8.9p1 Ubuntu-3ubuntu0.1, OpenSSL 3.0.2 15 Mar 2022 root@jiangcheng01:~# service ssh start ssh: unrecognized service

3,解决 ssh:unrecognized service 问题

apt-get install openssh-server

报错如下:

root@jiangcheng01:~# apt-get install openssh-server Reading package lists... Done Building dependency tree... Done Reading state information... Done Some packages could not be installed. This may mean that you have requested an impossible situation or if you are using the unstable distribution that some required packages have not yet been created or been moved out of Incoming. The following information may help to resolve the situation: The following packages have unmet dependencies: openssh-server : Depends: openssh-client (= 1:8.3p1-1ubuntu0.1) but 1:8.9p1-3ubuntu0.1 is to be installed Depends: openssh-sftp-server but it is not going to be installed Recommends: ncurses-term but it is not going to be installed Recommends: ssh-import-id but it is not going to be installed E: Unable to correct problems, you have held broken packages.

4,解决 you have held broken packages. 问题先卸载后重新安装

a、 apt-get autoremove openssh-client openssh-server

root@jiangcheng01:~# apt-get autoremove openssh-client openssh-server Reading package lists... Done Building dependency tree... Done Reading state information... Done Package 'openssh-server' is not installed, so not removed The following packages will be REMOVED: libfuse2 liblzo2-2 libxext6 libxmuu1 openssh-client snapd squashfs-tools sshfs xauth 0 upgraded, 0 newly installed, 9 to remove and 0 not upgraded. After this operation, 107 MB disk space will be freed. Do you want to continue? [Y/n] y (Reading database ... 24197 files and directories currently installed.) Removing snapd (2.58+22.04) ... Stopping snap.ubuntu-desktop-installer.subiquity-server.service Stopping unit snap.ubuntu-desktop-installer.subiquity-server.service System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down Waiting until unit snap.ubuntu-desktop-installer.subiquity-server.service is stopped [attempt 1] System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down snap.ubuntu-desktop-installer.subiquity-server.service is stopped. Removing sshfs (3.6.0+repack+really2.10-0ubuntu1) ... Removing libfuse2:amd64 (2.9.9-3) ... Removing squashfs-tools (1:4.5-3build1) ... Removing liblzo2-2:amd64 (2.10-2build3) ... Removing xauth (1:1.1-1build2) ... Removing libxext6:amd64 (2:1.3.4-1build1) ... Removing libxmuu1:amd64 (2:1.1.3-3) ... Removing openssh-client (1:8.9p1-3ubuntu0.1) ... Processing triggers for man-db (2.10.2-1) ... Processing triggers for dbus (1.12.20-2ubuntu4.1) ... Processing triggers for libc-bin (2.35-0ubuntu3.1) ...

b、 apt-get install openssh-client openssh-server

root@jiangcheng01:~# apt-get install openssh-client openssh-server Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libssl1.1 libwrap0 libxext6 libxmuu1 ncurses-term openssh-sftp-server python3-certifi python3-chardet python3-idna python3-requests python3-urllib3 ssh-import-id xauth Suggested packages: keychain libpam-ssh monkeysphere ssh-askpass molly-guard python3-openssl python3-socks python-requests-doc The following NEW packages will be installed: libssl1.1 libwrap0 libxext6 libxmuu1 ncurses-term openssh-client openssh-server openssh-sftp-server python3-certifi python3-chardet python3-idna python3-requests python3-urllib3 ssh-import-id xauth 0 upgraded, 15 newly installed, 0 to remove and 0 not upgraded. Need to get 3214 kB of archives. After this operation, 16.5 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy-updates/main amd64 libssl1.1 amd64 1.1.1f-1ubuntu4.4 [1325 kB] Get:2 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy/main amd64 libxext6 amd64 2:1.3.4-0ubuntu1 [29.1 kB] Get:3 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy/main amd64 libxmuu1 amd64 2:1.1.3-0ubuntu1 [9728 B] Get:4 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy-security/main amd64 openssh-client amd64 1:8.3p1-1ubuntu0.1 [682 kB] Get:5 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy/main amd64 xauth amd64 1:1.1-0ubuntu1 [25.0 kB] Get:6 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy/main amd64 libwrap0 amd64 7.6.q-30 [46.3 kB] Get:7 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy/main amd64 ncurses-term all 6.2-1 [247 kB] Get:8 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy-security/main amd64 openssh-sftp-server amd64 1:8.3p1-1ubuntu0.1 [51.8 kB] Get:9 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy-security/main amd64 openssh-server amd64 1:8.3p1-1ubuntu0.1 [382 kB] Get:10 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy/main amd64 python3-certifi all 2020.4.5.1-1 [151 kB] Get:11 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy/main amd64 python3-chardet all 3.0.4-7 [80.2 kB] Get:12 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy/main amd64 python3-idna all 2.10-1 [35.2 kB] Get:13 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy/main amd64 python3-urllib3 all 1.25.9-1 [88.8 kB] Get:14 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy/main amd64 python3-requests all 2.23.0+dfsg-2 [50.9 kB] Get:15 http://mirrors.ustc.edu.cn/ubuntu-old-releases/ubuntu groovy/main amd64 ssh-import-id all 5.10-0ubuntu1 [10.0 kB] Fetched 3214 kB in 2s (2136 kB/s) Preconfiguring packages ... Selecting previously unselected package libssl1.1:amd64. (Reading database ... 24002 files and directories currently installed.) Preparing to unpack .../00-libssl1.1_1.1.1f-1ubuntu4.4_amd64.deb ... Unpacking libssl1.1:amd64 (1.1.1f-1ubuntu4.4) ... Selecting previously unselected package libxext6:amd64. Preparing to unpack .../01-libxext6_2%3a1.3.4-0ubuntu1_amd64.deb ... Unpacking libxext6:amd64 (2:1.3.4-0ubuntu1) ... Selecting previously unselected package libxmuu1:amd64. Preparing to unpack .../02-libxmuu1_2%3a1.1.3-0ubuntu1_amd64.deb ... Unpacking libxmuu1:amd64 (2:1.1.3-0ubuntu1) ... Selecting previously unselected package openssh-client. Preparing to unpack .../03-openssh-client_1%3a8.3p1-1ubuntu0.1_amd64.deb ... Unpacking openssh-client (1:8.3p1-1ubuntu0.1) ... Selecting previously unselected package xauth. Preparing to unpack .../04-xauth_1%3a1.1-0ubuntu1_amd64.deb ... Unpacking xauth (1:1.1-0ubuntu1) ... Selecting previously unselected package libwrap0:amd64. Preparing to unpack .../05-libwrap0_7.6.q-30_amd64.deb ... Unpacking libwrap0:amd64 (7.6.q-30) ... Selecting previously unselected package ncurses-term. Preparing to unpack .../06-ncurses-term_6.2-1_all.deb ... Unpacking ncurses-term (6.2-1) ... Selecting previously unselected package openssh-sftp-server. Preparing to unpack .../07-openssh-sftp-server_1%3a8.3p1-1ubuntu0.1_amd64.deb ... Unpacking openssh-sftp-server (1:8.3p1-1ubuntu0.1) ... Selecting previously unselected package openssh-server. Preparing to unpack .../08-openssh-server_1%3a8.3p1-1ubuntu0.1_amd64.deb ... Unpacking openssh-server (1:8.3p1-1ubuntu0.1) ... Selecting previously unselected package python3-certifi. Preparing to unpack .../09-python3-certifi_2020.4.5.1-1_all.deb ... Unpacking python3-certifi (2020.4.5.1-1) ... Selecting previously unselected package python3-chardet. Preparing to unpack .../10-python3-chardet_3.0.4-7_all.deb ... Unpacking python3-chardet (3.0.4-7) ... Selecting previously unselected package python3-idna. Preparing to unpack .../11-python3-idna_2.10-1_all.deb ... Unpacking python3-idna (2.10-1) ... Selecting previously unselected package python3-urllib3. Preparing to unpack .../12-python3-urllib3_1.25.9-1_all.deb ... Unpacking python3-urllib3 (1.25.9-1) ... Selecting previously unselected package python3-requests. Preparing to unpack .../13-python3-requests_2.23.0+dfsg-2_all.deb ... Unpacking python3-requests (2.23.0+dfsg-2) ... Selecting previously unselected package ssh-import-id. Preparing to unpack .../14-ssh-import-id_5.10-0ubuntu1_all.deb ... Unpacking ssh-import-id (5.10-0ubuntu1) ... Setting up libssl1.1:amd64 (1.1.1f-1ubuntu4.4) ... Setting up libxext6:amd64 (2:1.3.4-0ubuntu1) ... Setting up python3-chardet (3.0.4-7) ... Setting up libwrap0:amd64 (7.6.q-30) ... Setting up python3-certifi (2020.4.5.1-1) ... Setting up python3-idna (2.10-1) ... Setting up python3-urllib3 (1.25.9-1) ... Setting up libxmuu1:amd64 (2:1.1.3-0ubuntu1) ... Setting up ncurses-term (6.2-1) ... Setting up openssh-client (1:8.3p1-1ubuntu0.1) ... Installing new version of config file /etc/ssh/ssh_config ... Setting up python3-requests (2.23.0+dfsg-2) ... Setting up xauth (1:1.1-0ubuntu1) ... Setting up openssh-sftp-server (1:8.3p1-1ubuntu0.1) ... Setting up openssh-server (1:8.3p1-1ubuntu0.1) ... Creating config file /etc/ssh/sshd_config with new version Creating SSH2 RSA key; this may take some time ... 3072 SHA256:Nns2cut2wNaEpuJxx/D4NXlGOxIun3BJat1KCUZy+6c root@jiangcheng01 (RSA) Creating SSH2 ECDSA key; this may take some time ... 256 SHA256:QNuoQ3ZzT4RLKDI7Vf0tSbUuTzqtwhbzvC6U6E9I/qU root@jiangcheng01 (ECDSA) Creating SSH2 ED25519 key; this may take some time ... 256 SHA256:bfXoP7YzJ9XjFpSFMqZB3gkuL3Ufl6aJp8EgnVvfqc4 root@jiangcheng01 (ED25519) Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service. Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/systemd/system/ssh.service. invoke-rc.d: could not determine current runlevel Setting up ssh-import-id (5.10-0ubuntu1) ... Attempting to convert /etc/ssh/ssh_import_id Processing triggers for ufw (0.36.1-4build1) ... Processing triggers for man-db (2.10.2-1) ... Processing triggers for libc-bin (2.35-0ubuntu3.1) ... root@jiangcheng01:~# root@jiangcheng01:~# service ssh restart * Restarting OpenBSD Secure Shell server sshd [ OK ]

5,再次验证本地ssh访问wsl2

C:\Users\jiangcheng> ssh root@xxx.xx.xxx.xx -p 22 The authenticity of host 'xxx.xx.xxx.xx (xxx.xx.xxx.xx)' can't be established. ED25519 key fingerprint is SHA256:bfXoP7YzJ9XjFpSFMqZB3gkuL3Ufl6aJp8EgnVvfqc4. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'xxx.xx.xxx.xx' (ED25519) to the list of known hosts. root@xxx.xx.xxx.xx's password: Permission denied, please try again. root@xxx.xx.xxx.xx's password: Permission denied, please try again. root@xxx.xx.xxx.xx's password: root@xxx.xx.xxx.xx: Permission denied (publickey,password).

6,解决 Permission denied (publickey,password) 问题

问题解决了,可以访问,如下图所示:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows Subsystem for Linux 2(WSL2)是Windows 10操作系统中的一个功能,它允许用户在Windows中运行Linux操作系统。对于那些熟悉Linux的开发人员来说,WSL2是一种非常方便的方式来使用他们所熟悉的命令行界面和工具。 在WSL2中,用户可以使用多种方法进行远程访问。其中,其中之一是通过ssh远程登录SSH(Secure Shell)是一种安全的网络协议,允许用户在不安全的网络上进行加密通信。通过ssh远程登录WSL2,用户可以像在本地一样使用WSL2,同时还可以利用Windows的GUI界面。 要使用ssh远程登录WSL2,用户需要先启用ssh服务器。在Windows PowerShell或命令提示符中,可以使用以下命令来启动ssh服务器: ``` sudo apt update sudo apt install openssh-server sudo service ssh start ``` 安装和启动过程完成后,可以使用ssh客户端连接到WSL2。在本地电脑上,打开一个终端窗口并输入以下命令: ``` ssh [username]@[IP address of WSL2] ``` 其中,[username]是WSL2中的用户名,[IP address of WSL2]是WSL2的IP地址。默认情况下,WSL2会动态分配IP地址,可以使用以下命令WSL2中查找其IP地址: ``` ip addr show eth0 | grep inet | awk '{print $2}' | cut -d / -f 1 ``` 使用这种方法进行远程访问,需要确保WSL2和Windows的防火墙设置正确。在Windows防火墙中,需要允许ssh流量通过。在WSL2中,需要允许ssh服务器通过WSL2的防火墙。可以使用以下命令配置WSL2防火墙: ``` sudo ufw allow ssh ``` 使用ssh远程登录WSL2时,还需要注意ssh客户端和服务端之间的证书问题。如果出现证书错误,需要检查证书是否正确配置。 总之,使用ssh远程登录WSL2是一种方便的方式,可让用户像在本地一样使用WSL2。通过配置Windows和WSL2的防火墙,并确保证书正确配置,用户可以安全、可靠地进行远程访问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值