[root@rsyslog ~]# ssh-keygen -t rsa -C "root@zhangsan" 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: c8:bd:5d:3b:a5:d9:6d:09:b6:5f:db:55:1d:43:96:3d root@zhangsan The key's randomart image is: +--[ RSA 2048]----+ | oo| | oE.| | o.| | . o +| | o S .o. o| | o ..*o.o| | . . =...=| | ...=| | o.| +-----------------+
-t 加密算法
-C 注释 (加上这个也是为了最后进行对服务器访问人员进行辨别的一个关键点)
将公钥上传到服务器上的.ssh/authorized_keys 文件中。ssh-copy-id 命令会自动在服务器上创建.ssh/authorized_keys文件,即使该目录不存在,并自动赋予600权限。
[root@rsyslog ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.30.72 root@192.168.30.72's password: Now try logging into the machine, with "ssh 'root@192.168.30.72'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
4.3 client 2(192.168.30.71) 上同样的操作,用户lisi
[root@swift3 ~]# ssh-keygen -t rsa -C "root@lisi" Generating public/private rsa key pair.
Enterfile in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (emptyfor no passphrase):
Enter same passphrase again:
Your identification has been savedin /root/.ssh/id_rsa.
Your public key has been savedin /root/.ssh/id_rsa.pub.
The key fingerprint is:
8f:56:66:91:0c:6e:86:3b:90:19:42:9c:ab:9e:00:f6 root@lisi
The key's randomart image is:
+--[ RSA 2048]----+
| oo.. . |
| o. + o o . |
| .+ . + + |
|... . + . |
|o.. o S + |
|o E . * |
|o . o . |
| o . |
| |
+-----------------+
上传公钥到服务器上
[root@swift3 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.30.72 The authenticity of host '192.168.30.72 (192.168.30.72)' can't be established. RSA key fingerprint is 8f:a7:1b:8d:e4:92:ad:ae:ea:1b:fb:67:0b:0b:7c:ac. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.30.72' (RSA) to the list of known hosts. root@192.168.30.72's password: Now try logging into the machine, with "ssh 'root@192.168.30.72'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
4.4 现在去服务器上验证下该文件。
[root@open1 ~]# cat ~/.ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6fM+bpWEP3luauvOjmTB55ugUzVVMesmHCw4RNZ/C2e+KGXAYuxuAmEBbMcXQQj7OTAqVCQ0PWja58wReyZ7etiUGAtvoSBmSBpTPXteBMl40kDn4GdmXQ9UT/jnQ9gSZUQYJLMLJGWJks9S4xUI2cZ7oIytclrsUnKuOA1U6+luIJwJu9z7ya5OXh5FmmJQFnYtAEIhrLt4S8Ru5S00c0jiQCRk3RFlHYNc0IR02MXMH7d9bq7l04heAcT/y1EBS3EwINX8r0y6OridjJPCwxnm1sSfMKvLAbq/B+ufDjEOp7Y2SatL3qXaiP7NxdnhoJ4+Xar0zCoYi2A9oRGgUQ== root@zhangsan ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAupGSgXOWpQfzOVkHXYqN2BjMiAyaFRdKs6Wam3xGpMYRjZbIFX14kNR4CbrQtbUK8YonZPYdXG589blFmqF17sUPCNEqZEjCNer+yzDu+hYg/jAn4dCVtTBqUtBsTYUHSHIR0srruJ9keHNgU9aDRok8nulMUi/9Ej0NJZsBQ2npVNCf0YHgAd/ON5VsBYVLPvAT/cG3MuCjg5mgtU59qgAHyLKxkfpVc0/TRZ4eamX/1V0dsCxx9oYDbpn4YKLBAOaAS4kF6qEdrwRh0ssyWtWOo/CdyfLXKgwdbPtPfWZ63SM7wY7bAtcdxxu/QDkYVP+4oDfAtMxXZlY2bT5qMQ== root@lisi
View Code现在上面两个客户端已经可以免密钥登陆了,现在去服务器上配置,并创建脚本。
五、配置服务器
5.1 在log目录下创建一个 keys 文件,用于登陆时存进去公钥,之后对其进行取出判断的
[root@open1 ~]# touch /var/log/keys
创建检测脚本,内容如下: