Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม Bash

sudo apt-get update vs upgrade: ต่างกันอย่างไร? - คู่มือชัดเจน

sudo apt-get update และ sudo apt-get upgrade เป็น 2 คำสั่งใช้เก็บ packages ทั้งหมด up to date ใน Debian หรือ Debian-based Linux distribution

เป็น common commands สำหรับ Linux admins และคนทำ DevOps แต่ handy ไว้รู้ถึงแม้คุณไม่ค่อยใช้ command line

ในบทความนี้จะอธิบายว่าทั้ง 2 คำสั่งทำอะไร วิธีใช้ และ FAQs

ความแตกต่างระหว่าง sudo apt-get update และ sudo apt-get upgrade คืออะไร?

ความแตกต่างหลักคือ sudo apt-get update ดึง latest version ของ package list จาก software repository ของ distro และ third-party repositories ที่ configure ไว้ กล่าวคือ มันจะคำนวณ latest version ของแต่ละ package และ dependency แต่จะ ไม่ download หรือ install updates เหล่านั้นจริงๆ

คำสั่ง sudo apt-get upgrade download และ install updates สำหรับ outdated packages และ dependencies บนระบบ แต่แค่รัน sudo apt-get upgrade จะ ไม่ upgrade outdated packages อัตโนมัติ – คุณยังมีโอกาส review changes และ confirm ว่าอยาก upgrade จริงไหม

วิธีใช้คำสั่ง sudo apt-get update

ใน Debian-based Linux distro (Debian, Ubuntu, Linux Mint, Kali Linux, Raspberry Pi OS, ฯลฯ) เปิด terminal window

ขึ้นอยู่กับ distro terminal อาจชื่อต่างกัน ขึ้นอยู่กับวิธีเปิด เช่น Ubuntu และ Linux Mint default terminal คือ Gnome Terminal แต่อาจอยู่ใน application menu ใต้ Terminal

ใน terminal พิมพ์ sudo apt-get update บน command line พิมพ์ admin password กด Enter

ถ้ามี updates จะเห็น output ประมาณนี้:

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.

ถ้าอยากดูว่าฟีเจอร์ไหน upgrade ได้ รัน 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]

แต่ถ้าไม่มี newer versions ใน repository จะเห็น output แบบนี้:

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

สังเกตว่าไม่มี mention packages ที่ upgrade ได้ และไม่มี note ให้รัน apt list --upgradable

แต่นี่ไม่ได้หมายความว่าไม่มี outdated software บนระบบ แค่คุณได้ latest package list แล้ว อาจรัน sudo apt-get update หลายครั้ง

รัน apt list --upgradable อีกครั้งเช็คได้เสมอ

หรือใช้ sudo apt update ที่ modern กว่า คำสั่งนี้จะแสดงจำนวน packages ที่ upgrade ได้เสมอ หรือ note ว่าทุกอย่าง up to date

อ่านเพิ่มเติมเกี่ยวความแตกต่าง apt และ apt-get ในส่วนด้านล่าง

วิธีใช้คำสั่ง sudo apt-get upgrade

หลังรัน sudo apt-get update ใน terminal เดียวกัน พิมพ์ sudo apt-get upgrade พิมพ์ password ถ้าจำเป็น กด enter

จะเห็น output ประมาณนี้:

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] 

ด้านล่าง output จะเห็น packages ที่จะ upgrade:

The following packages will be upgraded: libcamera0 raspi-config rpi-eeprom 3 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

ข้อมูลที่ต้อง fetch และพื้นที่ disk ที่ upgraded packages จะใช้:

Need to get 2,616 kB of archives. After this operation, 1,596 kB of additional disk space will be used.

และสุดท้าย prompt ถามว่าอยาก continue upgrade หรือไม่:

Do you want to continue? [Y/n] 

พิมพ์ y, Y, หรือ yes เพื่อ continue หรือ n, N, no เพื่อออกจาก upgrade command

ถ้าเลือกออก จะเห็น output แบบนี้:

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] n Abort.

ถ้าเลือก continue จะเห็น long output:

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] 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) ...

จบแล้ว outdated packages และ dependencies ทั้งหมดจะ updated

สำคัญ: sudo apt-get upgrade upgrade แค่สิ่งที่ทำได้โดยไม่ remove อะไร

เช่น ถ้า upgrade ต้องการ new dependency upgrade จะ download และ install แต่จะ ไม่ remove old dependency การ remove old dependencies ต้องคำสั่งอื่น จะเห็นบ่อยตอน upgrade kernel version

ถ้าเห็น message แบบนี้หลัง 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 ใช้ sudo apt autoremove ลบ packages ที่ไม่จำเป็น

วิธีใช้ special options กับ sudo apt-get upgrade

