Bash scripts are one area of Linux that help to make it the marvel of flexibility and efficiency that it is. I have used bash scripts for just about everything, from backups to user creation, and much more, but there is one area where bash scripts really shine -- automation.
Also: The first 5 Linux commands every new user should learn
You might be thinking this is too challenging for your skills, but you would be surprised at how easy it can actually be. What I am going to do is show you how to use bash scripts and the Linux terminal to update several Linux machines at once.
Are you ready for this? Let's do it.
What you will need: The only things you will need for this are at least two or three Linux machines (on the same LAN) and a user with sudo privileges. That is it.
Open a terminal window and create the script with the command:
nano updates.sh
Paste the following into the file:
#!/bin/bash
#Define your remote servers with comma-separated IP addresses
servers=("SERVER1", "SERVER2")#Define the update command (make sure to edit this to conform to your package manager)
update_command="sudo apt-get update && sudo apt-get upgrade -y"#Loop through each server and execute the update command
for server in "${servers[@]}"; do
echo "Updating server$server..."
ssh USER@$server "$update_command"
doneecho "Updates completed on all servers."
Where SERVER1 and SERVER2 are the IP addresses of the remote machines, and USER is your remote username.
I have added comments in the script to help explain what is going on.
Also: 5 Linux terminal apps better than your default
Save and close the file.
To run the script, you have to give it executable permissions with the command:
chmod u+x update.sh
In case you have not done this yet, we need to generate an SSH key pair because we will be using SSH key authentication. To create your key, issue the following command:
ssh-keygen -t rsa
You will be prompted to type and verify a password for the key. Make sure to do this.
For SSH key authentication to work, you have to copy the public key to all of the servers that will benefit from the script. To do that, issue the command:
ssh-copy-id USER@SERVER
Where USER is your remote username, and SERVER is the IP address for the first server.
Do the same thing for all the servers you will be connecting to from within the script.
For this to work successfully, you have to start an ssh-agent session, which temporarily saves your SSH key password so you do not have to type it for every server on the list. To start a session, issue the command
eval `ssh-agent`
Next, add your SSH key password with:
ssh-add ~/.ssh/id_rsa
You will be prompted for your SSH key password.
You can now run the script with the command:
Also: The best Linux distros for beginners
./update.sh
You will not be asked for your remote user password because you are in an ssh-agent session that can negotiate the password hand-off for you. It does not matter how many servers you have configured for the script; as long as they are aware of the SSH keypair, and you have added the password to the ssh-agent session, everything will go off without a hitch.
Also: How to generate random passwords from the Linux command line
You can now take this script and alter it however you need, or you can create your own scripts and use SSH key authentication and ssh-agent to simplify the automation process.