【Linux】confファイル(コンフィグ)からコメントアウトを除外して表示する方法

公開日時:2018年03月03日 / 最終更新日時:2019年02月11日

Linuxのコンフィグファイル(httpd.conf や sudoers や zabbix_server.conf など)をチェックする時に、設定はたいして入っていないのに、コメントアウトが多すぎて特定の個所を見つけるのが大変な時があります。

(less や view コマンドを使って目的の項目を検索すればいいのですが)

 

そのような時に confファイル(コンフィグ)からコメントアウトを除外して表示する方法を説明します。

 

 

grep で正規表現を使って設定箇所だけ表示する

コメントアウトの行を除外して表示するために「grep」コマンドを利用します。

grep コマンドは「正規表現」を利用することができます。

またオプションで「除外」もすることができます。

 

cat -e コマンドでファイルの構造を確認する

いきなりコマンドだけを出してもなぜなのか分からないため、まずはファイルの改行コードがどうなっているのか確認します。

[root@cent07 conf]# cat -e httpd.conf
#$
# This is the main Apache HTTP server configuration file.  It contains the$
# configuration directives that give the server its instructions.$
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.$
# In particular, see $
# <URL:http://httpd.apache.org/docs/2.4/mod/directives.html>$
# for a discussion of each configuration directive.$
#$
# Do NOT simply read the instructions in here without understanding$
# what they do.  They're here only as hints or reminders.  If you are unsure$
# consult the online docs. You have been warned.  $
#$
# Configuration and logfile names: If the filenames you specify for many$
# of the server's control files begin with "/" (or "drive:/" for Win32), the$
# server will use that explicit path.  If the filenames do *not* begin$
# with "/", the value of ServerRoot is prepended -- so 'log/access_log'$
# with ServerRoot set to '/www' will be interpreted by the$
# server as '/www/log/access_log', where as '/log/access_log' will be$
# interpreted as '/log/access_log'.$
$
#$
# ServerRoot: The top of the directory tree under which the server's$
# configuration, error, and log files are kept.$
#$
# Do not add a slash at the end of the directory path.  If you point$
# ServerRoot at a non-local disk, be sure to specify a local disk on the$
# Mutex directive, if file-based mutexes are used.  If you wish to share the$
# same ServerRoot for multiple httpd daemons, you will need to change at$
# least PidFile.$
#$
ServerRoot "/etc/httpd"$
$
#$

#$
# Relax access to content within /var/www.$
#$
<Directory "/var/www">$
    AllowOverride None$
    # Allow open access:$
    Require all granted$
</Directory>$
$

 

こうしてファイルの構造を確認するといくつかのパターンがあることが分かります。

 

パターンの洗い出し

パターンを洗い出すと以下の6パターンがあることが分かります。

  1. #$ ← コメントアウトのみ
  2. #xxxxxxx$ ← コメントアウトの後にコメント
  3. xxxxxxxx$ ← 設定
  4. $ ← 空行
  5. 空白空白空白xxxx$ ← 空白の後に設定
  6. 空白空白空白#xxxx$ ← 空白の後にコメントアウトが来て設定

 

実行結果

実際に「/etc/httpd/conf/httpd.conf」ファイルで試してみました。

[root@cent07 conf]# grep -E -v "^$|#$|#.+$|^\s+#.$" httpd.conf
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
    AllowOverride none
    Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

 

スッキリして見やすくなりましたね。

httpd.conf は大量に設定項目が入っているように思うかもしれませんが、コメントアウトの行を除外して表示してみるとたいした量の設定が入っているわけではないことが分かるので落ち着いて正確に設定を確認することができるかもしれません。

 

grep コマンドの解説

grep コマンドの正規表現の部分を解説します。

 

 

Posted by 100%レンタルサーバーを使いこなすサイト管理人

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

AlphaOmega Captcha Medica  –  What Do You See?
     
 

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

Secured By miniOrange