TORIPIYO DIARY

recent events, IoT, programming, security topics

apt-get update と yum update の違い

(自分への📝メモも兼ねて)

apt-get update と、yum update は似たような書き方なのでどちらも同じような挙動をするように認識しがちですが、異なります。これら2つのコマンドの解釈を間違えると、思わぬ事故に繋がります。

apt-get update

apt-get updateの説明は以下となります。(Linux ディストリビューションによって多少説明内容は異なるかもしれません。)

manpages.debian.org

update

update is used to resynchronize the package index files from their sources. 
The indexes of available packages are fetched from the location(s) specified in /etc/apt/sources.list. 
For example, when using a Debian archive, this command retrieves and scans the Packages.gz files, so that information about new and updated packages is available. 
An update should always be performed before an upgrade or dist-upgrade. 
Please be aware that the overall progress meter will be incorrect as the size of the package files cannot be known in advance.

apt-get update は、パッケージを新規インストールしたり、既存パッケージの更新を実行したりはしません。/etc/apt/sources.list に記載されている場所から入手可能なパッケージのインデックス情報を取得してインデックスファイルを再同期させます。

例えば、debian の docker イメージでapt-get updateを実行する前に、apt-get install apache2を実行するとパッケージを見つけることが出来ません。

$ docker run -it --rm debian bash

# apt-get install apache2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package apache2

apt-get updateを実行すると、入手可能なパッケージのインデックス情報を取得してきて、apache2のパッケージをインストール出来るようになります。

# apt-get update
Get:1 http://deb.debian.org/debian bullseye InRelease [116 kB]
Get:2 http://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB]
Get:3 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
Get:4 http://deb.debian.org/debian bullseye/main arm64 Packages [8071 kB]
Get:5 http://deb.debian.org/debian-security bullseye-security/main arm64 Packages [181 kB]
Get:6 http://deb.debian.org/debian bullseye-updates/main arm64 Packages [2604 B]
Fetched 8463 kB in 4s (2107 kB/s)
Reading package lists... Done

# apt-get install apache2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils bzip2 ca-certificates file libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap
  libbrotli1 libcurl4 libexpat1 libgdbm-compat4 libgdbm6 libgpm2 libicu67 libjansson4 libldap-2.4-2 libldap-common liblua5.3-0
  libmagic-mgc libmagic1 libncurses6 libncursesw6 libnghttp2-14 libperl5.32 libprocps8 libpsl5 librtmp1 libsasl2-2
  libsasl2-modules libsasl2-modules-db libsqlite3-0 libssh2-1 libxml2 mailcap media-types mime-support netbase openssl perl
  perl-modules-5.32 procps psmisc publicsuffix ssl-cert xz-utils

以下略

debian系でパッケージのインストールに失敗するときは、apt-get updateを実行し忘れていないか確認しましょう。

yum update

yum updateの説明は以下となります。(これも、Linux ディストリビューションによって説明は多少異なると思います。)

linux.die.net

update

If run without any packages, update will update every currently installed package. 
If one or more packages or package globs are specified, Yum will only update the listed packages. 
While updating packages, yum will ensure that all dependencies are satisfied. (See Specifying package names for more information) 
If the packages or globs specified match to packages which are not currently installed then update will not install them. 
update operates on groups, files, provides and filelists just like the "install" command.

yum update を実行すると、現在インストールされている全てのパッケージについてアップデートを行います。yum update httpdのように、インストールされているパッケージを指定すればそのパッケージを更新しますが、もし指定したパッケージがインストールされていない場合は、新規インストールは実行されません。

amazon linux の docker イメージで試すと以下のようになります。yum update で、gnupg2, tzdata のパッケージが更新されようとします。 また、yum update httpd を実行しても、httpdパッケージがインストールされていないので、更新や新規インストールは実行されません。

$ docker run -it --rm amazonlinux bash

# yum update
Loaded plugins: ovl, priorities
amzn2-core                                                                                                            | 3.7 kB  00:00:00
(1/3): amzn2-core/2/aarch64/group_gz                                                                                  | 2.5 kB  00:00:00
(2/3): amzn2-core/2/aarch64/updateinfo                                                                                | 498 kB  00:00:00
(3/3): amzn2-core/2/aarch64/primary_db                                                                                |  48 MB  00:00:14
Resolving Dependencies
--> Running transaction check
---> Package gnupg2.aarch64 0:2.0.22-5.amzn2.0.4 will be updated
---> Package gnupg2.aarch64 0:2.0.22-5.amzn2.0.5 will be an update
---> Package tzdata.noarch 0:2022a-1.amzn2 will be updated
---> Package tzdata.noarch 0:2022c-1.amzn2 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================
 Package                      Arch                          Version                                   Repository                        Size
=============================================================================================================================================
Updating:
 gnupg2                       aarch64                       2.0.22-5.amzn2.0.5                        amzn2-core                       1.5 M
 tzdata                       noarch                        2022c-1.amzn2                             amzn2-core                       481 k

Transaction Summary
=============================================================================================================================================
Upgrade  2 Packages

Total download size: 2.0 M
Is this ok [y/d/N]:

以下略

# yum update httpd
Loaded plugins: ovl, priorities
Package(s) httpd available, but not installed.
No packages marked for update

===========================

apt-get update と yum update は違いを意識して使おう

apt-get updateyum update の違いを知らないと、debian 系と centos 系の OS を混同して運用しているときに操作を間違えてしまうことがあります。

例えば、yum updateapt-get update と同じと勘違いして、centos にインストールされている全てのパッケージを意図せずに更新してしまったり、yum -y update ${package} と変数に更新するパッケージ名を指定しているつもりが、指定されていなくて全てのパッケージを更新してしまったりなど、障害を引き落とす落とし穴にはまりやすいので気をつけましょう。

参考

unix.stackexchange.com