How to Effectively and Efficiently Edit Configuration Files in Linux

11897

Every Linux administrator has to eventually (and manually) edit a configuration file. Whether you are setting up a web server, configuring a service to connect to a database, tweaking a bash script, or troubleshooting a network connection, you cannot avoid a dive deep into the heart of one or more configuration files. To some, the prospect of manually editing configuration files is akin to a nightmare. Wading through what seems like countless lines of options and comments can put you on the fast track for hair and sanity loss.

Which, of course, isn’t true. In fact, most Linux administrators enjoy a good debugging or configuration challenge. Sifting through the minutiae of how a server or software functions is a great way to pass time. But this process doesn’t have to be an exercise in ineffective inefficiency. In fact, tools are available to you that go a very long way to make the editing of config files much, much easier. I’m going to introduce you to a few such tools, to ease some of the burden of your Linux admin duties. I’ll first discuss the command-line tools that are invaluable to the task of making configuration more efficient.

Let’s begin.

diff

If you’ve never used the diff command, you don’t know what you’re missing. The gist of diff is simple: It compares one file against another and displays the differences. Let me explain.
Say you have two files. File1 has the contents:

<Directory “/var/www”>

     AllowOverride None

     Require all granted

</Directory>

File2 has the contents:

<Directory “/var/www/html”>

     AllowOverride None

    Require all granted

</Directory>

If that’s all those two files contained, it would really simple to open them up and see the diff. But what if those lines of code were buried within thousands of other lines and interspersed with comments and other options? All of a sudden, that task becomes a bit more daunting.

Thanks to diff, we can find these differences easily. If we open up a terminal and issue the command diff File1 File2, we’ll see the output clearly displaying the differences (Figure 1).

Figure 1: The diff command outputting the variances between File1 and File2.

What you want to look for are the letters a, c, and d, where:

  • a means something was added

  • c means something was changed

  • d means something was deleted

In this example, you see 1c1, which means line 1 was changed in the second file.

The diff output is a bit cumbersome because it was actually intended to be read by the system, not humans. The intention of diff is to show what would need to be done to the files to put them in sync with one another. What is important in the output, however, is that it will only output the lines which are different. In our example, everything in the file is identical except for the first lines, where you have /var/www in one and /var/www/html in the other. Using diff makes it incredibly easy to find out the differences between two configuration files. Of course, diff is much more complex than that, but understanding this very fundamental usage of the tool will help you tremendously when comparing two files.

If we change File2 to reflect:

<Directory “/var/www/html”>
    AllowOverride all
</Directory>

The output of diff would then a bit more complex. For that, we might want to run diff -c File1 File2. The c option prints the output in context format, which makes it much easier to read (Figure 2).

Figure 2: More complex diff output which has been made easier to understand with the c option.

Here we see diff reporting that lines 1 and 4 in File1 and lines 1 and 3 in File2 do not match. You can now make those changes.

grep

The grep command should be one of the first tools you learn as a Linux administrator. Without it, you will find yourself searching for the proverbial needle in a haystack, especially when digging around more extensive configuration files. Say, for instance, you want to disable the EnableSendfile in your CentOS Apache configuration. You could open up the /etc/httpd/httpd.conf and then scroll through until you see the entry, or you could issue the command grep -n EnableSendfile /etc/httpd/conf/httpd.conf.

What grep does is print lines matching a pattern. It’s that simple. However, if you add the -n option, grep will also print the line number for which the pattern can be found. In our example, grep outputs that EnableSendfile is found on lines 340, 346, and 349 (Figure 3).

Figure 3: Using grep to locate an option in a configuration file.

 

If you happen to use a text editor, such as nano, you can open up the /etc/httpd/conf/httpd.conf file, scroll down a bit and hit Ctrl-c to report what line number the cursor is on. Keep scrolling until you find the line you need to edit. You can also open up the file with nano, using the -c option, to display the current line number (without having to hit the key combination — Figure 4).

Figure 4: Nano displaying the line number.

The grep command is incredibly powerful. Make sure to view the man page (man grep) to learn everything you can about this helpful tool.

Find a good GUI

Some people would rather spend their time with a GUI tool than the command line. Although I highly recommend you fully understand how to work with the command line, there are instances where a GUI can go a long way to make this process easier. Take, for instance, the Gedit text editor. With this GNOME-friendly editor, you can set syntax highlighting on the fly to easily suit the configuration file you’re working with.

Suppose you open up /etc/httpd/conf/httpd.conf in Gedit. Because this particular file is just a basic text file, Gedit will open it set on Plain Text (in other words, no syntax highlighting). You can switch that from the drop-down in the bottom toolbar and select the type of syntax highlighting you want. If you switch it to PHP, anything that could be viewed as a PHP element will be highlighted (Figure 5).

Figure 5: Adding syntax highlighting for easier configuration.

There are plenty of solid editors out there that will aid you in making cumbersome configurations a bit easier. Start with the tool included with your desktop and see if it will do the trick. If not, open up your package manager and see if you can find one that might fit your needs (such as Sublime Text, Geany, or Leafpad).

Don’t let it overwhelm you

With just a few simple tools, you can make the process of editing Linux configuration files quite easy. Start out with these three tools and build from there. Eventually you’ll have a toolkit so powerful, you’ll be editing config files like a pro. And don’t forget to RTFM!

Want to learn more about managing your configuration files? Check out the Essentials of System Administration course from The Linux Foundation.