為 yum repository 指定 proxy

要設定讓 yum 可以使用指定的 proxy 伺服器,可以在 /etc/yum.conf 的 [main] 區段裡增加 proxy 設定即可

[main]
proxy=http://<proxy_server>:3128

那如果是個別的 repository 要使用不同的 proxy 伺服器,可以怎麼做呢?
原本以為會很麻煩的,但意想不到簡單,因為有支援此設定。
就在 repository 的區段裡增加 proxy 設定就可以:

[repo]
name=baseos
baseurl=http://yumrepo.example.com/baseos
enabled=1
gpgcheck=0
proxy=http://<proxy_server>:3128

參考資料

測試指定的套件會影響哪些套件

若是想知道指定的套件會影響哪些套件,這時候可以怎麼做呢?

我第一個是想到可以用 rpm ,用 man rpm 可以查到 rpm 有以下參數

  • –provides
  • –requires
  • –whatprovides
  • –whatrequires

所以就試試看

rpm -q --whatrequires libweb

但結果卻不如預期,上網找到這篇 centos – Why does rpm –whatrequires fail to report dependencies? – Unix & Linux Stack Exchange

才知道正確用法是這樣,要先用 rpm -q --provides libwebp 去查到 libwebp 有提供什麼capability,然後再用 rpm -q --whatrequires <capability> 去找。

例如 libwebp

[root@workstation ~]# rpm -q --provides libwebp
libwebp = 1.0.0-5.el8
libwebp(x86-64) = 1.0.0-5.el8
libwebp.so.7()(64bit)
libwebpdecoder.so.3()(64bit)
libwebpdemux.so.2()(64bit)
libwebpmux.so.3()(64bit)
[root@workstation ~]# rpm -q --whatrequires "libwebp.so.7()(64bit)"
libwebp-1.0.0-5.el8.x86_64
gd-2.2.5-7.el8.x86_64
ImageMagick-libs-6.9.12.50-2.el8.x86_64

這樣就可以找到,但若是如此,就要寫腳本了。因為 libwebp 有提供多個 capability ,需要逐一去查才行。

有沒有更快的方法呢?

centos – Why does rpm –whatrequires fail to report dependencies? – Unix & Linux Stack Exchange 裡面有提到可以用 rpm -e --test ,直接模擬看看移除。

rpm -e --test libwebp 2>&1 | grep needed | awk '{print $6}' | sort | uniq

這樣就可以很快找出來了。

如何檢查RHEL有受到特定CVE影響?

因為好奇怎麼去查,就找到這篇:How to check if an RHEL system is vulnerable to a CVE ,文章裡面提到兩個方法。

第一個方法是用 rpm -q --changelog <套件> | grep <CVE_number>

舉個例子

rpm -q --changelog openssl | grep CVE-2021-3450

第二個方法是用 yum updateinfo info --cve <CVE_number>

舉個例子

yum updateinfo info --cve CVE-2021-3445

最後,想調查這台 RHEL 有受到哪些 Errata 影響,可以用

yum updateinfo info --summary
yum updateinfo info --list

若要帶入 CVE 資訊,可以用這兩個指令

yum updateinfo info --summary --with-cve
yum updateinfo info --list --with-cve

Red Hat 也有一篇 KB ,方法相似:https://access.redhat.com/solutions/3628301

不太一樣的地方是,指令是查看 rpm 的,所以是用

rpm -qp kernel-3.10.0-862.11.6.el7.x86_64.rpm --changelog | grep CVE-2017-12190

或者是用 yum list –cve <CVE_number>

yum list --cve CVE-2017-12190 | grep kernel.x86_64

[RHEL]grub設定密碼

以下指令可以避免讓使用者在開機時,可以調整 kernel 參數。若是在 kernel 參數填入 single 或 rescue ,使用者就有機會可以使用到較高的權限。

先用 grub2-setpassword 設定密碼,設定以後,/boot/grub2 資料夾裡會多出 user.cfg

grub2-setpassword 

再來重新產生 grub.cfg

