设置ssh免密码登录脚本(hadoop自动化部署脚本一)

时间:2021-12-25 15:28:56

设置ssh免密码登录脚本(hadoop自动化部署脚本一)

设置ssh免密码登录脚本(飞谷云大数据自动化部署脚本一)

1.#!/bin/sh
2.#important note:this script is expected to run on master node
3.PWD=hdp
4.USER=hdp
5.SSHDIR=/home/dhp/.ssh
6.
7.#create temp dir to store id_pub file
8.mkdir $SSHDIR/sshtmp
9.
10.HOST=(master slave01 slave02);
11.
12.for HOST in ${HOSTS[*]}
13.do
14. #clear files
15. sshpass -p $PWD ssh $USER@$HOST rm -f $SSHDIR/authorized_keys
16. sshpass -p $PWD ssh $USER@$HOST rm -f $SSHDIR/id_rsa
17. sshpass -p $PWD ssh $USER@$HOST rm -f $SSHDIR/id_rsa.pub
18.
19. #generatee key pair on "HOST"
20. sshpass -p $PWD ssh $USER@$HOST "ssh-keygen -t rsa -P \"\" -f \"$SSHDIR/id_rsa\""
21.
22. #copy pub key to master node collectively
23. sshpass -p $PWD ssh $USER@$HOST "sshpass -p $PWD scp $SSHDIR/id_rsa.pub $USR@master:$SSHDIR/sshtmp"
24.
25. cat $SSHDIR/sshtmp/id_rsa.pub >> $SSHDIR/authorized_keys
26.done
27.
28.#remove temporary dir
29.rm -fR $SSHDIR/sshtmp
30.
31.#must ensure the file permission
32.chmod 644 $SSHDIR/authorized_keys
33.
34.#distribute authorized_keys to all nodes
35.for HOST in ${HOSTS[*]}
36.do
37. sshpass -p $PWD scp $SSHDIR/authorized_keys $USR@$HOST:$SSHDIR
38.done