codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐧

Linux & Bash

52 / 60 topics
39Advanced Scripting40Bash Arrays41Bash Associative Arrays42Advanced Functions43Advanced Script Debugging44Script Optimization45Automation with Scripts46Script Integration47Script Logging48Error Handling49Script Performance50Parallel Processing51Remote Execution52Configuration Management53Script Monitoring54Automation Tools55Continuous Integration56Script Deployment57Script Security58Script Audit59Optimization Tips60Advanced Debugging
Tutorials/Linux & Bash/Configuration Management
🐧Linux & Bash

Configuration Management

Updated 2026-05-15
10 min read

Configuration Management

Introduction

In the realm of system administration and software development, configuration management is a critical task. It involves ensuring that systems are consistently configured across different environments, from development to production. Bash scripts play a pivotal role in automating these tasks, making them efficient and error-free.

This tutorial will guide you through using Bash scripts for configuration management, covering everything from basic setup to advanced techniques. Whether you're a beginner looking to automate your first script or an intermediate developer seeking to enhance your existing tools, this guide has something for everyone.

Concept

Configuration management in the context of Bash scripting involves writing scripts that can:

  • Automate repetitive tasks: Such as setting up environments, installing software, and configuring services.
  • Ensure consistency: Across different machines and environments.
  • Handle errors gracefully: With robust error handling and logging mechanisms.
  • Be reusable: By creating modular scripts that can be easily integrated into larger workflows.

Bash scripts are particularly well-suited for configuration management due to their simplicity, power, and wide availability across Unix-like systems. They allow you to execute a series of commands in sequence, making it easy to automate complex tasks.

Examples

1. Basic Configuration Script

Let's start with a simple example of a Bash script that configures a basic web server environment on an Ubuntu system. This script will install Apache2 and set up a basic virtual host.

Step 1: Create the Script

First, create a new file named setup_webserver.sh:

Bash
1#!/bin/bash
2
3# Update package list
4sudo apt update
5
6# Install Apache2
7sudo apt install -y apache2
8
9# Enable Apache2 to start on boot
10sudo systemctl enable apache2
11
12# Start Apache2 service
13sudo systemctl start apache2
14
15# Create a basic virtual host configuration
16echo "<VirtualHost *:80>
17 ServerAdmin webmaster@localhost
18 DocumentRoot /var/www/html
19
20 ErrorLog ${APACHE_LOG_DIR}/error.log
21 CustomLog ${APACHE_LOG_DIR}/access.log combined
22</VirtualHost>" | sudo tee /etc/apache2/sites-available/000-default.conf
23
24# Restart Apache2 to apply changes
25sudo systemctl restart apache2

Step 2: Make the Script Executable

Before running the script, make sure it's executable:

Terminal
chmod +x setup_webserver.sh

Step 3: Execute the Script

Run the script using the following command:

Terminal
sudo ./setup_webserver.sh
Output
...
Setting up apache2 (2.4.41-4ubuntu3) ...
...
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
...
[ ok ] Restarting apache2 (via systemctl): apache2.service.

2. Advanced Configuration Script with Error Handling

For more robust configuration management, it's essential to include error handling in your scripts. Here's an enhanced version of the previous script that includes basic error checking:

Bash
1#!/bin/bash
2
3# Function to handle errors
4handle_error() {
5 echo "Error: $1"
6 exit 1
7}
8
9# Update package list
10sudo apt update || handle_error "Failed to update package list"
11
12# Install Apache2
13sudo apt install -y apache2 || handle_error "Failed to install Apache2"
14
15# Enable Apache2 to start on boot
16sudo systemctl enable apache2 || handle_error "Failed to enable Apache2 service"
17
18# Start Apache2 service
19sudo systemctl start apache2 || handle_error "Failed to start Apache2 service"
20
21# Create a basic virtual host configuration
22echo "<VirtualHost *:80>
23 ServerAdmin webmaster@localhost
24 DocumentRoot /var/www/html
25
26 ErrorLog ${APACHE_LOG_DIR}/error.log
27 CustomLog ${APACHE_LOG_DIR}/access.log combined
28</VirtualHost>" | sudo tee /etc/apache2/sites-available/000-default.conf || handle_error "Failed to create virtual host configuration"
29
30# Restart Apache2 to apply changes
31sudo systemctl restart apache2 || handle_error "Failed to restart Apache2 service"

3. Modular Configuration Scripts

