Posted on
Scripting for DevOps

Automating Server Configuration with Bash Scripts

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Automating server configuration with Bash scripts is an efficient way to ensure consistency, reduce manual effort, and streamline the provisioning of servers. Here’s a guide on how to do it effectively.


Steps for Automating Server Configuration with Bash Scripts

1. Define the Requirements

Before scripting, identify the configuration tasks:

  • Software installations

  • Service configurations

  • User and permission setups

  • Network configurations

  • Security settings


2. Prepare the Environment

  • Ensure the server has Bash installed (most Linux distributions come with it by default).

  • Have SSH access or another mechanism to run the scripts on the server.

  • Use sudo or root privileges if required for system-level tasks.


3. Create the Bash Script

Here’s a step-by-step approach:

a. Start with a Shebang

The shebang defines the script interpreter.

#!/bin/bash

b. Update the System

Update package lists and install updates.

sudo apt update && sudo apt upgrade -y   # For Debian/Ubuntu
sudo yum update -y                       # For CentOS/RHEL

c. Install Software Packages

Automate package installations.

sudo apt install -y nginx git curl       # Example for Debian-based systems

d. Configure Services

Write configurations directly or modify existing ones using tools like sed or echo.

Example: Configure NGINX

sudo tee /etc/nginx/sites-available/my-site <<EOF
server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.html;
}
EOF
sudo ln -s /etc/nginx/sites-available/my-site /etc/nginx/sites-enabled/
sudo systemctl restart nginx

e. Manage Users and Permissions

Create users, assign groups, and set permissions.

sudo useradd -m -s /bin/bash newuser
echo "newuser:password" | sudo chpasswd
sudo usermod -aG sudo newuser

f. Set Environment Variables

Configure global or user-specific environment variables.

echo "export MY_VAR=my_value" | sudo tee -a /etc/environment

g. Enable and Start Services

Ensure critical services start on boot and are running.

sudo systemctl enable nginx
sudo systemctl start nginx

h. Security Hardening

Apply basic security measures:

  • Configure the firewall:
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw enable
  • Disable root login via SSH:
sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sudo systemctl restart sshd

4. Test the Script

Run the script in a test environment before deploying it to production servers:

bash server-setup.sh

5. Make It Reusable

Enhance the script for flexibility:

  • Use variables for dynamic configurations:
APP_NAME="myapp"
sudo mkdir /var/www/$APP_NAME
  • Add input parameters:
#!/bin/bash
APP_NAME=$1
echo "Setting up $APP_NAME"

6. Error Handling

Improve script reliability by handling errors:

set -e   # Exit script on any error
set -u   # Treat unset variables as errors
set -o pipefail  # Catch errors in piped commands

if ! sudo apt update; then
    echo "Failed to update packages. Exiting."
    exit 1
fi

7. Integrate with Automation Tools

For larger-scale automation:

  • Use Bash in conjunction with Ansible, Terraform, or Chef for managing multiple servers.

  • Example: Use bash scripts as Ansible tasks.


Example: Full Bash Script for Basic Server Setup

#!/bin/bash

# Variables
HOSTNAME="my-server"
USER="admin"
PASSWORD="password"

# Set hostname
sudo hostnamectl set-hostname $HOSTNAME

# Update system
sudo apt update && sudo apt upgrade -y

# Install basic packages
sudo apt install -y nginx git curl

# Create a user
sudo useradd -m -s /bin/bash $USER
echo "$USER:$PASSWORD" | sudo chpasswd
sudo usermod -aG sudo $USER

# Configure firewall
sudo ufw allow OpenSSH
sudo ufw allow "Nginx Full"
sudo ufw enable

# Start services
sudo systemctl enable nginx
sudo systemctl start nginx

# Print completion message
echo "Server setup completed successfully."

8. Schedule or Automate Execution

Run the script automatically when a server boots:

  • Place the script in /etc/init.d/ or use systemd.

  • Use cloud-init or user data scripts for cloud servers.


By following these steps, you can automate server configurations efficiently, ensuring consistent and repeatable setups for your infrastructure.