本地主机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) 问题
问题解决了,可以访问,如下图所示: