ubuntu的apt與gpg

最近因為在 Ubuntu 更新的時候,老是出現 gpg pub key 錯誤,有問題的套件庫是 hashicorp 跟 yarn 的套件庫,今天終於下定決心處理。

第一個找到的是這篇:How to configure HashiCorp repository

處理方法是這樣的,先下載 gpg key,然後用 sudo gpg 匯入

wget --quiet --output-document - https://apt.releases.hashicorp.com/gpg | \
  sudo gpg --no-default-keyring --keyring gnupg-ring:/usr/share/keyrings/hashicorp-archive-keyring.gpg --import

然後檢查 /usr/share/keyrings/hashicorp-archive-keyring.gpg

ls -l /usr/share/keyrings/hashicorp-archive-keyring.gpg

permission 結果應該要是 644 。

接著檢查 source list 檔案,裏面會有 signed-by 的字串

deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com jammy main

基本上這樣就解決了,yarn 的處理方式也一樣。

wget --quiet --output-document - https://dl.yarnpkg.com/debian/pubkey.gpg | sudo gpg --no-default-keyring --keyring gnupg-ring:/usr/share/keyrings/yarnkey.gpg --import

只是這邊做完,還是有問題,後來才靈機一動想到要檢查 /usr/share/keyrings/yarnkey.gpg 的 permission,發現是 600,用 chmod 改為 644 以後,apt update 就沒問題了。

Debian/Ubuntu 自動更新

要如何設定 Debian/Ubuntu 自動做更新呢?這可以使用 dpkg-reconfigure -plow unattended-upgrade 來設定

sudo dpkg-reconfigure -plow unattended-upgrades

執行指令後,會跳出視窗詢問是否要自動更新,選擇 Yes 就可以了。執行以後,會更新 /etc/apt/apt.conf.d/20auto-upgrades 這個檔案的內容

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

如果有不想自動更新的套件,可以用黑白名單來設置,這邊可以參考 /etc/apt/apt.conf.d/50unattended-upgrades 檔案裡的範例來設定。

有些套件在更新後,會需要重新開機,unattended-upgrade 也可以做到自動重開機,主要是在 /etc/apt/apt.conf.d/20auto-upgrades 加入以下設定

//Unattended-Upgrade::Automatic-Reboot "false";
//Unattended-Upgrade::Automatic-Reboot-WithUsers "true";
//Unattended-Upgrade::Automatic-Reboot-Time "02:00";

除了可以指定是否自動重開機外,也可以指定自動重開機的時間。

了解 Debian/Ubuntu 如何自動更新以後,相信在管理主機上會更為輕鬆。

參考資料

tuned-套用適合的系統設定

RHEL 跟 Ubuntu 都有提供這個系統服務,可以啟用這個系統服務來微調系統的設定,以達到預期的效能。

安裝

印象中,RHEL8 預設已經安裝,若沒有,可以用以下指令來安裝

yum install -y tuned

Ubuntu 可以使用以下指令來安裝

sudo apt install tuned

使用

使用相當簡單,啟動系統服務以後就可以了。

systemctl start tuned

之後就可以使用 tuned-adm 來做一些操作,這邊先說明一下什麼是 profile 。

profile 就是一組針對特定用途所提供的系統設定,例如網路最佳化或是資料庫最佳化等等的,不同的用途要調整的設定也不一樣。在啟動 tuned 以後,會啟用預設的 profile ,以 ubuntu 來說,是啟動 balanced 這個 profile。

tuned-adm 有以下子指令可以使用,以進行 profile 的操作:

子指令用途
list列出所有可用的 profile,每個 Linux 發行版所提供的 profile 都不太一樣。
active列出目前啟用的 profile
off關閉所有調整
profile切換到指定的 profile
profile_info顯示指定的 profile 資訊
recommend顯示建議的 profile
verify檢查
auto_profile自動選擇 profile
profile_mode顯示目前是自動選擇 profile 還是手動指定 profile

如果要客製化自己的 profile,也是可以的,這邊可以參考 /usr/lib/tuned 裡的 profile 來做複製跟改動。

Ubuntu LivePatch

Ubuntu LivePath 是一個動態修補 kernel 漏洞的機制。在傳統作法上,如果修補、更新了 kernel ,就一定要重新開機,讓新的 kernel 生效才能真正的修補漏洞。LivePatch 則提供了一個不需要重開機的方法,他會動態的修補在目前的 kernel 裡。在 ubuntu 的 How kernel livepatching works 有圖片說明。

Canonical 很佛心的提供了 Personal token,只要去 ubuntu.com 註冊,就可以取得。

取得 token 以後,就可以使用以下指令啟用

sudo ua attach <token>
sudo ua enable livepatch

啟用後,若想知道目前套用了哪些修補,可以用 canonical-livepatch 指令察看

