Change config settings using a bash script

There is a trend to perform all system administration tasks using scripts. The benefit of this approach is that the scripts can be checked into a source control system, such as github.

One great application of this strategy is the script that you use to harden your Linux systems.  This hardening script, with the appropriate in-line comments, can serve as part of your compliance documentation.  Other scripts can be used to verify the the configuration is actually as intended and has not been altered.

One of the simple and powerful command line tools that I use is “sed,” the stream editor.  I urge you to read the man page and get to know it well.

In the rest of this post, I will be demonstrating how to use sed to edit the /etc/ssh/sshd_config file.  Lets say that you want to make sure the the file contains an entry that prevents the root user account from accessing the server via SSH.  You would want it to contain the following line:

and it currently contains the following lines:

Well, you could use these lines in your hardening script:

But what about if the current sshd_config file looks like this:

In this case, the line of interest has been commented out and has white space to content with.  Instead, use the following syntax:

This command says to sed, “find the line that may have an octothorpe in the beginning position and contains ‘PermitRootLogin’ and whitespace then replace what ever follows that with a ‘no.’ but remove the octothorpe”

Note that the  sed -i can be used to modify a file in place rather than redirecting to a temp file.

I use this very handy trick all of the time. The one caution is that if sed does not find a line containing the ‘PermitRootLogin’ search string nothing happens. For this reason, you should also use another script to audit if the configuration settings are as expected.

Tagged , , , . Bookmark the permalink.
  • The postings and views on this site are my own and do not necessarily reflect the positions, strategies, or opinions of any current or previous employer.