天天看點

linux 中将檔案設定密碼,Stratis 加密入門 | Linux 中國

linux 中将檔案設定密碼,Stratis 加密入門 | Linux 中國

導讀:Stratis 在其 官方網站 上被描述為“易于使用的 Linux 本地存儲管理”。

本文字數:4284,閱讀時長大約: 5分鐘

https://linux.cn/article-13311-1.html

作者:Briansmith

譯者:Xingyu.Wang

Stratis 在其 官方網站 上被描述為“易于使用的 Linux 本地存儲管理”。請看這個 短視訊,快速示範基礎知識。該視訊是在 Red Hat Enterprise Linux 8 系統上錄制的。視訊中顯示的概念也适用于 Fedora 中的 Stratis。

Stratis 2.1 版本引入了對加密的支援。繼續閱讀以了解如何在 Stratis 中開始加密。

先決條件

加密需要 Stratis 2.1 或更高版本。這篇文章中的例子使用的是 Fedora 33 的預釋出版本。Stratis 2.1 将用在 Fedora 33 的最終版本中。

你還需要至少一個可用的塊裝置來建立一個加密池。下面的例子是在 KVM 虛拟機上完成的,虛拟磁盤驅動器為 5GB(/dev/vdb)。

在核心密鑰環中建立一個密鑰

Linux 核心密鑰環(keyring)用于存儲加密密鑰。關于核心密鑰環的更多資訊,請參考 keyrings 手冊頁(man keyrings)。

使用 stratis key set 指令在核心鑰匙圈中設定密鑰。你必須指定從哪裡讀取密鑰。要從标準輸入中讀取密鑰,使用 -capture-key 選項。要從檔案中讀取密鑰,使用 -keyfile-path 選項。最後一個參數是一個密鑰描述。它将稍後你建立加密的 Stratis 池時使用。

例如,要建立一個描述為 pool1key 的密鑰,并從标準輸入中讀取密鑰,可以輸入:

# stratis key set --capture-key pool1key

Enter desired key data followed by the return key:

該指令提示我們輸入密鑰資料/密碼,然後密鑰就建立在核心密鑰環中了。

要驗證密鑰是否已被建立,運作 stratis key list:

# stratis key list

Key Description

pool1key

這将驗證是否建立了 pool1key。請注意,這些密鑰不是持久的。如果主機重新開機,在通路加密的 Stratis 池之前,需要再次提供密鑰(此過程将在後面介紹)。

如果你有多個加密池,它們可以有一個單獨的密鑰,也可以共享同一個密鑰。

也可以使用以下 keyctl 指令檢視密鑰:

# keyctl get_persistent @s

318044983

# keyctl show

Session Keyring

701701270 --alswrv 0 0 keyring: _ses

649111286 --alswrv 0 65534 \_ keyring: _uid.0

318044983 ---lswrv 0 65534 \_ keyring: _persistent.0

1051260141 --alswrv 0 0 \_ user: stratis-1-key-pool1key

建立加密的 Stratis 池

現在已經為 Stratis 建立了一個密鑰,下一步是建立加密的 Stratis 池。加密池隻能在建立池時進行。目前不可能對現有的池進行加密。

使用 stratis pool create 指令建立一個池。添加 -key-desc 和你在上一步提供的密鑰描述(pool1key)。這将向 Stratis 發出信号,池應該使用提供的密鑰進行加密。下面的例子是在 /dev/vdb 上建立 Stratis 池,并将其命名為 pool1。確定在你的系統中指定一個空的/可用的裝置。

# stratis pool create --key-desc pool1key pool1 /dev/vdb

你可以使用 stratis pool list 指令驗證該池是否已經建立:

# stratis pool list

Name Total Physical Properties

pool1 4.98 GiB / 37.63 MiB / 4.95 GiB ~Ca, Cr

在上面顯示的示例輸出中,~Ca 表示禁用了緩存(~ 否定了該屬性)。Cr 表示啟用了加密。請注意,緩存和加密是互相排斥的。這兩個功能不能同時啟用。

接下來,建立一個檔案系統。下面的例子示範了建立一個名為 filesystem1 的檔案系統,将其挂載在 /filesystem1 挂載點上,并在新檔案系統中建立一個測試檔案:

# stratis filesystem create pool1 filesystem1

# mkdir /filesystem1

# mount /stratis/pool1/filesystem1 /filesystem1

# cd /filesystem1

# echo "this is a test file" > testfile

重新開機後通路加密池

當重新啟動時,你會發現 Stratis 不再顯示你的加密池或它的塊裝置:

# stratis pool list

Name Total Physical Properties# stratis blockdev list

Pool Name Device Node Physical Size Tier

要通路加密池,首先要用之前使用的相同的密鑰描述和密鑰資料/密碼重新建立密鑰:

# stratis key set --capture-key pool1key

Enter desired key data followed by the return key:

接下來,運作 stratis pool unlock 指令,并驗證現在可以看到池和它的塊裝置:

# stratis pool unlock

# stratis pool list

Name Total Physical Properties

pool1 4.98 GiB / 583.65 MiB / 4.41 GiB ~Ca, Cr

# stratis blockdev list

Pool Name Device Node Physical Size Tier

pool1 /dev/dm-2 4.98 GiB Data

接下來,挂載檔案系統并驗證是否可以通路之前建立的測試檔案:

# mount /stratis/pool1/filesystem1 /filesystem1/

# cat /filesystem1/testfile

this is a test file

使用 systemd 單元檔案在啟動時自動解鎖 Stratis 池

可以在啟動時自動解鎖 Stratis 池,無需手動幹預。但是,必須有一個包含密鑰的檔案。在某些環境下,将密鑰存儲在檔案中可能會有安全問題。

下圖所示的 systemd 單元檔案提供了一個簡單的方法來在啟動時解鎖 Stratis 池并挂載檔案系統。歡迎提供更好的/替代方法的回報。你可以在文章末尾的評論區提供建議。

首先用下面的指令建立你的密鑰檔案。確定用之前輸入的相同的密鑰資料/密碼來代替passphrase。

# echo -n passphrase > /root/pool1key

確定該檔案隻能由 root 讀取:

# chmod 400 /root/pool1key

# chown root:root /root/pool1key

在 /etc/systemd/system/stratis-filesystem1.service 建立包含以下内容的 systemd 單元檔案:

[Unit]

Description = stratis mount pool1 filesystem1 file system

After = stratisd.service

[Service]

ExecStartPre=sleep 2

ExecStartPre=stratis key set --keyfile-path /root/pool1key pool1key

ExecStartPre=stratis pool unlock

ExecStartPre=sleep 3

ExecStart=mount /stratis/pool1/filesystem1 /filesystem1

RemainAfterExit=yes

[Install]

WantedBy = multi-user.target

接下來,啟用服務,使其在啟動時運作:

# systemctl enable stratis-filesystem1.service

現在重新啟動并驗證 Stratis 池是否已自動解鎖,其檔案系統是否已挂載。

結語

在今天的環境中,加密是很多人群組織的必修課。本篇文章示範了如何在 Stratis 2.1 中啟用加密功能。