0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

apt upgrade -y, 本当にyesで大丈夫?

Posted at

著者はyesしています。

はじめに

apt upgradeするときにどんな質問をされているのか気になったので調べました。

質問1. 設定ファイル変更するけど大丈夫?

パッケージが管理しているファイルが変更されている場合、対話が求められます。
とりあえずyesしていると、既存設定が消し飛んでしまう可能性があるので注意が必要です。
困った時はnoと回答すると.dpkg-dist, 例えばsshd_config.dpkg-distというファイルが作成されてキャンセルされるのでdiffコマンドで変更差分を確認できます。

変更検知の仕組み

dpkgによる検知、ucfによる検知の2種類の仕組みがあります。

dpkgによる検知

各パッケージは変更を検知したいファイルリストを保持しており、それぞれのhashを保存しています。
dpkgは現行設定のhashと比較することで変更検知を行っています。

# 例) openssh-server
root@ubuntu:~# grep -F "Package: openssh-server" /var/lib/dpkg/status -A25 | grep -i conffiles -A6
Conffiles:
 /etc/default/ssh 500e3cf069fe9a7b9936108eb9d9c035
 /etc/init.d/ssh 3649a6fe8c18ad1d5245fd91737de507
 /etc/pam.d/sshd 8b4c7a12b031424b2a9946881da59812
 /etc/ssh/moduli e8fbe2dcefa45888cf7341d78d8258ce
 /etc/ufw/applications.d/openssh-server 486b78d54b93cc9fdc950c1d52ff479e
Description: secure shell (SSH) server, for secure access from remote machines
# オプションをつけてupgradeし、対話画面を出力させた
root@ubuntu:~# sudo apt install --reinstall openssh-server -o Dpkg::Options::="--force-confask"
Reading package lists... Done
...
Setting up openssh-server (1:8.9p1-3ubuntu0.13) ...

Configuration file '/etc/default/ssh'
 ==> Modified (by you or by a script) since installation.
     Version in package is the same as at last installation.
   What would you like to do about it ?  Your options are:
    Y or I  : install the package maintainer\'s version
    N or O  : keep your currently-installed version
      D     : show the differences between the versions
      Z     : start a shell to examine the situation
 The default action is to keep your current version.
*** ssh (Y/I/N/O/D/Z) [default=N] ? D
--- /etc/default/ssh    2025-07-10 02:51:41.271833056 +0900
+++ /etc/default/ssh.dpkg-new   2024-07-10 19:17:07.000000000 +090
@@ -3,3 +3,4 @@
 
 # Options to pass to sshd
 SSHD_OPTS=
+hoge

ucfによる検知

デフォルト設定などの差分検知はucfで行っているようです。
dpkgファイルは重要なファイル、ucfはデフォルト設定などと使い分けているようです。
パッケージインストール直後のpost scriptの中でucfコマンドが実行され、以下差分検知を行っているようです。

  • 旧設定ファイルのhash
  • 現在の設定ファイルのhash
  • 新パッケージに含まれる設定ファイルのhash
# 通常、*.postinstはapt install直後に実行される
root@ubuntu:~# grep "ucf " /var/lib/dpkg/info/openssh-server.postinst -A3
    # 差分検知を行い、conflictがあればユーザに対話を求める
    # conflictが解消したら/etc/ssh/sshd_configを更新する
	ucf --three-way --debconf-ok \
		--sum-file /usr/share/openssh/sshd_config.md5sum \
		"$new_config" /etc/ssh/sshd_config
    # 更新された設定のhashを管理簿に登録する
	ucfr openssh-server /etc/ssh/sshd_config

# ucfが管理しているhash管理簿
root@ubuntu:~# cat /var/lib/ucf/hashfile
1b03acc84964cf70b8a584870bcdf9b3  /etc/rsyslog.d/50-default.conf
1c261d6541420797f8b824d65ac5c197  /etc/apt/apt.conf.d/20auto-upgrades
3c21abeb36409045662e46e43343b5f3  /etc/apt/apt.conf.d/50unattended-upgrades
30e0fe758429c57d35a5e71dbd8dd2f8  /etc/ssh/sshd_config

# 現在の設定ファイルのhash
root@ubuntu:~# md5sum /etc/ssh/sshd_config
30e0fe758429c57d35a5e71dbd8dd2f8  /etc/ssh/sshd_config
# 両方変更してconflictを発生させる
root@ubuntu:~# vim /etc/ssh/sshd_config
root@ubuntu:~# vim /usr/share/openssh/sshd_config
root@ubuntu:~# ucf --three-way --debconf-ok --sum-file /usr/share/openssh/sshd_config.md5sum /usr/share/openssh/sshd_config /etc/ssh/sshd_config

対話画面

対話画面

質問2. サービスを再起動するけど大丈夫?

カーネルや共有ライブラリがアップデートされる場合に、設定を適用するために関連するサービスを再起動していいか聞かれる
古いカーネルやライブラリを参照し続けてしまうため、こちらはyesした方が良さそう
瞬断を許さないシステムや影響なしと周知してメンテナンスしている場合はnoにして別途対応した方が良さそう

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?