我正在使用 ecryptfs 加密目录 /opt/directory。我想使用 systemd 和 init.d 创建服务,提示用户输入加密密码并挂载目录。我尝试在 systemd 服务调用的脚本中使用 ssh-askpass,但是当我的计算机启动时它不会提示输入密码,因此该目录没有挂载。
以下是我挂载目录的脚本:
#!/bin/bash
SIG='/root/.ecryptfs/encryption.sig'
ECRYPTFS_WRAPPED_PASSWORD="/root/.ecryptfs/directory-encryption.wrapped"
# test if authing and test if the mount point is not mounted
if ! mountpoint -q "/opt/directory"; then
PASSWORD=$(ssh-askpass)
# insert the wrapped password and mount the directory, expose_authtok types the password into the
# ecryptfs-insert-wrapped-passphrase-into-keyring command for us
SIG=$(head -n 1 "$SIG")
sudo -s -u "root" /bin/bash -c "printf "%s" "$PASSWORD" | ecryptfs-insert-wrapped-passphrase-into-keyring $ECRYPTFS_WRAPPED_PASSWORD && sudo mount -i -t ecryptfs /opt/directory /opt/directory -o ecryptfs_sig=$SIG,ecryptfs_fnek_sig=$SIG,ecryptfs_cipher=aes,ecryptfs_key_bytes=32,ecryptfs_unlink_sigs"
fi
该脚本在直接调用时工作正常,但在启动时使用 systemd 调用时,它不会提示我输入密码。以下是我的 systemd 文件:
[Unit]
Description=Directory Encryption Service
[Service]
ExecStart=/root/.ecryptfs/directory-encryption-script
StandardOutput=null
[Install]
WantedBy=multi-user.target
Alias=directory a.encryption.service