มี special options/parameters หลายตัวใช้กับ sudo apt-get upgrade แต่ 2 ตัวโดดเด่น: --dry-run และ --yes

วิธีใช้ --dry-run Option:

--dry-run (หรือ -s หรือ --simulate) simulate สิ่งที่จะเกิดขึ้นระหว่าง upgrade แต่ ไม่ change anything บนระบบ:

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])

Debian และ Debian-based distros มัก stable แต่อย่างไรก็ตาม option นี้ useful ถ้าอยากแน่ใจว่าไม่มี conflicts ระหว่าง upgrade

วิธีใช้ --yes Option:

--yes (หรือ -y หรือ --assume-yes) ตอบ yes อัตโนมัติให้ prompts ถ้าปลอดภัย:

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) ...

สังเกต Do you want to continue? [Y/n] ถูกข้าม และ packages ทั้งหมด upgraded

FAQs

sudo และ apt-get คืออะไร?

สำคัญ: sudo apt-get update และ sudo apt-get upgrade ทั้งคู่ประกอบด้วย 3 ส่วน: sudo, apt-get, และ update หรือ upgrade

sudo ย่อมาจาก "superuser do" ให้รัน programs ด้วย root หรือ admin privileges

เช่น reboot system ต้องการ superuser/root privileges รัน reboot ใน terminal อาจ 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.

แต่รัน sudo reboot พิมพ์ admin password system จะ restart ทันที

apt-get คือ command line tool ใน Debian และ Debian-based Linux distros ใช้ install และ manage packages

ความแตกต่างระหว่าง apt-get และ apt คืออะไร?

apt เป็น modern tool สำหรับ install และ manage applications บน Debian และ Debian-based distros

ส่วนใหญ่ apt และ apt-get ใช้แทนกันได้ – sudo apt update และ sudo apt-get update ทั้งคู่ update package list บนระบบ

ความแตกต่างหลัก: apt พิมพ์ง่ายกว่า output มีประโยชน์กว่า และมี user-friendly features เช่น progress bar ตอน install packages

แม้ตัวอย่างในบทความส่วนใหญ่ใช้ apt-get แต่ขอแนะนำให้ใช้ apt แทน

sudo apt-get update และ sudo apt-get upgrade ปลอดภัยไหม?

ใช่ Debian และ Debian-based distros โดยทั่วไป stable มาก และคำสั่ง update และ upgrade ปลอดภัย เพราะ major updates สำหรับ packages/dependencies และ distros เอง release แค่ 1-2 ครั้งต่อปี

Downside คือ ไม่เหมือน bleeding edge distros เช่น Arch Linux หากอยากใช้ most recent version ของ package อาจต้องทำ extra work configure third-party repository via PPA, ใช้ alternative packaging system เช่น Snap หรือ Flatpak, หรือ compile package เอง

แต่ stability ที่มากับ software ที่เก่าลงเล็กน้อย คุ้มค่า ตามความคิดเห็นของผม

Chain sudo apt-get update และ sudo apt-get upgrade ได้ไหม?

อาจคิดว่า รัน sudo apt-get update รอเสร็จ แล้วรัน sudo apt-get upgrade เหนื่อยไหม

ทั้งคู่รันเร็วมาก แต่บางครั้ง execute string of commands แล้วเช็คภายหลังสะดวกกว่า

ด้วย operator && chain multiple commands ได้:

sudo apt-get update && sudo apt-get upgrade

สำคัญกับ && operator: command หลัง operator รันต่อก็ต่อเมื่อ command ก่อนหน้า successful

ตัวอย่างข้างต้น sudo apt-get upgrade รันต่อก็ต่อเมื่อ sudo apt-get update succeeds หากมี error เช่น network problem ระหว่าง update package list sudo apt-get update จะ skipped

sudo apt-get dist-upgrade และ sudo apt full-upgrade คืออะไร และปลอดภัยไหม?

ตาม Stack Overflow thread นี้ ทั้งคู่ทำ same thing under the hood – upgrade outdated packages และ intelligently remove some packages เมื่อจำเป็น

ก็คือ combination ของ sudo apt-get upgrade และ sudo apt autoremove

รันคำสั่งเหล่านี้ should safe ใน most cases

แต่มากมาย (รวมถึงผม) แนะนำ sudo apt-get update และ sudo apt-get upgrade แทน มีโอกาส review upcoming changes มากกว่า และ upgrade ไม่เคย remove packages ทำให้ less destructive

./thanks_for_reading.sh

ถ้าคุณพบ breakdown นี้เกี่ยว sudo apt-get update และ sudo apt-get upgrade useful โปรด share ให้เพื่อนๆ เพื่อคนมากขึ้นได้ประโยชน์

อย่าลืม reach out บน Twitter บอกความคิดเห็นนะครับ