canonical-livepatch status

若是公司使用,會需要購買企業訂閱。若是想把修補放到公司內部來節省頻寬,可以參考 Livepatch on-prem 的說明來進行。我大致看了一下,說是會把修補放到 S3 上面,離線的作法或許可以架設 minio 來代替 S3。這部份因為沒用到,就先跳過不看了。

SSH登入使用2FA驗證

Ubuntu 跟 RHEL 的設定方法大同小異:

  • Ubuntu: sudo apt install libpam-google-authenticator
  • RHEL: sudo yum install -y google-authenticator qrencode

步驟1

google-authenticator 產生所需的設定檔。

產生出來的檔案會在 ~/.google_authenticator

步驟2

設定 /etc/pam.d/sshd

加入

auth required pam_google_authenticator.so

步驟3

在 SSHD 設定檔 /etc/ssh/sshd_config 裡找到 ChallengeResponseAuthentication ,將其後的值改為 yes

ChallengeResponseAuthentication yes

修改完成後,重新啟動 sshd。

有陷阱!!

大致就這樣,在 RHEL8 會踩到 SELinux 的雷,這邊要修改 /etc/pam.d/sshd 的設定,將之前加入的那行改為

auth required pam_google_authenticator.so secret=/home/${USER}/.ssh/.google_authenticator

然後把 ~/.google_authenticator 檔案搬到 ~/.ssh/google_authenticator

處理一下 SELinux 權限

restorecon -Rv ~/.ssh/google_authenticator

然後一樣,重新啟動 sshd

圖形介面

參考資料裡的後半段有關於 GUI 登入的設定,這邊我就沒試過了。

參考資料

在 Ubuntu 開發 .Net 應用程式

這幾年微軟大力推廣 .Net core ,把 .Net runtime 鋪到每個 Linux 發行版去,所以現在 Ubuntu、Red Hat Enterprise、Debian、Fedora、CentOS、Alpine 等 Linux 發行版都可以安裝 .Net runtime 跟 .Net SDK 。

以 Ubuntu 來說,安裝方法很簡單,就是照這篇 在Ubuntu 上安裝 .NET SDK 或 .NET 執行時間 來做就可以。

我桌機安裝的是 Ubuntu 21.10 ,所以下面就只節錄 Ubuntu 21.10 的部份

wget https://packages.microsoft.com/config/ubuntu/21.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get update; \
  sudo apt-get install -y apt-transport-https && \
  sudo apt-get update && \
  sudo apt-get install -y dotnet-sdk-6.0 aspnetcore-runtime-6.0

安裝完成以後,就可以使用 dotnet 這個指令來開發應用程式了。

首先建立目錄,然後切換到此目錄下

mkdir -p ~/dotnetcore-hello
cd ~/dotnetcore-hello

然後用 dotnet 指令建立範本

dotnet new webapp -n dotnetcore-hello -o .
  • webapp 表示建立網頁應用程式。
  • -n 是表示專案名稱。
  • -o . 是表示輸出到目前的目錄。

產生完畢,就可以啟動。

dotnet run

啟動以後會有以下訊息

正在建置...
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7219
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5063
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /home/user/dotnetcore-hello/

從訊息可以知道,用瀏覽器開啟 http://localhost:5063 或是 https://localhost:7219 就可以開啟網站。

一般在開發時,會把 port 固定起來,以方便開發;或者是禁用 https 。那這樣該怎麼做呢?這時候可以參考 How do I disable HTTPS in ASP.NET Core 2.1 + Kestrel? ,dotnet 應用程式的 port 跟 https 設定是在 Properties/launchSettings.json 裏面去指定的,只要修改裡面的 applicationUrl 即可。

例如 applicationUrl 本來是 https://localhost:7219;http://localhost:5063,改為只有 http,port 8000 並讓網路上其他電腦都可以連進來的話,就改為 http://0.0.0.0:8000

最後,開發完畢以後,要輸出檔案去佈署,可以用 dotnet publish 來建置。

dotnet publish -c release -o out

以上面的指令來說,

  • -c 是指定要使用的 configuration
  • -o 是指定把建置好的檔案輸出到 out 目錄下。

今天就把 QuickStart 的開發流程紀錄起來,免得以後遇到又在那邊查。

如何在 multipass VM內執行 GUI 應用程式

multipass 是 Canonical 所推出的一個輕量級 VM 管理工具,它跨平台,支援 Linux、Windows 跟 MacOS。

這個工具用一陣子了,相當的好用,缺點是目前只有 Ubuntu 的 VM 可用。之前是想到,既然 multipass shell 是用 ssh 登入,那麼應該要可以 Forward X11 才對。也就是說以 SSH 登入以後,執行 GUI 應用程式,畫面會轉向到目前 Host 的 X Server 。

