SSH configuration password free login manual version and script version

Time:2020-1-13

In the actual development process, if the number of clusters is too large, it will be very troublesome to input the password frequently for cluster building and transmission. Therefore, we will generally configure the cluster with password free login. Take a machine as an example to share the manual configuration of password free login and a script configuration of password free login

Environmental preparation

Add cluster IP map

Cluster machines should be configured
vim /etc/hosts

SSH configuration password free login manual version and script version

Configure password free login manually

Generate SSH login free key

Execute at each node
Cd ~ /. SSH enter my home directory
If it is a newly created server or virtual machine, it may report an error
**[[email protected] ~]# cd ~/.ssh
-bash: cd: /root/.ssh: No such file or directory**
For the solution, see another article (https://segmentfault.com/a/119000019919016? Share_user = 1030000019541827)
Then execute the generate key command
SSH keygen - t RSA (four carriage returns)
After executing this command, two files will be generated: id_rsa (private key) and id_rsa.pub (public key)

SSH configuration password free login manual version and script version

Copy the public key to the login free machine
Execute in node1, node2 and node3 respectively
ssh-copy-id -i ~/.ssh/id_rsa.pub node1

SSH configuration password free login manual version and script version

Execute on node1
ssh-copy-id -i ~/.ssh/id_rsa.pub node1
ssh-copy-id -i ~/.ssh/id_rsa.pub node2
ssh-copy-id -i ~/.ssh/id_rsa.pub node3
So that each node has the secret keys of other nodes (the process is the same as the above figure, so there is no screenshot)

Verification

Whether to log in directly when executing the command SSH node2 at node1:
Mutual verification required
node1->node1
node1->node2
node2->node1
node2->node2
node1->node3
node3->node3
node3->node1
node2->node3
node3->node2
As shown in the figure below, all cases will not be verified one by one

SSH configuration password free login manual version and script version

Script configuration password free login

Sshfreelogin.sh script code
#! /bin/bash
function sshFreeLogin()
{
 #1. Check whether the expect service exists. If not, use Yum to install expect
 expectIsExists=`rpm -qa | grep expect` 
 if [ -z $expectIsExists ]
 then
      yum -y install expect
 fi
 #2. Create a key if the key pair does not exist
 [ ! -f /root/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa
 while read line;do
       #Extract IP in file
       hostname=`echo $line | cut -d " " -f2`
       #Extract user name from file
       user_name=`echo $line | cut -d " " -f3`
       #Extract password from file
       pass_word=`echo $line | cut -d " " -f4`          
       expect <<EOF
              #Copy public key to target host
              spawn ssh-copy-id $hostname
              expect {
                      #Expect to input password automatically
                      "yes/no" { send "yes\n";exp_continue } 
                      "password" { send "$pass_word\n";exp_continue }
                      eof
              }
EOF
 #Read the file where IP is stored, and the directory address where the host? IP file is located
 done < /home/hadoop/automaticDeploy/host_ip.txt
}
sshFreeLogin
Host_ip.txt configuration
192.168.31.35 node1 root 123456
192.168.31.36 node2 root 123456
192.168.31.37 node3 root 123456

Corresponding IP host name, user name and password respectively
After the script configuration is completed, it can be executed directly. The script can increase the number of cluster machines at will. However, the cluster that can only execute the script temporarily can log in to all other machines without password