grub2-mkconfig -o /boot/grub2/grub.cfg

這樣就完成了。

重開機看到開機選單以後,按下 e ,此時會要求輸入密碼,表示設定已經生效。

若要移除,刪除掉 user.cfg 即可。

rm /boot/grub2/user.cfg

若是要一進入開機選單就要輸入帳號跟密碼,那麼可以這樣設定

grub2-editenv - set grub_users="root"

這樣在開機時,就會問帳號跟密碼了。

要移除,可以用

grub2-editenv - set grub_users=

參考資料

RHEL8 系統憑證管理

RHEL8 管理系統 SSL 憑證,首先要安裝 ca-certificates 套件

yum install ca-certificates

這個套件主要是外部的憑證,會定期更新,若是在連線到外部有遇到憑證問題,通常更新這個套件就可以解決。

如果有需要安裝自簽憑證,可以複製到以下這兩個憑證路徑:

  • /etc/pki/ca-trust/source/anchors
  • /usr/share/pki/ca-trust-source/anchors

/etc/pki/ca-trust 的權限比較高。

複製完成,執行

update-ca-trust

要察看目前有放了哪些憑證,可以用 trust 指令

trust list

前面提到的複製、執行 update-ca-trust ,也可以用以下指令替代

trust anchor <path.to/certificate.crt>

參考資料

RHEL8系統加密原則(SSH)

在 Red Hat Enterprise Linux 8 以後導入了整合的加密原則管理,透過 update-crypto-policies 指令就可以很方便的去設定系統加密原則。

使用以下指令就可以看到使用哪個原則

update-crypto-policies --show

預設都是 DEFAULT

若需要設定為 FUTURE 可以用

update-crypto-policies --set FUTURE

Red Hat 有針對這部份撰寫一篇 KB: System-wide crypto policies in RHEL ,說明是如何運作的。在產品手冊裡也有提到這部份:Chapter 4. Using system-wide cryptographic policies。若有需要客製化,可以參考 Red Hat 的另外一篇 How to customize crypto policies in RHEL 8.2 來進行客製化。

接下來講一下 sshd 的部份,一般若要讓 sshd 使用指定的金鑰交換原則或是加密原則,會說要去修改 /etc/sysconfig/sshd ,把裡面的

# System-wide crypto policy:
# To opt-out, uncomment the following line
#CRYPTO_POLICY=

改為

# System-wide crypto policy:
# To opt-out, uncomment the following line
CRYPTO_POLICY=

然後再去修改 /etc/ssh/sshd_config 。

這個設定實際上會影響的地方是 sshd systemd service ,我們可以用 systemctl cat sshd 來看看

# /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.target
Wants=sshd-keygen.target

[Service]
Type=notify
EnvironmentFile=-/etc/crypto-policies/back-ends/opensshserver.config
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS $CRYPTO_POLICY
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

從這邊可以看到有兩行 EnvironmentFile,第一行 EnvironmentFile 去含括 /etc/crypto-policies/back-ends/opensshserver.config ,這就是系統的 ssh 加密原則設定。第二行 EnvironmentFile 含括的檔案,就是前面提到的 /etc/sysconfig/sshd 。

那在 ExecStart 這行可以看到啟動 sshd 時,有放置 $CRYPTO_POLICY ,表示會使用這個環境變數的內容。也因此,若在 /etc/sysconfig/sshd 裡移除 CRYPTO_POLICY 的註解,在啟動 sshd 時,就不會帶入參數,而使用 /etc/ssh/sshd_config 裡的設定。

在查到這邊時,我才想起之前好像有找過,但之前沒有記錄,沒記錄就容易忘記,所以今天特別記下來。

RHEL8/CentOS8 grubby

上週幫客戶處理問題時,對 RHEL8/CentOS8 的 grub 又多了解了一些,記錄一下。

Ubuntu 的選單是放在 /boot/grub/grub.cfg ,所以看這個檔案,就大略知道選單有哪些選項,修改可以直接改這個檔案。RHEL8 跟 CentOS8 使用 grubby 來管理開機時的 grub 選單,所以可以用 grubby 指令來做管理。

