はじめに:セキュリティは選択ではなく必須

サーバーセキュリティは単一ソリューションではなく、多層防御(Defense in Depth)戦略が必要です。この最終編では、Linuxサーバーセキュリティ強化の核心を解説します。

1. システムハードニング基礎

1.1 不要なサービスの無効化

# 実行中のサービス確認
systemctl list-units --type=service --state=running

# 不要なサービス無効化
sudo systemctl disable --now cups
sudo systemctl disable --now avahi-daemon
sudo systemctl disable --now bluetooth

# 起動時開始サービス確認
systemctl list-unit-files --type=service --state=enabled

1.2 セキュリティアップデート

# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y
sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades

# RHEL/CentOS
sudo dnf update -y
sudo dnf install dnf-automatic
sudo systemctl enable --now dnf-automatic.timer

1.3 GRUBセキュリティ

# GRUBパスワード設定
sudo grub-mkpasswd-pbkdf2
# ハッシュ値をコピー

# /etc/grub.d/40_custom 編集
set superusers="admin"
password_pbkdf2 admin grub.pbkdf2.sha512...

# 設定適用
sudo update-grub

2. ユーザーセキュリティ

2.1 パスワードポリシー

# /etc/login.defs
PASS_MAX_DAYS   90    # 最大使用期間
PASS_MIN_DAYS   7     # 最小使用期間
PASS_WARN_AGE   14    # 期限切れ警告

# PAMパスワード複雑性 (/etc/pam.d/common-password)
password requisite pam_pwquality.so retry=3 minlen=12 dcredit=-1 ucredit=-1 ocredit=-1 lcredit=-1

# 既存ユーザーに適用
sudo chage -M 90 -m 7 -W 14 username

2.2 sudo設定

# /etc/sudoers (visudo使用)
# ログ記録
Defaults logfile=/var/log/sudo.log
Defaults log_input, log_output

# タイムアウト設定
Defaults timestamp_timeout=5

# 特定コマンドのみ許可
username ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx

2.3 アカウントロック

# /etc/pam.d/common-auth
auth required pam_tally2.so deny=5 unlock_time=900

# 失敗回数確認
pam_tally2 --user=username

# ロック解除
pam_tally2 --user=username --reset

3. SSHセキュリティ強化

# /etc/ssh/sshd_config

# デフォルトポート変更
Port 2222

# rootログイン禁止
PermitRootLogin no

# パスワード認証無効化
PasswordAuthentication no

# 鍵認証のみ許可
PubkeyAuthentication yes

# 空パスワード禁止
PermitEmptyPasswords no

# ログイン試行制限
MaxAuthTries 3
LoginGraceTime 30

# 特定ユーザー/グループのみ許可
AllowUsers admin deploy
AllowGroups sshusers

# プロトコルバージョン
Protocol 2

# 適用
sudo systemctl restart sshd

4. ファイアウォール設定

4.1 UFW (Ubuntu)

# デフォルトポリシー
sudo ufw default deny incoming
sudo ufw default allow outgoing

# 必要なポートのみ許可
sudo ufw allow 2222/tcp comment 'SSH'
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'

# IPベース制限
sudo ufw allow from 192.168.1.0/24 to any port 22

# 有効化
sudo ufw enable
sudo ufw status verbose

4.2 firewalld (RHEL/CentOS)

# デフォルトゾーン確認
sudo firewall-cmd --get-default-zone

# サービス許可
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

# ポート許可
sudo firewall-cmd --permanent --add-port=2222/tcp

# ルール適用
sudo firewall-cmd --reload

5. 侵入検知システム

5.1 Fail2Ban

# インストール
sudo apt install fail2ban

# /etc/fail2ban/jail.local
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3

[sshd]
enabled = true
port = 2222
logpath = /var/log/auth.log

[nginx-http-auth]
enabled = true

# 状態確認
sudo fail2ban-client status
sudo fail2ban-client status sshd

5.2 AIDE(整合性検査)

# インストール
sudo apt install aide

# データベース初期化
sudo aideinit

# 検査実行
sudo aide --check

# データベース更新
sudo aide --update

6. SELinux / AppArmor

6.1 SELinux (RHEL/CentOS)

# 状態確認
getenforce
sestatus

# モード変更
sudo setenforce 0  # Permissive
sudo setenforce 1  # Enforcing

# 永続設定 (/etc/selinux/config)
SELINUX=enforcing

# トラブルシューティング
sudo ausearch -m avc -ts recent
sudo sealert -a /var/log/audit/audit.log

6.2 AppArmor (Ubuntu)

# 状態確認
sudo aa-status

# プロファイル管理
sudo aa-enforce /etc/apparmor.d/usr.sbin.nginx
sudo aa-complain /etc/apparmor.d/usr.sbin.nginx

# ログ確認
sudo journalctl -fx | grep apparmor

7. 脆弱性スキャン

7.1 Lynis

# インストール
sudo apt install lynis

# システム監査
sudo lynis audit system

# レポート確認
sudo cat /var/log/lynis.log
sudo cat /var/log/lynis-report.dat

7.2 OpenVAS

# インストール (Ubuntu)
sudo apt install openvas

# 初期設定
sudo gvm-setup

# スキャン実行
sudo gvm-start
# Webインターフェース: https://localhost:9392

8. ログモニタリング

# 不審な活動の監視
# ログイン失敗
grep "Failed password" /var/log/auth.log | tail -20

# sudo使用記録
grep "sudo" /var/log/auth.log

# システム起動/終了
last reboot
last shutdown

# 現在接続ユーザー
who
w

# ネットワーク接続
ss -tuln
netstat -tuln

9. セキュリティチェックリスト

#!/bin/bash
# security_audit.sh

echo "=== Linux セキュリティ監査 ==="

echo -e "\n[1] システムアップデート確認"
apt list --upgradable 2>/dev/null | head -10

echo -e "\n[2] rootログイン可否"
grep "^PermitRootLogin" /etc/ssh/sshd_config

echo -e "\n[3] パスワード認証状態"
grep "^PasswordAuthentication" /etc/ssh/sshd_config

echo -e "\n[4] 開いているポート"
ss -tuln | grep LISTEN

echo -e "\n[5] SUIDファイル"
find / -perm -4000 -type f 2>/dev/null | head -10

echo -e "\n[6] 空パスワードアカウント"
awk -F: '($2 == "") {print $1}' /etc/shadow

echo -e "\n[7] 最近のログイン失敗"
grep "Failed password" /var/log/auth.log 2>/dev/null | tail -5

echo -e "\n[8] ファイアウォール状態"
ufw status 2>/dev/null || firewall-cmd --state 2>/dev/null

echo -e "\n監査完了"

10. シリーズ完結

Linuxサーバー管理完全攻略シリーズを終了します。10編にわたり、基礎からセキュリティまでサーバー管理の核心を解説しました:

  • 第1編:Linux基礎とファイルシステム
  • 第2編:ユーザーと権限管理
  • 第3編:パッケージ管理とサービス運用
  • 第4編:ネットワーク設定とファイアウォール
  • 第5編:SSHとリモートサーバー管理
  • 第6編:シェルスクリプト基礎
  • 第7編:シェルスクリプト上級
  • 第8編:ログ管理とモニタリング
  • 第9編:バックアップと復旧戦略
  • 第10編:セキュリティ強化と脆弱性管理

このシリーズがLinuxサーバー管理の確かな基礎となることを願います。