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! |
[[180050]] Telecoms.com has teamed up with mobile...
[[389300]] Not long ago, the market was hotly dis...
Introduction: At present, security technology is ...
At the Brazil Olympics, the 4×400 relay race is t...
Traditional data transmission methods mostly use ...
According to foreign media reports, NASA is upgra...
[[412406]] As an industry that deeply integrates ...
5G is a new generation of communication technolog...
VLAN (Virtual Local Area Network) is a data excha...
On November 27, the Ministry of Industry and Info...
According to IDC's Global Ethernet Switch and...
Software-defined networking (SDN) has become an i...
SpartanHost is a well-known foreign VPS hosting c...
PacificRack started selling the new Virtualizor p...
There has been a lot of news about HTTP/3 in rece...