How I automate basic tasks on Linux with bash scripts - and why you should try it (2025)

How I automate basic tasks on Linux with bash scripts - and why you should try it (1)

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.

How to use bash scripts to automate tasks on Linux

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.

1. Write the script

Open a terminal window and create the script with the command:

Show more

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"
done

echo "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.

2. Give the script executable permissions

To run the script, you have to give it executable permissions with the command:

Show more

chmod u+x update.sh

3. Generate an SSH key pair

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:

Show more

ssh-keygen -t rsa

You will be prompted to type and verify a password for the key. Make sure to do this.

4. Copy your public key to the remote machines

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:

Show more

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.

5. Run ssh-agent

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

Show more

eval `ssh-agent`

Next, add your SSH key password with:

ssh-add ~/.ssh/id_rsa

You will be prompted for your SSH key password.

6. Run the script

You can now run the script with the command:

Also:The best Linux distros for beginners

Show more

./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.

Open Source

  • The open-source tools that could disrupt the entire IT incident management market
  • This Linux distro could let your old laptop 'shine on' after Windows 10's sunset
  • 6 Linux myths, busted
  • 5 lightweight Linux distributions with very low system requirements
Editorial standards

Show Comments

How I automate basic tasks on Linux with bash scripts - and why you should try it (2025)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Eusebia Nader

Last Updated:

Views: 6044

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.