apt 的 invalid signatures / public key is not available (NO_PUBKEY/EXPKEYSIG/KEYEXPIRED) 問題處理方式

症狀 – apt / apt-get 操作時出現以下錯誤:

Err:4 https://dl.winehq.org/wine-builds/ubuntu xenial InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 76F1A20FF987672F
Reading package lists... Done
W: GPG error: https://dl.winehq.org/wine-builds/ubuntu xenial InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 76F1A20FF987672F
E: The repository 'https://dl.winehq.org/wine-builds/ubuntu xenial InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://deb.torproject.org/torproject.org bionic InRelease: The following signatures were invalid: EXPKEYSIG 74A941BA219EC810 deb.torproject.org archive signing key
W: Failed to fetch https://deb.torproject.org/torproject.org/dists/bionic/InRelease  The following signatures were invalid: EXPKEYSIG 74A941BA219EC810 deb.torproject.org archive signing key
W: Some index files failed to download. They have been ignored, or old ones used instead.
W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: http://apt.puppetlabs.com trusty Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 7F438280EF8D349F
W: Failed to fetch http://apt.puppetlabs.com/dists/trusty/Release
W: Some index files failed to download. They have been ignored, or old ones used instead.

可以看到上面缺的 key 分別是 76F1A20FF987672F74A941BA219EC8107F438280EF8D349F ,這種情況有可能是新增了新的 apt repository 但沒有對應的 key,或是舊有的 apt repository 所使用的 key已經過期了

通常可以到對應軟體提供者的網站上找到正確的 key ,並放到 /etc/apt/trusted.gpg 這個檔案、或 /etc/apt/trusted.gpg.d 目錄下,或是透過 apt-key 的指令從 keyserver 進行下載匯入,像是這樣(以 Yarn 和 Google Cloud 的套件庫為例):

  • curl -sS https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add –
  • curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

我們也可以透過將有問題/闕漏的 key 直接拿去問 OpenPGP keyserver ,透過 apt-key adv --keyserver keyserver.ubuntu.com --recv-keys XXXXXXXX 的指令,像這樣:

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7F438280EF8D349F
Executing: /tmp/apt-key-gpghome.VyE6MFMrnc/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys 7F438280EF8D349F
gpg: key 7F438280EF8D349F: public key "Puppet, Inc. Release Key (Puppet, Inc. Release Key) <release@puppet.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 74A941BA219EC810
Executing: /tmp/apt-key-gpghome.lRWvuhWnPw/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys 74A941BA219EC810
gpg: key EE8CBC9E886DDD89: public key "deb.torproject.org archive signing key" imported
gpg: Total number processed: 1
gpg:               imported: 1
閱讀全文

A rapid way to erase multiple partition tables on Ubuntu/Debian Linux

When doing labs / playing with ZFS on Linux, ZFS pool may creates a few partitions on each drive, we may want to erase all the playground partition table to start from scratch again(though it may not be required), if we have many drives like more than 10, and we want to erase them all, it’ll take a while to finish the job.

Here is a rapid method to do it, using the non-interactive command sgdisk from package gdisk – the GPT fdisk text-mode partitioning tool, with parameter -Z or -o (both work in my case):

$ sudo sgdisk -Z /dev/sda
GPT data structures destroyed! You may now partition the disk using fdisk or other utilities.
$ sudo sgdisk -o /dev/sdb
The operation has completed successfully.

According to the help message, by specifing parameters -Z, it’ll zap (destroy) the GPT and MBR data structures and exit, and by specifing parameters -o, it’ll clear out all partition data. This includes GPT header data, all partition definitions, and the protective MBR.

Because the command sgdisk is non-interactive, so we can simply integrate the whole massive partition clean up job with shell scripts, for example:

#!/bin/bash
sudo sgdisk -Z /dev/sda &
sudo sgdisk -Z /dev/sdb &
sudo sgdisk -Z /dev/sdc &
sudo sgdisk -Z /dev/sdd &
sudo sgdisk -Z /dev/sde &
sudo sgdisk -Z /dev/sdf &
sudo sgdisk -Z /dev/sdg &

That’s it! Happy hacking!

在 Debian / Ubuntu GNU/Linux 底下找出特定命令 / 程式的來源套件

在 Debian / Ubuntu based GNU/Linux 底下做事有個很棒的優點,有太多工具可以透過 apt 套件庫來進行安裝,重灌電腦的時候只要透過 apt 或 apt-get 就可以把一大半會需要用到的工具給裝起來,不過有時候可能邊摸索邊上網查資料來看某工具要怎麼用,一骨腦裝完卻沒作筆記,日後要準備重灌時卻想不起來該裝什麼套件才能把要用到的命令找回來,窘 …

如果你有安裝 command-not-found 這個套件,在 bash shell 底下如果執行了找不到的命令,除了常見的 blah: command not found,他還會有很貼心的提示使用者應該安裝哪個缺漏的套件,像這樣:

$ kvm
 The program 'kvm' is currently not installed. You can install it by typing:
 sudo apt install qemu-kvm

上面範例為我執行了一個不存在的命令 – kvm ,然後 command-not-found 這套工具建議我去裝 qemu-kvm 這個套件,裝完後就會有 kvm 這個命令可以使用了,如果同時存在不只一個套件包含了同樣的命令,command-not-found 這套工具一樣會很好心的幫我們列出來,例如:

