配置基于SSH密钥的身份认证
目标
配置并使用ssh密钥的身份认证,在远程客户端执行命令。
实操演练
- 登陆远程服务器
命令:ssh
语法:ssh 主机名称@主机地址
-p 选项指定远程端口,如下:
ssh -p 77 root@192.168.1.118
[root@sc-test program]# ssh root@192.168.1.118
The authenticity of host '192.168.1.118 (192.168.1.118)' can't be established.
ECDSA key fingerprint is SHA256:hcoO3E+RZMh7+4IlMmZ4yYO64qY0AejRlAlmKUDKMqo.
ECDSA key fingerprint is MD5:92:b8:8f:db:36:fd:64:3e:68:b5:40:4f:d6:b2:c0:b9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.118' (ECDSA) to the list of known hosts.
root@192.168.1.118's password:
Last login: Wed Feb 9 17:05:16 2022 from 192.168.1.117
##登陆远程服务器
客户端登陆访问的时候会提示保存公钥在本地。在以下文件:用户家目录的./ssh/known_hosts
如下:
[root@sc-test ~]# cat .ssh/known_hosts
192.168.1.118 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBId/rVvzc22Bl56CFTRs32bLgZJFSVWdo/7RD3by0bWcF1XMIHJY5T2RcQ1SuFWsCipNW3HBFPGO+4UZjlj//KU=- 远程执行ssh命令
命令:ssh
语法:ssh 主机名称@主机地址 双引号将命令括起来
ssh root@192.168.1.118 “date”
[root@sc-test ~]# ssh root@192.168.1.118 "date"
root@192.168.1.118's password:
2022年 02月 10日 星期四 14:45:06 CST
##查看远程主机的时间- 配置远程免密码登陆
实验步骤
1.客户端生成公私钥密钥对
2.将生成的密钥对复制到服务端的登陆用户家目录下使用ssh-cypy-id
3.登陆测试
命令:ssh-copy-id、ssh-keygen
[root@sc-test ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:0RAoMzE4q5Dojd4CKZRJd3DvRDoDmt16RprDxJzuutk root@sc-test
The key's randomart image is:
+---[RSA 2048]----+
| ++o oo. |
| .O.Oo= o |
|o++O.X o. . |
|++= = = . |
|+ooB o .S |
|=o..+ |
|+ .. |
| o+. |
| +oE |
+----[SHA256]-----+
##使用ssh-keygen命令创建ssh公私密钥对
[root@sc-test ~]# ssh-copy-id root@192.168.1.118
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.1.118's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.1.118'"
and check to make sure that only the key(s) you wanted were added.
##验证
[root@sc-test ~]# ssh root@192.168.1.118
Last failed login: Thu Feb 10 14:44:49 CST 2022 from 192.168.1.232 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Thu Feb 10 14:33:04 2022 from 192.168.1.232
文档更新时间: 2022-02-10 15:02 作者:xiubao yan