跳转至内容

systemd-creds

来自 ArchWiki

systemd-creds 是由 systemd 提供的一个实用程序和服务,旨在安全地存储和检索由 systemd 单元使用的凭据。其目标是简化对各种服务和应用程序所需的敏感信息(如用户名、密码、API 密钥和其他身份验证数据)的管理。

更多信息请参阅 https://systemd.io/CREDENTIALS/systemd-creds(1)

凭据加密

凭据可以选择性地进行加密和认证,可以使用源自本地 TPM2 芯片的密钥,或者存储在 /var/ 中的密钥,或者在两者都可用时同时使用(默认设置)。要检查您的系统是使用 TPM2 加密还是仅使用存储在 /var/ 中的密钥,请运行

$ systemd-analyze has-tpm2
yes
+firmware
+driver
+system
+subsystem

要加密存储在 plaintext.txt 中的密钥,请运行

# systemd-creds --name=foobar encrypt plaintext.txt ciphertext.cred
注意 对于敏感凭据,请考虑为 plaintext.txt 使用 tmpfs

加密的凭据内部携带了其预定名称,因此无法被误用。如果您省略 --name,则文件名将被用作凭据名称。

您可以通过以下命令检查加密是否成功:

# shred -u plaintext.txt
# systemd-run --pipe --wait --property=LoadCredentialEncrypted=foobar:$(pwd)/ciphertext.cred systemd-creds cat foobar

如果不使用上述的 --name,正确的命令应该是

# systemd-run --pipe --wait --property=LoadCredentialEncrypted=ciphertext.cred:$(pwd)/ciphertext.cred systemd-creds cat ciphertext.cred 

或者,您可以使用 systemd-ask-password 来提供密钥,而无需先将其写入磁盘。-p 开关允许以一种可以直接包含在服务文件中的格式输出凭据

# systemd-ask-password -n | systemd-creds encrypt --name=mysql-password -p - -
🔐 Password: ***************         
SetCredentialEncrypted=mysql-password: \
       k6iUCUh0RJCQyvL8k8q1UyAAAAABAAAADAAAABAAAAD42X4T5h86ynMHG5MAAAAAgAAAA \
       AAAAAALACMA8AAAACAAAAAAngAgws4cwpkrOvpxVf4/GSyOYCoQ4dmPs3Vcfg3D0eH/Ov \
       cAEC/U9JTyeHgktYBDl8U5jOq/NqFX3IPc24EX7aeTevsL6OKwlTDcF04W1M6eodqngRo \
       1JfRqHWnSfVHHYVXgMjIkWT4AcC0OoqAzQobK5e5G1zjTPygiG/ewGttY9sueKCL/INUu \
       n1e6/QB6D9T0x9Sl18CeBtoASepCAE4ACAALAAAAEgAgY9RHOefBR+mMgV/Frr+UzQX1S \
       IsLFItIbfB6Ep76It0AEAAgKH2Tduip205Dw0GVkME+yvOUNYVhnSHYqWHQoxelKrdj1E \
       c558FH6YyBX8Wuv5TNBfVIiwsUi0ht8HoSnvoi3QAAAAA3WzuXTYG2OFX9wFo740b3YjE \
       ZtiCWakoAOxzX+D/vIFyB3INROxpfXUCV7LY4eRITI9PJZiE6lGn4kjZBnbUf47yo1WDG \
       Wg==

编辑 服务文件以包含相关的凭据

用户凭据加密

systemd-creds 实用程序也可以由普通用户使用。

$ echo -n hunter2 | systemd-creds --user encrypt - ciphertext.cred
$ systemd-creds --user decrypt ciphertext.cred
hunter2

systemd-creds(1) 中所述,用户的 scope 和 /etc/machine-id 被用于加密凭据。例如,root 用户可以解密它,但必须匹配相应的 user scope

$ systemd-creds --uid anotheruser decrypt ciphertext.cred
Failed to decrypt: io.systemd.InteractiveAuthenticationRequired
# systemd-creds decrypt ciphertext.cred
Encrypted file is scoped to a user, but no user selected.
# systemd-creds --uid user decrypt ciphertext.cred
hunter2

© . This site is unofficial and not affiliated with Arch Linux.

Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.