$ sar
 The program 'sar' can be found in the following packages:
  * sysstat
  * atsar
 Ask your administrator to install one of them

就算是找不到 100% 符合的命令,command-not-found 也會很好心的列出其他參考,例如:

$ gitx
 No command 'gitx' found, did you mean:
  Command 'gitg' from package 'gitg' (universe)
  Command 'git' from package 'git' (main)
  Command 'gitk' from package 'gitk' (main)
 gitx: command not found

不過上述的功能僅限於我們沒有正確的安裝對應的套件才能使用,如果是套件已經裝起來了的時候呢?例如我工具都已經裝好了,只是想確認某個命令是從哪裡來的,要做個筆記而已,這種情況很常見的啊,例如遇到問題跑去問 Google ,看到一堆解法,一個不行換一個,換到可以的解法的時候可能已經 apt-get install 不知道多少次了,已經忘記剛剛的命令到底是從哪邊來的了XD

還好 dpkg 有個強大的搜尋功能,可以讓我們從已經安裝的套件中搜尋出他們到底包含了哪些檔案,底下是從 Ubuntu 16.04.1 裏面擷取的 dpkg manpage:

-S, –search filename-search-pattern…
Search for a filename from installed packages.

使用方式為:

$ dpkg -S '檔案路徑/名稱'

要透過 dpkg -S 來找命令對應所屬的套件,只要把命令的完整路徑丟給他就可以了,命令的完整路徑我們可以透過 type 或 which 這兩個指令來確認,例如:

$ type whoami
whoami is /usr/bin/whoami
$ which whoami
/usr/bin/whoami

從上面的輸出我們可以知道 whoami 這個命令的完整路徑是 /usr/bin/whoami ,再把 /usr/bin/whoami 拿去 dpkg 查詢就可以了!例如:

$ dpkg -S '/usr/bin/whoami'
coreutils: /usr/bin/whoami
$ dpkg -S '/usr/bin/curl'
curl: /usr/bin/curl

或是直接

$ dpkg -S "$(which whoami)"
coreutils: /usr/bin/whoami
$ dpkg -S "$(which curl)"
curl: /usr/bin/curl

從上面的結果我們可以發現 curl 這個命令是從 curl 這個套件來的, whoami 這個命令則是從 coreutils 這個套件來的,如果你查詢的是還沒有安裝或是不是透過 apt / dpkg 套件管理工具安裝的命令,那就沒辦法了:

$ dpkg -S '/usr/bin/kvm'
dpkg-query: no path found matching pattern /usr/bin/kvm

要注意的是

1. which 跟 type 輸出的格式不太一樣,type 的輸出結果因為多了 blah is … ,所以還需要經過處理把前面那段拿掉才有辦法餵給 dpkg ,而 which 因為是直接出出路徑所以沒有這問題

2. 這邊如果輸入的不是完整路徑,則可能會出現一堆其他不相關的東西,例如:

$ dpkg -S 'whoami'
 bash-completion: /usr/share/bash-completion/completions/ldapwhoami
 coreutils: /usr/share/man/man1/whoami.1.gz
 coreutils: /usr/bin/whoami

因為 dpkg 會直接把所有完整路徑裏面符合輸入字串的部份都印出來,所以像是 whoami 的 manpage 以及 bash-completion 的自動補齊指令都被列出來了。

好了,這篇筆記雖然短,可我覺得很實用,哪天失憶或是被問問題的時候就可以把這篇丟出來,這就是做筆記最大的用途 … :D

LAMP Web Server 網頁伺服器快速建置入門

前言 …

LAMP 是透過 Linux + Apache + MySQL + PHP 這種方式來架設網站伺服器組合的簡稱,分別代表著

  1. Linux 作業系統
  2. Apache http 網頁伺服器
  3. MySQL 資料庫
  4. PHP 網頁後端語言

LAMP 是非常廣泛被使用的網站環境建置組合,也有將其中的元件替換為其它相同功能但不同產品的組合,例如把 Linux 改為 FreeBSD 或 Windows 的 FAMP、WAMP,把 Apache 改為 NGINX 的 LNMP 等,本篇是使用 Ubuntu / Debian 作為示範,但基本上基於 Debian 以及 Ubuntu 的 GNU/Linux distributions 操作方式都大同小異,要注意不同版本的使用上多少會略有差異,本篇僅供參考。

根據撰寫本篇紀錄時 W3Techs 的統計,Linux 在 web server 的作業系統市佔率高達 36.2% ( Unix 佔總共的 68%、Linux 佔其中的 53.2% ),其中 Debian / Ubuntu 就分別佔了 Linux 的 32.6% / 31.1%:

Apache 在 Web Server 的領域有著 55% 過半的驚人市佔率:

最新、更多資料請參考 W3Techs :

寫這篇的原因,主要是因為推薦不少人來使用 WordPress 撰寫部落格、筆記,WordPress 在此時已經擁有高達 26% 的網頁套裝軟體市佔率,而且持續成長中,平均每四個網站就有一個是使用 WordPress 架設,可想而知 WordPress 不管是在社群、生態系方便都已經相當成熟,考量到自行架設 WordPress 會比直接使用 WordPress.com 現成的服務來的更有彈性、功能更加強大,同時又可以學技術,但對於初學者對於遇到問題的恐懼一直是最大的阻礙,故撰寫這份 LAMP 簡易安裝筆記供入門者參考,對於其他同樣使用 PHP 語言的網站平台基本上都適用。

閱讀全文