但試了以後卻不行,請教 Google 以後,Google 告訴我 StackOverflow 上已經有人解答了:18.04 – How to open GUI applications in multipass container(guest = ubuntu, host = ubuntu)? X-Forwarding – Ask Ubuntu

先說明一下我的環境:

  • Ubuntu 20.04
  • 用 snap 安裝 multipass

首先,先取得 ssh key,因為 multipass 是用 snap 安裝的,所以得從 /var/snap/multipass 裏面找到 ssh private key,把它複製到 ~/.ssh/multipassKey 目錄下。

    mkdir ~/.ssh/multipassKey
    sudo cp /var/snap/multipass/common/data/multipassd/ssh-keys/id_rsa ~/.ssh/multipassKey/id_rsa

接著變更這個 private key 的權限為 0600,並且變更擁有者

sudo chown user -R ~/.ssh/multipassKey
sudo chmod 0600 ~/.ssh/multipassKey/id_rsa

再來啟動 VM,並且察看 VM 的 IP

multipass start name
multipass ls

最後使用 ssh 登入

ssh -X -i ~/.ssh/multipassKey/id_rsa ubuntu@<my_vm_ip>

就這樣。

Ubuntu 14.04 + Python 3.7.2

我是使用 pyenv 來安裝,pyenv 的安裝:

# 一鍵安裝
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

在安裝 Python 3.7.2 時,出現 ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib? 這樣的錯誤。

參考錯誤訊息,先依據 https://github.com/pyenv/pyenv/wiki/Common-build-problems 上的說明去檢查並安裝,但安裝了所需的套件之後,仍然有錯誤。後來再去找,才在 pyenv issue #950 找到原因,原來是 Ubuntu 14.04 的 OpenSSL 版本過舊,Python 3.7 需要 OpenSSL 1.0.2 以後的版本才行。

看了一下 Ubuntu 16.04 的 openssl 套件,版本是 1.0.2,於是就拿了 16.04 openssl 的 source package 回來,在 14.04 上編譯出 deb 來安裝就可以了。

# On Ubuntu 16.04
apt-get source openssl
# On Ubuntu 14.04
cd openssl-1.0.2g
dpkg-buildpackage
ls ../*.deb
# Output:
# ../libssl1.0.0_1.0.2g-1ubuntu4.14_amd64.deb      ../libssl-dev_1.0.2g-1ubuntu4.14_amd64.deb  ../openssl_1.0.2g-1ubuntu4.14_amd64.deb
# ../libssl1.0.0-dbg_1.0.2g-1ubuntu4.14_amd64.deb  ../libssl-doc_1.0.2g-1ubuntu4.14_all.deb
# Install them
ls ../*.deb | xargs sudo dpkg -i

再次安裝 Python 3.7.2

pyenv install -v 3.7.2
# 將當前目錄的 python 設定為 3.7.2,也就是以後切換到這個目錄時,自動使用 python 3.7.2
pyenv local 3.7.2
python --version
# Output:
# Python 3.7.2
# Use local python to make virtualenv
mkvirtualenv myproject --python=$(pyenv which python) -r requirements.txt

apt 使用 mirror

看到這篇文章:How to select the fastest apt mirror on Ubuntu Linux ,sources.list 裡可以改用 mirror://mirrors.ubuntu.com/mirrors.txt 這樣的 URL,apt 就會自動使用 mirror ,不過 apt 不一定會選到最快的 mirror。

在選最快的 mirror 時,可以用 netselect ,但 debian 才有包 netselect 。文章的留言有人提到一個組合命令可以用來取代 netselect ,我試了之後,發現不能用,稍稍做了調整 (改 sed 以及改用 awk 取 ping 結果):

for mirror in $(wget -qO - mirrors.ubuntu.com/mirrors.txt|sed 's/^http:\/\///;s/\/.*$//') ; do echo $(ping -c 1 $mirror|awk 'BEGIN {FS="[=]|[ ]"} NR==2 {print $11}') $mirror ; done|sort -n|grep "^[0-9]"|head -n 20

執行以後的結果:

2.49 mirror01.idc.hinet.net
3.47 debian.linux.org.tw
3.61 ubuntu.cs.nctu.edu.tw
5.45 ftp.tku.edu.tw
13.5 ftp.ntou.edu.tw
33.3 ubuntu.stu.edu.tw
40.8 ftp.ubuntu-tw.net
91.8 mirror.ncunwlab.tk
96.6 ftp.yzu.edu.tw
284 archive.ubuntu.com

看起來是中華電信最快,這時候再用 sed 去替換 sources.list 的來源即可。

sed 's/mirror:\/\/mirrors\.ubuntu\.com\/mirrors\.txt/http:\/\/mirror01\.idc\.hinet\.net\/ubuntu/' /etc/apt/sources.list | sudo tee /etc/apt/sources.list