System administrators frequently manage multiple text-based configuration files in Linux, located under /etc/
and application directories. Editing each manually can be time-consuming, especially on large-scale systems.
That’s why advanced Linux users use sed and awk to modify config files in Linux — two powerful command-line utilities that automate text editing, pattern searching, and data extraction.
With these tools, you can perform complex edits like updating parameters, extracting values, or inserting new directives — all without opening a text editor. This guide will teach you exactly how to use sed and awk effectively to modify configuration files in Linux.
What are sed
and awk
in Linux?
What is sed?
sed
stands for Stream Editor. It reads text line-by-line, performs operations like search, replace, insert, or delete, and outputs the modified text. With the -i
flag, it can edit files directly — a must-have for config automation.
Example:
sed -i 's/old_value/new_value/' /path/to/config.conf
What is awk?
awk
is a pattern scanning and text processing language. It works with columns and fields, allowing you to extract, modify, or summarize structured text like key-value pairs or logs.
Example:
awk '$1 == "KeyName" { print $2 }' /path/to/config.conf
Both tools are part of almost every Linux distribution by default, making them ideal for scripting and DevOps tasks.
Why Use sed and awk to Modify Config Files in Linux
There are many reasons system administrators prefer these utilities:
Automation: Run bulk updates across multiple files or systems.
Precision: Modify only specific lines or parameters without touching others.
Efficiency: Avoid manual editing in editors like
vi
ornano
.Repeatability: Include them in scripts for consistent, repeatable results.
Let’s explore real-world examples.
Example 1: Updating a Configuration Parameter with sed
Suppose you need to change the ListenPort
value in /etc/ssh/sshd_config
. Instead of manually searching for it, use this one-liner:
sed -i 's/^#\?ListenPort.*/ListenPort 2222/' /etc/ssh/sshd_config
Explanation:
-i
— edits the file in-place.^#\?
— matches commented or uncommented lines.ListenPort.*
— captures any existing value.The command replaces the entire line with the new setting
ListenPort 2222
.
Even if the directive was commented out, it gets cleanly updated.
Example 2: Extracting Configuration Values Using awk
Let’s say you want to check whether root login is allowed in SSH.
awk '$1 == "PermitRootLogin" { print $2 }' /etc/ssh/sshd_config
What it does:
$1 == "PermitRootLogin"
— matches lines where the first word is the directive.{ print $2 }
— prints the second field, which is the assigned value (yes
,no
, orprohibit-password
).
This command is extremely useful for configuration audits and monitoring scripts.
Example 3: Removing an Unwanted Parameter Using sed
If a deprecated option such as UseDNS
is present and needs removal:
To list all currently running services:
awk '$2 == "running" { print $1 }' /etc/myapp/services.conf
Result:
apache2 nginx ssh
awk
here acts like a mini reporting tool — perfect for creating summaries or logs.
Example 5: Inserting a Line After a Specific Match
You can insert new lines or parameters dynamically using sed
.
Example:
sed -i '/^PermitRootLogin/a Banner /etc/issue.net' /etc/ssh/sshd_config
Explanation:
/^PermitRootLogin/
— matches the directive line.a
— appends after the match.Adds a
Banner /etc/issue.net
line neatly under the related directive.
This is great for keeping logically related options together.
Example 6: Combining sed and awk in Bash Scripts
You can combine both commands in shell scripts for large-scale automation.
Example script to update MaxConnections
across multiple .conf
files:
#!/bin/bash NEW_VALUE=500 for file in /etc/myapp/*.conf; do if awk '$1 == "MaxConnections"' "$file" > /dev/null; then sed -i 's/^MaxConnections.*/MaxConnections '"$NEW_VALUE"'/' "$file" echo "Updated MaxConnections in $file" else echo "MaxConnections $NEW_VALUE" >> "$file" echo "Added MaxConnections to $file" fi done
What it does:
Checks if
MaxConnections
exists in each config file.If yes, replaces it with the new value.
If not, appends it to the end of the file.
This approach saves hours of manual work when dealing with multiple servers.
Tips for Safely Editing Configuration Files
Always create backups before running sed -i
. Example:
sed -i.bak 's/foo/bar/' /etc/example.conf
This saves the original as /etc/example.conf.bak
.
- Test your expressions with
sed
orawk
without the-i
flag first. - Use version control (e.g.,
git
) for critical config directories. - Verify file syntax using service reload commands:
sudo systemctl restart sshd sudo nginx -t
Common Mistakes to Avoid
Forgetting
-i
when you intend to save changes.Using incorrect regex patterns — always test before deploying.
Editing system-critical files without backup.
Overwriting commented directives unintentionally — use
^#\?
to handle both.
Conclusion
Learning to use sed and awk to modify config files in Linux is one of the most valuable skills for system administrators and DevOps engineers. These command-line tools allow for fast, consistent, and repeatable configuration changes — from updating a single parameter to bulk editing hundreds of files.
By mastering them, you can streamline your automation workflows, minimize human error, and ensure your systems remain efficiently configured.
FAQs — Use sed and awk to Modify Config Files in Linux
Q1. Can I use sed and awk together in one command?
Yes. You can pipe data between them, for example:
awk '/pattern/' file | sed 's/foo/bar/' This combines the filtering power ofawk
with the text substitution features ofsed
. Q2. How do I test a sed command without modifying files?
Simply omit the-i
flag: sed 's/old/new/' file.conf
This prints the output to the terminal without saving changes.
Q3. Is sed faster than awk?
For simple substitutions or deletions, sed
is generally faster. For structured field operations or reports, awk
is more flexible.
Q4. What are safer alternatives to sed -i?
Use -i.bak
to create a backup or use output redirection:
sed 's/old/new/' file.conf > new.conf
Q5. Where can I learn more about sed and awk commands?
You can check official GNU documentation and trusted tutorials below.
Suggested Read:
- How to Install Nano Text Editor in Linux – A Simple, Modeless Text Editor for Linux
- How to clear var space in Linux – Best Practices for Freeing Up Disk Space
- How to Install Spaceview Disk Space Analyzer (Disk Utility) in Ubuntu – A Best Disk Usage Indicator for Linux
- Best Useful Linux DF Command With Examples
- Useful Linux Fdisk Command With Examples – A Linux Disk Partition Tool
- How to Extend Root Partition in Linux: Step-by-Step Guide for Safe Disk Expansion