1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
## 测试.ssh目录文件是否存在,否则创建之
826 [ -d ~/. ssh ] || mkdir -m 600 ~/. ssh
## 查看权限
827 ls -dl ~/. ssh
## 获取文件名
# fgrep "auth" /etc/ssh/sshd_config
## 查看authorized_keys是否存在,否则创建之
828 [ -f ~/. ssh /authorized_keys ] || touch ~/. ssh /authorized_keys
## 设定其权限
829 chmod 0600 ~/. ssh /authorized_keys
## 查看权限
830 ls -l ~/. ssh /authorized_keys
## IO重定向追加至文件中(公钥在远程主机,私钥在本地主机。远程利用公钥加密一个数据,看本地主机能否解密,能则通过登陆系统。否则,....)
831 echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAGEAp9S/ev+YWkiEnAO6mziN2JOO21YbHK0I2fHwaIFW83OMFAWk9Bc+Kv0F6WkZ8ZW0dfgU0iv0W0SzJAc8gsedE6e1R3ALr7oEfhhvGk8+3/Bb22Ml2GqRgPJo8htOUvx5" >> ~/. ssh /authorized_keys
## 查看文件中的内容
832 cat ~/. ssh /authorized_keys
|
脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/bin/bash # Version: 0.0.12 # Author: Lcc.org # Description: Small toys [ -d ~/. ssh ] || install -d -m 700 ~/. ssh
[ -f ~/. ssh /authorized_keys ] || touch ~/. ssh /authorized_keys
Perm=$( ls -l ~/. ssh /authorized_keys | cut -d ' ' -f1)
[ "$Perm" != "-rw-------" ] && chmod 0600 ~/. ssh /authorized_keys
read -p "Enter a pubkey: " pubKey
[ -n "$pubKey" ] && [[ "$pubKey" =~ ^ ssh -rsa.*AAAA ]] && echo "$pubKey" >> ~/. ssh /authorized_keys
|
公钥来源:
1、本地主机上生成公钥和私钥,将公钥追加至远程主机的 ~/.ssh/authorized_keys文件中
2、非本地生成公钥和私钥,将公钥追加至远程主机的 ~/.ssh/authorized_keys文件中,私钥放于本地
生成密钥方法:
1、本地
2、linux主机
linux主机:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
ssh -keygen [OPTIONS...]
-b bits 指定密钥长度。对于RSA密钥,768,1024,2048,4096,8192
-f filename 指定密钥文件名。 -t type 指定要创建的密钥类型。可以使用: "rsa1" (SSH-1) "rsa" (SSH-2) "dsa" (SSH-2)
-P passphrase 对私钥加密 1、生成密钥 [root@localhost ~] # ssh-keygen -b 768 -f /tmp/primarykey -t rsa -P ''
Generating public /private rsa key pair.
Your identification has been saved in /tmp/primarykey .
Your public key has been saved in /tmp/primarykey .pub.
The key fingerprint is: 00:4f:93:1c:d5:99:cb:b6:10:d8:4d:71:62:3e:bb:80 root@localhost.localdomain The key's randomart image is: +--[ RSA 768]----+ | ..+*.+=+. | | ++.oo=o | | o oo. | | o. +o | | E So.. | | ... | | . | | | | | +-----------------+ 2、查看 [root@localhost ~] # ls /tmp/primarykey*
/tmp/primarykey /tmp/primarykey .pub
3、导入公钥至~/. ssh /authorized_keys
[root@localhost ~] # cat /tmp/primarykey.pub >> ~/.ssh/authorized_keys
4、将私钥导入至本地主机 5、登陆 |
本文转自 lccnx 51CTO博客,原文链接:http://blog.51cto.com/sonlich/1955070,如需转载请自行联系原作者