基礎LESSON 02

この教材で学ぶこと プロセス・ログ・権限・サービスを調査する基本を学びます。

Linux 実践 — インフラエンジニアの日常操作

前提知識: ls・cd・grepなどの基本操作。本教材ではトラブルシューティングとチューニングを扱います。


1. プロセス・システム調査

top より強力な htop / ps の使い方

# CPU使用率上位10プロセス
ps aux --sort=-%cpu | head -11

# メモリ使用率上位
ps aux --sort=-%mem | head -11

# 特定プロセスのスレッド確認
ps -eLf | grep nginx

# プロセスツリー(親子関係を可視化)
pstree -p $(pgrep nginx)

# プロセスのオープンファイル数(FD枯渇調査)
ls /proc/$(pgrep -f myapp)/fd | wc -l

システムリソース確認

# CPU・メモリ・ディスクの一括確認
echo "=== CPU ===" && lscpu | grep -E "^CPU\(s\)|Model name|Socket"
echo "=== Memory ===" && free -h
echo "=== Disk ===" && df -h | grep -v tmpfs
echo "=== Load ===" && uptime

# ディスクI/O調査( iostat は sysstat パッケージ)
iostat -x 1 5  # 1秒間隔で5回

# ネットワーク帯域確認
ss -s  # ソケット統計
cat /proc/net/dev  # インターフェース別送受信バイト数

2. ログ解析パターン

# Nginx アクセスログから5xxエラーを集計
awk '$9 ~ /^5/' /var/log/nginx/access.log | \
    awk '{print $9}' | sort | uniq -c | sort -rn

# 直近1時間のエラーログ
awk -v d="$(date -d '1 hour ago' '+%d/%b/%Y:%H')" \
    '$0 ~ d' /var/log/nginx/access.log | grep -E " [45][0-9]{2} "

# journald(systemd のログ)
journalctl -u nginx --since "1 hour ago" --no-pager
journalctl -p err --since today  # エラー以上のみ
journalctl -f  # リアルタイム追跡

# ログのリアルタイム監視+フィルタ
tail -f /var/log/app/error.log | grep --line-buffered "CRITICAL\|ERROR"

3. systemd — サービス管理

# サービスの基本操作
systemctl status nginx
systemctl start/stop/restart/reload nginx
systemctl enable/disable nginx  # 自動起動の有効化

# サービスの状態確認(障害調査)
systemctl is-active nginx   # active/inactive
systemctl is-failed nginx   # failed かどうか

# 全サービスの起動失敗を確認
systemctl --failed

# サービスファイルの確認
systemctl cat nginx

カスタム systemd サービスの作成

# /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target

[Service]
Type=simple
User=myapp
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/python3 /opt/myapp/main.py
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal

# リソース制限
LimitNOFILE=65536
MemoryMax=512M

[Install]
WantedBy=multi-user.target
systemctl daemon-reload  # ファイル変更後に必要
systemctl enable --now myapp

4. ネットワークトラブルシューティング

# ポートのリッスン確認
ss -tlnp | grep :8080          # TCP リッスンポート
ss -tlnp | grep ESTABLISHED    # 確立済み接続

# 接続先への疎通確認(traceroute より詳細)
mtr --report google.com

# DNS 解決の確認
dig @8.8.8.8 example.com A     # 外部DNSで確認
resolvectl query example.com   # systemd-resolved 経由

# TCPダンプ(パケットレベルの確認)
tcpdump -i eth0 -n port 80 -w /tmp/capture.pcap
tcpdump -r /tmp/capture.pcap -A | head -50

# ファイアウォール確認(nftables/iptables)
iptables -L -n -v
nft list ruleset

5. ファイルシステム・ディスク

# ディスク使用量の調査(大きいディレクトリを特定)
du -sh /* 2>/dev/null | sort -rh | head -10
du -sh /var/log/* | sort -rh | head -10

# inode 枯渇確認(ファイル数が多すぎる問題)
df -i  # inode 使用率

# ファイルを開いているプロセスを確認(削除後もディスクを占有する問題)
lsof | grep deleted | awk '{print $1, $2, $7/1024/1024 "MB"}' | sort -k3 -rn

# ディスクI/Oが高いファイルを特定
iotop -ao  # -a: 累積、-o: 実際にI/Oしているもののみ

6. ユーザー・権限管理

# sudoers の設定(visudo で編集)
# 特定コマンドのみ sudo 許可
deploy ALL=(ALL) NOPASSWD: /bin/systemctl restart myapp, /bin/systemctl status myapp

# ファイル権限の確認
stat /etc/passwd  # 詳細な権限情報
getfacl /path/to/file  # ACL確認

# setuid/setgid ファイルの検索(セキュリティ監査)
find / -perm -4000 -type f 2>/dev/null  # setuid
find / -perm -2000 -type f 2>/dev/null  # setgid

7. カーネルパラメータ(sysctl)

# 現在の設定確認
sysctl -a | grep net.core
sysctl vm.swappiness  # スワップ使用傾向(デフォルト60)

# 一時的な変更
sysctl -w net.core.somaxconn=65535
sysctl -w vm.swappiness=10

# 永続化
echo "net.core.somaxconn = 65535" >> /etc/sysctl.d/99-custom.conf
sysctl --system  # 再読み込み

よく変更するパラメータ

パラメータ デフォルト 推奨値(Webサーバー) 意味
net.core.somaxconn 128 65535 TCPバックログキュー
net.ipv4.tcp_tw_reuse 0 1 TIME_WAIT再利用
vm.swappiness 60 10 スワップ使用頻度
fs.file-max 可変 1000000 システム全体のFD上限

🔗 次のステップ