常用的指令可以參考 12 practical grubby command examples (cheat sheet)

我這次主要使用到的指令有

  • 列出開機選單項目的資訊
  • 修改該選單項目的 kernel options
  • 取得目前預設開機項目
  • 設定預設開機項目

列出開機選單項目的資訊

列出選單項目,可以用下面指令

grubby --info=ALL | grep -E "^kernel|^index"

取得 index 跟 kernel ,就可以修改指定項目的 kernel options 了。

修改指定選單項目的 kernel options

修改的指令如下

# 修改指定的 kernel 的 kernel options,路徑可參考上面指令
grubby --update-kernel=<kernel路徑> --args="ipv6.disable=1"
# 修改目前的
grubby --update-kernel=/boot/vmlinuz-$(uname -r) --args="ipv6.disable=1"

若要移除 kernel options,要用 –remove-args

# 移除指定的 kernel 的 kernel options
grubby --update-kernel=<kernel路徑> --remove-args="ipv6.disable=1"
# 移除目前 kernel 的 kernel options
grubby --update-kernel=/boot/vmlinuz-$(uname -r) --remove-args="ipv6.disable=1"

這裡的修改,會影響到 /boot/loader/entries 目錄下的檔案。

取得目前預設開機項目

grubby --default-index
grubby --default-title

設定預設開機項目

用以下指令設定

grubby --set-default="/boot/vmlinuz-4.18.0-193.1.2.el8_2.x86_64"
grubby --set-default-index=2

這裡設置以後,會影響到 /boot/grub/grubenv 的檔案內容。

可惜 ubuntu 沒有 grubby,感覺這在管理開機選單上還蠻方便的。

如何建立YUM mirror server

在企業裡,因為不會讓所有伺服器都對外,一般會在內部架設一台 YUM mirror server ,定期去 mirror 外部的 RHEL 套件庫。

坦白說,我不知道用哪個名字比較好,知道的名字有

  • Local mirror
  • YUM repository server
  • YUM mirror server

總之,用途都是一致的。

作法

首先這台要作為 mirror 的伺服器必須要對外,接著在伺服器裡,以 subscription-manager 註冊到 RHEL 服務去。

安裝做 mirror 的套件,這邊選用 apache,也可以用 nginx,路徑跟設定調整一下就可以。

yum install yum-utils createrepo httpd

使用 subscription-manager 查看可以下載的 repository

subscription-manager repos --list

接著對需要做 mirror 的 repository 去做 mirror

mkdir -p /var/www/html/{baseos,appstream}
reposync -p /var/www/html/baseos \
        --download-metadata \
        --repo=rhel-8-for-x86_64-baseos-rpms \
        --delete \
        --downloadcomps

reposync -p /var/www/html/appstream \
        --download-metadata \
        --repo=rhel-8-for-x86_64-appstream-rpms \
        --delete \
        --downloadcomps

這邊可以寫成一個腳本,然後設定 cron,定期去執行 mirror。

一般會再多寫一個 repo 檔案,放在 /var/www/html ,後續到其他主機時,可以直接下載這個 repo 檔案,放到 /etc/yum.repos.d 裡面,就可以直接 yum update 。

[baseos]
name=baseos
baseurl=http://<ip>/rhel-8-for-x86_64-baseos-rpms/
enabled=1
gpgcheck=0

[appstream]
name=appstream
baseurl=http://<ip>/rhel-8-for-x86_64-appstream-rpms/
enabled=1
gpgcheck=0

最後啟用 httpd,就大功告成了。

systemctl enable --now httpd

限制

使用這個方法,基本上一台主機就是對應一個版本,例如 7.9, 8, 8.5, 8.6 這樣,沒辦法有多個版本,若需要有多個版本,建議使用 Foreman 或是 Satellite 會比較方便。

參考資料

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 來做複製跟改動。

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 登入的設定,這邊我就沒試過了。

參考資料