During the operation and maintenance of a large project, maintenance engineer Xiao Li needs to perform a routine system update task on dozens of Linux servers. In order to ensure the efficient completion of the task, he needs to implement SSH password-free login between all target servers, so as to avoid manually entering the password every time to log in and improve work efficiency. However, manually configuring password-free login for each server is not only time-consuming, but also prone to errors. To solve this problem, Xiao Li decided to write a Shell script to automatically configure SSH password-free login between all target servers in batches. This article will detail how to achieve this goal through Shell scripts to help operation and maintenance personnel easily handle similar batch management tasks. Implementation ideas
ScriptingThis shell script is used to distribute SSH public keys to multiple target hosts in batches to achieve password-free login. The script first checks the necessary dependencies and inputs, then iterates over the target host list and copies the locally generated SSH public key to each target host. (1) Set the public key file path Defines the path of the local public key file. The default path is ~/.ssh/id_rsa.pub. (2) Check whether sshpass is installed Use command -v to check whether the sshpass tool is installed in the system. If not, prompt the user to install it and exit the script. (3) Set the target host list file and check whether the file exists Assign the first command line argument (the target host list file) to the host_list_file variable. (4) Read the target host list and distribute the public key Use a while loop to read the target host list file line by line, read a host address each time and assign it to the host variable. (5) Generate an SSH key pair
Check whether the SSH private key file ~/.ssh/id_rsa already exists locally. If not, generate a new SSH key pair. (6) Use sshpass and ssh-copy-id to copy the public key to the target host Use the sshpass tool to provide the password, and use the ssh-copy-id command to copy the local public key to the ~/.ssh/authorized_keys file of the target host. Specify the SSH port and the target host address. Script UsageFour test machines are prepared here, as shown below: Create a new file named deploy_ssh_keys.sh on the jump server, copy the script content into it, and then save and exit. As shown below: Then, grant execution permissions to the script and execute the following command: Next, create a new file named ip.txt to store the IP list. The content format is as follows: Run the following command to execute the script file After successful execution, the following information will be output. Finally, I found a machine on the jump server to test it, and the input results are as follows: in conclusionThrough the above steps and Shell scripts, you can easily implement SSH password-free login between multiple Linux servers. This method is suitable for scenarios that require batch management and automated operation and maintenance, and can greatly improve work efficiency. I hope this tutorial is helpful to you! |
South Korea is expected to launch 5G (fifth-gener...
Yecao Cloud is a Chinese hosting company founded ...
iWebFusion (also known as iWFHosting) is a subsid...
Hey! Dear friends, long time no see. Today I brin...
Maximum distance of data transmission via network...
[[222946]] Activities surrounding "3.15"...
[[421462]] The convergence of IoT and 5G is a gam...
LoRa will lead the growth of non-cellular low-pow...
MIMO technology has continued to evolve since its...
[51CTO.com original article] In March this year, ...
The tribe shared information about IKIHOST last y...
【51CTO.com Quick Translation】 "I didn't ...
Regarding the Internet of Things, the importance ...
[51CTO.com original article] Expert introduction:...
As 2017 is coming to an end, many companies are p...