SSH無密碼登入
阿新 • • 發佈:2021-07-07
Linux金鑰生成
公鑰: 相當於鎖
私鑰: 相當於鑰匙,防止丟失
Linux使用命令生成公私鑰:
ssh-keygen -f /root/.ssh/id_rsa -N ''
使用工具放置公鑰
ssh-copy-id 192.168.1.35
輸入密碼下發公鑰
SSH操作伺服器 ssh登入伺服器 -> 執行Shell命令 -> 執行完退出ssh 127.0.0.1 "echo sjgtest"
Shell批量部署示例
for ip in $(cat /root/sjghost);do echo $ip ssh ${ip} "echo deploying" done
Shell批量下發部署指令碼、部署包等
for ip in $(cat /root/sjghost);do echo $ip scp -r /tmp/sjg.log ${ip}:/usr/local/src/ done
Shell批量拉取檔案等
for ip in $(cat /root/sjghost);do echo $ip scp root@${ip}:/usr/local/src/sjg.log /tmp/sjg.log.${ip} done
ssh公鑰批量下發
ssh-copy-id下發公鑰 ssh-copy-id -i /root/.ssh/id_rsa.pub 127.0.0.1
sshpass命令可實現自動輸入密碼
yum install sshpass -y sshpass -p "xxx" ssh-copy-id -i /root/.ssh/id_rsa.pub xxx.xxx.xxx.xxx
批量下發公鑰實現指令碼deployrsa.sh
IFSBAK=$IFS IFS=$'\n' for line in $(cat /root/sjghost);do myhostname=$(echo "$line" | awk '{print $1}') myip=$(echo "$line" | awk '{print $2}') mypwd=$(echo "$line" | awk '{print $3}') sshpass -p "$mypwd" ssh-copy-id -i /root/.ssh/id_rsa.pub $myip done IFS=$IFSBAK