1、非交互式创建秘钥对
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
ssh-keygen秘钥对参数:
[-P old_passphrase]:接密码
[-f output_keyfile]:输出的秘钥文件
[-q]:不输出信息
[-t dsa]:制定秘钥类型
2、分发秘钥文件
ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
(不输入回车,分发秘钥文件)
3、sshpass工具分发秘钥文件
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
yum install sshpass -y
[root@aaa ~]# sshpass -help
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
-f filename Take password to use from file
-d number Use number as file descriptor for getting password
-p password Provide password as argument (security unwise) #指定用户密码操作
-e Password is passed as env-var "SSHPASS"
With no parameters - password will be taken from stdin
-P prompt Which string should sshpass search for to detect a password prompt
-v Be verbose about what you're doing
-h Show help (this screen)
-V Print version information
At most one of -f, -d, -p or -e should be used
4、利用脚本实现:
[root@aaa .ssh]# cat /server/script/ssh-copy.sh
#!/bin/bash
#yum install sshpass -y
ssh-keygen -f ~/.ssh/id_rsa -P '' -q #非交互式创建秘钥对(公钥)
for ip in 3
do
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 123.57.220.$ip
done