设置免密登录时提示连接失败

[lj@localhost .ssh]$ ssh-copy-id lj@192.168.145.129
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/bin/ssh-copy-id: ERROR: ssh: connect to host 192.168.145.129 port 22: Connection refused

查看22号端口是否开启ss -lnt ,结果是未开启

lj@ubuntu .ssh % ss -lnt                                        [0]
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process 
LISTEN 0      4096   127.0.0.53%lo:53          0.0.0.0:*            
LISTEN 0      5          127.0.0.1:631         0.0.0.0:*            
LISTEN 0      5              [::1]:631            [::]:*            
lj@ubuntu .ssh % lsof -i:22                                     [0]

察觉到可能是SSH服务未安装因此进行下载安装sudo apt-get install openssh-server openssh-client

lj@ubuntu .ssh % sudo apt-get install openssh-server openssh-client   
[sudo] lj 的密码: 
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
openssh-client 已经是最新版 (1:8.2p1-4ubuntu0.5)。
openssh-client 已设置为手动安装。
下列软件包是自动安装的并且现在不需要了:
  libfprint-2-tod1 libfwupdplugin1 libllvm10
使用'sudo apt autoremove'来卸载它(它们)。
将会同时安装下列软件:
  ncurses-term openssh-sftp-server ssh-import-id
建议安装:
  molly-guard monkeysphere ssh-askpass
下列【新】软件包将被安装:
  ncurses-term openssh-server openssh-sftp-server ssh-import-id
升级了 0 个软件包,新安装了 4 个软件包,要卸载 0 个软件包,有 9 个软件包未被升级。
需要下载 688 kB 的归档。
解压缩后会消耗 6,010 kB 的额外空间。
您希望继续执行吗? [Y/n] y
获取:1 http://ftp.sjtu.edu.cn/ubuntu focal/main amd64 ncurses-term all 6.2-0ubuntu2 [249 kB]
获取:2 http://ftp.sjtu.edu.cn/ubuntu focal-updates/main amd64 openssh-sftp-server amd64 1:8.2p1-4ubuntu0.5 [51.5 kB]
获取:3 http://ftp.sjtu.edu.cn/ubuntu focal-updates/main amd64 openssh-server amd64 1:8.2p1-4ubuntu0.5 [377 kB]
获取:4 http://ftp.sjtu.edu.cn/ubuntu focal/main amd64 ssh-import-id all 5.10-0ubuntu1 [10.0 kB]
已下载 688 kB,耗时 10(65.5 kB/s)
正在预设定软件包 ...
正在选中未选择的软件包 ncurses-term。
(正在读取数据库 ... 系统当前共安装有 170245 个文件和目录。)
准备解压 .../ncurses-term_6.2-0ubuntu2_all.deb  ...
正在解压 ncurses-term (6.2-0ubuntu2) ...
正在选中未选择的软件包 openssh-sftp-server。
准备解压 .../openssh-sftp-server_1%3a8.2p1-4ubuntu0.5_amd64.deb  ...
正在解压 openssh-sftp-server (1:8.2p1-4ubuntu0.5) ...
正在选中未选择的软件包 openssh-server。
准备解压 .../openssh-server_1%3a8.2p1-4ubuntu0.5_amd64.deb  ...
正在解压 openssh-server (1:8.2p1-4ubuntu0.5) ...
正在选中未选择的软件包 ssh-import-id。
准备解压 .../ssh-import-id_5.10-0ubuntu1_all.deb  ...
正在解压 ssh-import-id (5.10-0ubuntu1) ...
正在设置 openssh-sftp-server (1:8.2p1-4ubuntu0.5) ...
正在设置 openssh-server (1:8.2p1-4ubuntu0.5) ...

Creating config file /etc/ssh/sshd_config with new version
Creating SSH2 RSA key; this may take some time ...
3072 SHA256:1j46TJASEI//oOKLmgAcI8vQnJWxZHemllI/kqPXkEc root@ubuntu (RSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:l6Stjl1ZVKPuTmvFA6uLMEeaaUHjR9Dx4xPDLWFqzVQ root@ubuntu (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:7h0kKmUYhxTP65ZEIdrAWGCxZb7x5tWXPdFR4tWJ6/w root@ubuntu (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.
rescue-ssh.target is a disabled or a static unit, not starting it.
正在设置 ssh-import-id (5.10-0ubuntu1) ...
Attempting to convert /etc/ssh/ssh_import_id
正在设置 ncurses-term (6.2-0ubuntu2) ...
正在处理用于 systemd (245.4-4ubuntu3.17) 的触发器 ...
正在处理用于 man-db (2.9.1-1) 的触发器 ...
正在处理用于 ufw (0.36-6ubuntu1) 的触发器 ...

再次进行查询,安装之后22端口已经打开

lj@ubuntu .ssh % lsof -i:22                                     [0]
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ssh     11870   lj    3u  IPv4 137835      0t0  TCP ubuntu:50830->ubuntu:ssh (ESTABLISHED)
lj@ubuntu .ssh % ss -lnt                                        [0]
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process 
LISTEN 0      4096   127.0.0.53%lo:53          0.0.0.0:*            
LISTEN 0      128          0.0.0.0:22          0.0.0.0:*            
LISTEN 0      5          127.0.0.1:631         0.0.0.0:*            
LISTEN 0      128             [::]:22             [::]:*            
LISTEN 0      5              [::1]:631            [::]:*

再次运行,成功

[lj@localhost ~]$ ssh-copy-id lj@192.168.145.129
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/lj/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
		(if you think this is a mistake, you may want to use -f option)


Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