sudo apt-get update và sudo apt-get upgrade là hai lệnh dùng để keep all packages up to date trong Debian hoặc Debian-based Linux distribution.
Chúng là common commands cho Linux admins và DevOps, nhưng handy to know ngay cả nếu bạn không thường dùng command line.
Bài viết này đi sâu vào cả hai lệnh làm gì, cách dùng, và FAQs.
Khác biệt giữa sudo apt-get update và sudo apt-get upgrade?
Khác biệt chính: sudo apt-get update fetch latest version của package list từ software repository của distro, và third-party repositories bạn configured. Nghĩa là nó figure out latest version của mỗi package và dependency, nhưng không download hoặc install updates.
Lệnh sudo apt-get upgrade download và install updates cho mỗi outdated package và dependency trên system. Chỉ chạy sudo apt-get upgrade không tự động upgrade – bạn vẫn có chance review changes và confirm muốn perform upgrades.
Cách dùng lệnh sudo apt-get update
Trong Debian-based Linux distro (Debian, Ubuntu, Linux Mint, Kali Linux, Raspberry Pi OS, v.v.), mở terminal window.
Tùy distro, terminal có tên khác nhau tùy cách mở. Ví dụ Ubuntu và Linux Mint, default terminal là Gnome Terminal, nhưng có thể listed under Terminal trong application menu.
Trong terminal, gõ sudo apt-get update, nhập admin password, nhấn Enter.
Nếu có updates, output tương tự:
kris@pihole:~ $ sudo apt-get update
Hit:1 https://ftp.harukasan.org/raspbian/raspbian bullseye InRelease
Get:2 https://download.docker.com/linux/raspbian bullseye InRelease [26.7 kB]
Get:3 https://archive.raspberrypi.org/debian bullseye InRelease [23.7 kB]
Get:4 https://packages.azlux.fr/debian buster InRelease [3,989 B]
Get:5 https://archive.raspberrypi.org/debian bullseye/main armhf Packages [282 kB]
Get:6 https://packages.azlux.fr/debian buster/main armhf Packages [3,418 B]
Fetched 340 kB in 4s (94.8 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
3 packages can be upgraded. Run 'apt list --upgradable' to see them.
Muốn xem packages nào upgradable, chạy apt list --upgradable:
kris@pihole:~ $ apt list --upgradable
Listing... Done
libcamera0/stable 0~git20220426+18e68a9b-1 armhf [upgradable from: 0~git20220303+e68e0f1e-1]
raspi-config/stable 20220425 all [upgradable from: 20220419]
rpi-eeprom/stable 13.13-1 armhf [upgradable from: 13.12-1]
Nếu không có newer versions packages/dependencies trong repository, output như:
kris@pihole:~ $ sudo apt-get update
Get:1 https://download.docker.com/linux/raspbian bullseye InRelease [26.7 kB]
Hit:2 https://ftp.harukasan.org/raspbian/raspbian bullseye InRelease
Hit:3 https://packages.azlux.fr/debian buster InRelease
Hit:4 https://archive.raspberrypi.org/debian bullseye InRelease
Fetched 26.7 kB in 3s (8,789 B/s)
Reading package lists... Done
Lưu ý: không mention packages upgradable, không note về apt list --upgradable.
Nhưng không nhất thiết nghĩa là không outdated software trên system, chỉ là bạn đã có latest package list. Có thể bạn chạy sudo apt-get update multiple times.
Luôn chạy apt list --upgradable lại để xem có gì upgradable.
Hoặc dùng modern sudo apt update command. Lệnh này luôn show số packages upgradable, hoặc note everything up to date.
Thêm info về differences apt và apt-get, xem section dưới.
Cách dùng lệnh sudo apt-get upgrade
Sau khi chạy sudo apt-get update, cùng terminal window, gõ sudo apt-get upgrade, nhập password nếu cần, enter.
Output tương tự:
kris@pihole:~ $ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
libcamera0 raspi-config rpi-eeprom
3 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,616 kB of archives.
After this operation, 1,596 kB of additional disk space will be used.
Do you want to continue? [Y/n]
Phần dưới output, thấy packages sẽ upgraded:
The following packages will be upgraded:
libcamera0 raspi-config rpi-eeprom
3 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Data cần fetch, storage space upgraded packages sẽ dùng:
Need to get 2,616 kB of archives.
After this operation, 1,596 kB of additional disk space will be used.
Cuối cùng, prompt ask continue upgrade:
Do you want to continue? [Y/n]
Nhập y, Y, hoặc yes continue, hoặc n, N, no exit.
Nếu exit, output như:
kris@pihole:~ $ sudo apt-get upgrade
...
Do you want to continue? [Y/n] n
Abort.
Nếu continue, output dài:
kris@pihole:~ $ sudo apt-get upgrade
...
Do you want to continue? [Y/n] y
Get:1 https://archive.raspberrypi.org/debian bullseye/main armhf libcamera0 armhf 0~git20220426+18e68a9b-1 [548 kB]
Get:2 https://archive.raspberrypi.org/debian bullseye/main armhf raspi-config all 20220425 [30.3 kB]
Get:3 https://archive.raspberrypi.org/debian bullseye/main armhf rpi-eeprom armhf 13.13-1 [2,037 kB]
Fetched 2,616 kB in 3s (1,019 kB/s)
Reading changelogs... Done
(Reading database ... 43496 files and directories currently installed.)
Preparing to unpack .../libcamera0_0~git20220426+18e68a9b-1_armhf.deb ...
Unpacking libcamera0:armhf (0~git20220426+18e68a9b-1) over (0~git20220303+e68e0f1e-1) ...
Preparing to unpack .../raspi-config_20220425_all.deb ...
Unpacking raspi-config (20220425) over (20220419) ...
Preparing to unpack .../rpi-eeprom_13.13-1_armhf.deb ...
Unpacking rpi-eeprom (13.13-1) over (13.12-1) ...
Setting up rpi-eeprom (13.13-1) ...
Setting up libcamera0:armhf (0~git20220426+18e68a9b-1) ...
Setting up raspi-config (20220425) ...
Processing triggers for man-db (2.9.4-2) ...
Processing triggers for libc-bin (2.31-13+rpt2+rpi1+deb11u2) ...
Hoàn tất, tất cả outdated packages/dependencies updated.
Lưu ý quan trọng: sudo apt-get upgrade chỉ upgrade gì có thể without removing anything.
Ví dụ, nếu upgrade requires new dependency, upgrade download và install nó, nhưng không remove old dependency. Remove old dependencies cần lệnh khác. Thấy thường khi upgrade kernel version.
Nếu thấy message sau upgrade:
The following packages were automatically installed and are no longer required:
g++-8 gir1.2-mutter-4 libapache2-mod-php7.2 libcrystalhd3
Use 'sudo apt autoremove' to remove them.
Follow suggestion dùng sudo apt autoremove remove unnecessary packages.
Cách dùng special options với sudo apt-get upgrade
Có nhiều special options/parameters cho sudo apt-get upgrade, nhưng hai stand out: --dry-run và --yes.
Cách dùng --dry-run:
--dry-run (alternatively -s hoặc --simulate) simulate upgrade process nhưng không thay đổi gì trên system:
kris@pihole:~ $ sudo apt-get upgrade --dry-run
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
libcamera0 raspi-config rpi-eeprom
3 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Inst libcamera0 [0~git20220303+e68e0f1e-1] (0~git20220426+18e68a9b-1 Raspberry Pi Foundation:stable [armhf])
Inst raspi-config [20220331] (20220425 Raspberry Pi Foundation:stable [all])
Inst rpi-eeprom [13.12-1] (13.13-1 Raspberry Pi Foundation:stable [armhf])
Conf libcamera0 (0~git20220426+18e68a9b-1 Raspberry Pi Foundation:stable [armhf])
Conf raspi-config (20220425 Raspberry Pi Foundation:stable [all])
Conf rpi-eeprom (13.13-1 Raspberry Pi Foundation:stable [armhf])
Dù Debian-based distros rất stable, option useful nếu muốn ensure no conflicts during upgrade.
Cách dùng --yes:
--yes (alternatively -y hoặc --assume-yes) tự động answer yes to prompts nếu safe:
kris@pihole:~ $ sudo apt-get upgrade --yes
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
libcamera0 raspi-config rpi-eeprom
3 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,616 kB of archives.
After this operation, 1,596 kB of additional disk space will be used.
Get:1 https://archive.raspberrypi.org/debian bullseye/main armhf libcamera0 armhf 0~git20220426+18e68a9b-1 [548 kB]
Get:2 https://archive.raspberrypi.org/debian bullseye/main armhf raspi-config all 20220425 [30.3 kB]
Get:3 https://archive.raspberrypi.org/debian bullseye/main armhf rpi-eeprom armhf 13.13-1 [2,037 kB]
...
Processing triggers for libc-bin (2.31-13+rpt2+rpi1+deb11u2) ...
Lưu ý Do you want to continue? [Y/n] skipped, all packages upgraded.
FAQs
sudo và apt-get là gì?
Lưu ý quan trọng: sudo apt-get update và sudo apt-get upgrade gồm ba phần: sudo, apt-get, update hoặc upgrade.
sudo = "superuser do", cho phép run programs với root/admin privileges.
Ví dụ reboot system requires superuser/root privileges, chạy reboot trong terminal might return errors:
Failed to set wall message, ignoring: Interactive authentication required.
Failed to reboot system via logind: Interactive authentication required.
Failed to open initctl fifo: Permission denied
Failed to talk to init daemon.
Nhưng chạy sudo reboot, nhập admin password, bạn run reboot as superuser, system restart immediately.
apt-get là command line tool trong Debian và Debian-based distros dùng install và manage packages.
Khác biệt apt-get và apt?
apt là modern tool hơn install/manage applications trên Debian-based distros.
Phần lớn, apt và apt-get interchangeable – sudo apt update và sudo apt-get update cả hai update package list.
Main differences: apt easier to type, output generally more useful, include user-friendly features như progress bar khi install packages.
Dù examples dùng apt-get, tôi strongly encourage dùng apt thay thế.
sudo apt-get update và sudo apt-get upgrade safe to use?
Yes, Debian và Debian-based distros generally very stable, update và upgrade commands safe. Vì major updates packages/dependencies, và distros themselves, chỉ release once/twice a year.
Downside: unlike bleeding edge distros như Arch Linux, nếu muốn most recent version package, might need extra work. Configure third-party repository via PPA, alternative packaging system như Snap/Flatpak, hoặc compile package yourself.
Nhưng stability với slightly older software worth it, ít nhất theo tôi.
Có chain sudo apt-get update và sudo apt-get upgrade không?
Có thể tedious chạy sudo apt-get update, wait, rồi sudo apt-get upgrade?
Cả hai chạy pretty quickly, nhưng sometimes easier execute string of commands check back vài phút sau.
Với operator &&, chain multiple commands:
sudo apt-get update && sudo apt-get upgrade
Quan trọng với &&: command sau operator chỉ run nếu command trước successful.
Ví dụ trên, sudo apt-get upgrade chỉ run nếu sudo apt-get update succeeds. Nếu error (network problem updating package list), sudo apt-get upgrade skipped.
sudo apt-get dist-upgrade và sudo apt full-upgrade là gì, safe không?
Theo Stack Overflow thread, commands do same thing under the hood – upgrade outdated packages, và intelligently remove some packages whenever necessary.
Essentially combination của sudo apt-get upgrade và sudo apt autoremove.
Running these commands should be safe in most cases.
Nhưng nhiều người, myself included, recommend sudo apt-get update và sudo apt-get upgrade. Bạn có more chance review upcoming changes, và vì upgrade never removes packages, less destructive.
./thanks_for_reading.sh
Nếu breakdown này useful, share với friends để more people benefit.
Cũng free to reach out on Twitter cho tôi biết thoughts.