[root@qy-clicent ~]# ssh-keygen -t rsa //使用 ssh-keygen 创建公钥-私钥对
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:roUVEiCF9wH2YqxcjAVxadhXSEBpx0xAqSHHkqbd8Fg root@qy-clicent
The key's randomart image is:
+---[RSA 2048]----+
| o+X%&+o. |
|+.BOE.B. |
|o=.%*+o.. |
|..++o... . |
| o S |
| + |
| . o |
| o |
| . |
+----[SHA256]-----+
[root@qy-clicent ~]# ls .ssh/ 查看生成的公钥
id_rsa id_rsa.pub
[root@qy-clicent ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.69.134 //将公钥复制到远程系统上的正确位置
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.69.134 (192.168.69.134)' can't be established.
ECDSA key fingerprint is SHA256:+QHBnIuUCWJcNRAaDYncL/7+fjLKQszXzjEhQdvjgL4.
ECDSA key fingerprint is MD5:fa:38:e6:82:20:60:6d:9f:78:cd:76:98:95:95:72:2e.
Are you sure you want to continue connecting (yes/no)? yes
/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.69.134's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.69.134'"
and check to make sure that only the key(s) you wanted were added.
[root@qy-clicent ~]# ssh root@192.168.69.134
Last login: Mon Jan 7 11:20:02 2019 from 192.168.69.1
[root@qy-server ~]# ip a \\验证ip远程登录成功
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:4a:d8:ba brd ff:ff:ff:ff:ff:ff
inet 192.168.69.134/24 brd 192.168.69.255 scope global noprefixroute ens33
[root@qy-clicent ~]# ssh root@192.168.69.134 "touch /root/abc" \\远程操作动作等等
[root@qy-server ~]# ls
abc anaconda-ks.cfg
cat /server/scripts/fenfa_sshkey.sh
#!/bin/bash
. /etc/init.d/functions
for i in 120 129
do
expect fenfa_sshkey.exp ~/.ssh/id_dsa.pub 192.168.43 .$i >/dev/null 2>&1
if [ $? -eq 0 ]
then
action "$i" /bin/true
else
action "$i" /bin/false
fi
done
[root@localhost ~]# vi /etc/hosts.allow
sshd: 192.168.69.134/32 \\最后一行添加
[root@localhost ~]# vi /etc/hosts.deny
ALL: all \\ \\最后一行添加
第二步验证
客户端1登陆验证成功
[root@localhost ~]# ssh root@192.168.69.131 \\登陆成功
The authenticity of host '192.168.69.131 (192.168.69.131)' can't be established.
ECDSA key fingerprint is SHA256:PkvJs3SbQ+TiawuXQxrBkZ7EgZzjGxNQj61GeKcus+k.
客户端2登陆失败
[root@localhost ~]# ssh root@192.168.69.131
ssh_exchange_identification: read: Connection reset by peer