How to Automate Linux Configuration: Using sed and Bash to Modify System Files


2 views

When automating Linux server setup, one of the most common tasks is modifying configuration files. Manual editing becomes impractical when deploying multiple servers or creating reproducible setups. The challenge lies in performing precise, atomic modifications without breaking existing configurations.

For simple substitutions, sed's in-place editing works perfectly. For example, to change enabled status:

sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/remi.repo

To target changes within specific sections (like [epel]), we need more sophisticated patterns. Here's how to add priority after enabled=1 in the [epel] section:

sed -i '/$$epel$$/,/^\[/ {/enabled=1/a priority=10' /etc/yum.repos.d/epel.repo

When dealing with quoted values like Testing = "0", we need to escape the quotes:

sed -i 's/Testing = "0"/Testing = "1"/g' config.conf

To add content at the end of a file (including quotes):

echo '\"Thanks Quanta\"' >> file.txt

For replacing entire lines matching a pattern (like worker_processes):

sed -i '0,/worker_processes/ s/worker_processes .*/worker_processes 4;/' nginx.conf

When you only know part of the line (like listen parameter):

sed -i 's/listen = .*/listen = \/tmp\/php5-fpm.sock;/' php-fpm.conf

Always create backups before running sed commands:

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sed -i.bak 's/old/new/' file  # Alternative backup method

Test commands without -i first to verify changes:

sed '/\[epel\]/,/^\[/ {/enabled=1/a priority=10' /etc/yum.repos.d/epel.repo

When automating Linux server setup, sed (stream editor) is far more efficient than interactive editors like nano for several reasons:

  • Non-interactive operation perfect for scripting
  • Precise text manipulation capabilities
  • In-place file modification with backup options

To safely modify the EPEL repository configuration while only affecting the [epel] section:

sed -i '/$$epel$$/,/^$$/ s/enabled=1/enabled=1\npriority=10/' /etc/yum.repos.d/epel.repo

This command:

  • Operates only between [epel] and next section header
  • Finds enabled=1 line and appends priority=10 after it
  • Preserves all other repository configurations

For your specific requirements, here are the exact solutions:

1. Changing Nginx worker_processes

sed -i '0,/worker_processes/{
  /worker_processes/ {
    s/worker_processes .*/worker_processes 4;/
  }
}' /etc/nginx/nginx.conf

2. Safe Modification of Repository Settings

sed -i '/\[remi$$/,/^\[/ s/enabled=0/enabled=1/' /etc/yum.repos.d/remi.repo

3. Handling Quoted Values

sed -i 's/Testing = "0"/Testing = "1"/' /path/to/file.conf

4. Appending Text to Files

sed -i '$a\"Thanks Quanta\"' /path/to/file

5. Modifying PHP-FPM Socket Path

sed -i 's/listen = .*/listen = \/tmp\/php5-fpm.sock;/' /etc/php5/fpm/pool.d/www.conf
  • Always test with -i.bak first to create backups
  • Use more specific patterns when possible (/^worker_processes/)
  • Consider using awk for more complex multi-line operations
  • Chain multiple sed commands with -e when needed
#!/bin/bash

# EPEL Repository Configuration
sed -i '/\[epel\]/,/^\[/ {
  s/enabled=0/enabled=1/
  s/enabled=1/enabled=1\npriority=10/
}' /etc/yum.repos.d/epel.repo

# Nginx Optimization
sed -i '0,/worker_processes/{
  /worker_processes/ {
    s/worker_processes .*/worker_processes 4;/
  }
}' /etc/nginx/nginx.conf

# PHP-FPM Configuration
sed -i 's/listen = .*/listen = \/tmp\/php5-fpm.sock;/' /etc/php5/fpm/pool.d/www.conf

# Append custom message
sed -i '$a\"Server configuration completed successfully\"' /root/setup.log