Linuxを運用していると必ずと言っていいほど、セキュリティホールが発見され、対応されるよう指示されることがあります。
CMSなど複雑なシステムが動いているようなサーバであれば自動アップデートは危険であり、手動対応が望ましいと思いますが、HTMLだけを表示しているような簡易的なものであれば自動アップデートで実施してしまえば運用が楽になると思うので試してみました。
パッケージはdnf-automaticです。dnfからインストールします。
# dnf install dnf-automatic |
設定ファイルは/etc/dnf/automatic.confです。ファイルの中に英語ですが、コメント的に意味を記載してくれているので、それを読めばある程度は設定の意味が理解できます。以下に編集したところを太字で記載しています。
# vi /etc/dnf/automatic.conf
[commands]
# What kind of upgrade to perform:
# default = all available upgrades
# security = only the security upgrades
# アップデート対象はセキュリティのみに絞る
upgrade_type = security
random_sleep = 0
# Maximum time in seconds to wait until the system is on-line and able to
# connect to remote repositories.
network_online_timeout = 60
# To just receive updates use dnf-automatic-notifyonly.timer
# Whether updates should be downloaded when they are available, by
# dnf-automatic.timer. notifyonly.timer, download.timer and
# install.timer override this setting.
download_updates = yes
# Whether updates should be applied when they are available, by
# dnf-automatic.timer. notifyonly.timer, download.timer and
# install.timer override this setting.
# アップデートの適用まで実施する
apply_updates = yes
# When the system should reboot following upgrades:
# never = don't reboot after upgrades
# when-changed = reboot after any changes
# when-needed = reboot when necessary to apply changes
reboot = never
# The command that is run to trigger a system reboot.
reboot_command = "shutdown -r +5 'Rebooting after applying package updates'"
[emitters]
# Name to use for this system in messages that are emitted. Default is the
# hostname.
# system_name = my-host
# How to send messages. Valid options are stdio, email and motd. If
# emit_via includes stdio, messages will be sent to stdout; this is useful
# to have cron send the messages. If emit_via includes email, this
# program will send email itself according to the configured options.
# If emit_via includes motd, /etc/motd file will have the messages. if
# emit_via includes command_email, then messages will be send via a shell
# command compatible with sendmail.
# Default is email,stdio.
# If emit_via is None or left blank, no messages will be sent.
# アップデートの通知を標準出力(stdio)ではなくemailにする
emit_via = email
[email]
# The address to send email messages from.
# 送信元メールアドレスの指定
email_from = root@example.com
# List of addresses to send messages to.
# 送信先アドレスの指定
email_to = root
# Name of the host to connect to to send email messages.
# SMTPサーバの指定
email_host = localhost
[command]
# The shell command to execute. This is a Python format string, as used in
# str.format(). The format function will pass a shell-quoted argument called
# `body`.
# command_format = "cat"
# The contents of stdin to pass to the command. It is a format string with the
# same arguments as `command_format`.
# stdin_format = "{body}"
[command_email]
# The shell command to use to send email. This is a Python format string,
# as used in str.format(). The format function will pass shell-quoted arguments
# called body, subject, email_from, email_to.
# command_format = "mail -Ssendwait -s {subject} -r {email_from} {email_to}"
# The contents of stdin to pass to the command. It is a format string with the
# same arguments as `command_format`.
# stdin_format = "{body}"
# The address to send email messages from.
email_from = root@example.com
# List of addresses to send messages to.
email_to = root
[base]
# This section overrides dnf.conf
# Use this to filter DNF core messages
debuglevel = 1
|
また、アップデート対象外とするパッケージを指定できます。念の為、kernel関係は除外するようにしておきます。
# vi /etc/dnf/dnf.conf <追記> excludepkgs=kernel* linux-firmware* |
ここまできたらデーモンを起動します。
# systemctl enable dnf-automatic.timer # systemctl start dnf-automatic.timer または # systemctl enable --now dnf-automatic.timer |
デフォルトですと早朝6時ぐらいに自動アップデートがかかるように設定されています。設定ファイルは下記となりますが基本触る必要はありません。時間を変更したいなど、必要に応じて編集してください。
# vi /etc/systemd/system/timers.target.wants/dnf-automatic.timer [Unit] Description=dnf-automatic timer # See comment in dnf-makecache.service ConditionPathExists=!/run/ostree-booted Wants=network-online.target [Timer] OnCalendar=*-*-* 6:00 RandomizedDelaySec=60m Persistent=true [Install] WantedBy=timers.target |
ちなみに上のOnCalendarの書式は下記のようになります。
OnCalendar=$YEAR-$MONTH-$DAY $HOUR:$MINUTE |
設定を変更した場合は以下で反映、確認が可能です。
# systemctl daemon-reload # systemctl list-timers NEXT LEFT LAST PASSED UNIT ACTIVATES Fri 2024-10-25 11:26:09 JST 16min left Fri 2024-10-25 06:35:50 JST 4h 33min ago dnf-automatic.timer dnf-automatic.service Fri 2024-10-25 12:08:25 JST 58min left Fri 2024-10-25 10:43:06 JST 26min ago dnf-makecache.timer dnf-makecache.service Fri 2024-10-25 16:48:18 JST 5h 38min left Thu 2024-10-24 16:48:18 JST 18h ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service Sat 2024-10-26 00:00:00 JST 12h left Fri 2024-10-25 00:00:06 JST 11h ago logrotate.timer logrotate.service Sat 2024-10-26 00:00:00 JST 12h left Fri 2024-10-25 00:00:06 JST 11h ago mlocate-updatedb.timer mlocate-updatedb.service |
また、アップデート後にデーモン再起動を必要とするミドルウェアも存在します。それを確認するには以下のコマンドで確認できます。-rでOSごと再起動いるかどうか、-sでサービスごとに必要なものを表示してくれます。
# needs-restarting -r No core libraries or services have been updated since boot-up. Reboot should not be necessary. # needs-restarting -s systemd-udevd.service polkit.service systemd-logind.service NetworkManager.service systemd-journald.service vsftpd.service sshd.service dbus-broker.service |