複数行のテキスト処理:sshd_configの編集

複数行のテキスト処理:sshd_configの編集

sshd_config ファイルの末尾には、次のような複数のブロックがあります。

Match User FOO
    ChrootDirectory /srv/www/FOO
    AllowTCPForwarding no
    X11Forwarding no
    ForceCommand internal-sftp

Bashスクリプトからこのブロック(FOO1、FOO2などに関連する他のブロックを除く)を削除するにはどうすればよいですか?

ベストアンサー1

すべてのMatchブロックがファイルの末尾にあるとしますsshd_config
ブロックが空行で区切られている場合は、次のようになります。

Match User FOO1
    PasswordAuthentication no

Match User FOO2
    PasswordAuthentication yes

Match User FOO
    ChrootDirectory /srv/www/FOO
    AllowTCPForwarding no
    X11Forwarding no
    ForceCommand internal-sftp

Match User FOO次に、最初の空白行まで(および含む)行から削除します。

sed '/^Match User FOO$/,/^$/d' sshd_config

たとえば、分離されていない場合:

Match User BAZ
    PasswordAuthentication yes
Match User FOO
    ChrootDirectory /srv/www/FOO
    AllowTCPForwarding no
    X11Forwarding no
    ForceCommand internal-sftp
Match User FOO1
    PasswordAuthentication no
Match User FOO2
    PasswordAuthentication yes

次に、Match User FOO次から始まる最初の行まで削除します(含まない)Match

sed '/^Match User FOO$/,/^Match.*/{//!d;/^Match User FOO$/d;}' sshd_config

sed -i ...ファイルの内部編集を使用する必要があります。sed詳細/バックアップオプションについては、マニュアルを確認してください。

おすすめ記事