For larger projects, it's beneficial to break down your scripts into smaller, reusable modules. This makes them easier to manage and maintain.

Example: Separate Modules for Installation and Configuration

Create two separate files: install_apache.sh and configure_apache.sh.

install_apache.sh

Bash
1#!/bin/bash
2
3# Function to handle errors
4handle_error() {
5 echo "Error: $1"
6 exit 1
7}
8
9# Update package list
10sudo apt update || handle_error "Failed to update package list"
11
12# Install Apache2
13sudo apt install -y apache2 || handle_error "Failed to install Apache2"
14
15# Enable Apache2 to start on boot
16sudo systemctl enable apache2 || handle_error "Failed to enable Apache2 service"
17
18# Start Apache2 service
19sudo systemctl start apache2 || handle_error "Failed to start Apache2 service"

configure_apache.sh

Bash
1#!/bin/bash
2
3# Function to handle errors
4handle_error() {
5 echo "Error: $1"
6 exit 1
7}
8
9# Create a basic virtual host configuration
10echo "<VirtualHost *:80>
11 ServerAdmin webmaster@localhost
12 DocumentRoot /var/www/html
13
14 ErrorLog ${APACHE_LOG_DIR}/error.log
15 CustomLog ${APACHE_LOG_DIR}/access.log combined
16</VirtualHost>" | sudo tee /etc/apache2/sites-available/000-default.conf || handle_error "Failed to create virtual host configuration"
17
18# Restart Apache2 to apply changes
19sudo systemctl restart apache2 || handle_error "Failed to restart Apache2 service"

Main Script

Create a main script setup_webserver.sh that calls these modules:

Bash
1#!/bin/bash
2
3# Run installation module
4./install_apache.sh
5
6# Run configuration module
7./configure_apache.sh

4. Logging Configuration Changes

Logging is crucial for tracking changes and diagnosing issues. You can enhance your scripts to log each step of the configuration process.

Example: Adding Logging

Modify setup_webserver.sh to include logging:

Bash
1#!/bin/bash
2
3# Log file
4LOG_FILE="/var/log/setup_webserver.log"
5
6# Function to handle errors and log them
7handle_error() {
8 echo "Error: $1" | tee -a $LOG_FILE
9 exit 1
10}
11
12# Function to log messages
13log_message() {
14 echo "$1" | tee -a $LOG_FILE
15}
16
17# Update package list
18log_message "Updating package list..."
19sudo apt update >> $LOG_FILE 2>&1 || handle_error "Failed to update package list"
20
21# Install Apache2
22log_message "Installing Apache2..."
23sudo apt install -y apache2 >> $LOG_FILE 2>&1 || handle_error "Failed to install Apache2"
24
25# Enable Apache2 to start on boot
26log_message "Enabling Apache2 service..."
27sudo systemctl enable apache2 >> $LOG_FILE 2>&1 || handle_error "Failed to enable Apache2 service"
28
29# Start Apache2 service
30log_message "Starting Apache2 service..."
31sudo systemctl start apache2 >> $LOG_FILE 2>&1 || handle_error "Failed to start Apache2 service"
32
33# Create a basic virtual host configuration
34log_message "Creating virtual host configuration..."
35echo "<VirtualHost *:80>
36 ServerAdmin webmaster@localhost
37 DocumentRoot /var/www/html
38
39 ErrorLog ${APACHE_LOG_DIR}/error.log
40 CustomLog ${APACHE_LOG_DIR}/access.log combined
41</VirtualHost>" | sudo tee /etc/apache2/sites-available/000-default.conf >> $LOG_FILE 2>&1 || handle_error "Failed to create virtual host configuration"
42
43# Restart Apache2 to apply changes
44log_message "Restarting Apache2 service..."
45sudo systemctl restart apache2 >> $LOG_FILE 2>&1 || handle_error "Failed to restart Apache2 service"
46
47log_message "Configuration completed successfully."

What's Next?

Now that you have a solid understanding of using Bash scripts for configuration management, the next step is to explore script monitoring. Monitoring your scripts can help you ensure they are running as expected and alert you to any issues promptly.

Stay tuned for more advanced topics on scripting and automation!


By following this guide, you should now be well-equipped to use Bash scripts for effective configuration management tasks. Remember to always test your scripts in a safe environment before deploying them to production systems. Happy scripting!


PreviousRemote ExecutionNext Script Monitoring

Recommended Gear

Remote ExecutionScript Monitoring