Linux中使用eCryptfs加密主目錄
本文根據回憶記述在 Arch Linux 上為某一新用戶建立使用 eCryptfs 加密的 $HOME 目錄并使之在登錄時自動解密掛載的過程。大量參考了 Unknown and partly hidden 的 eCryptfs and $HOME 一文。
依賴的軟件包:ecryptfs-utils。
加密目錄
1
2
3
4
5
6
|
# mkdir -p /home/.ecryptfs/user/private
# chmod 755 /home/.ecryptfs
# chmod -R 700 /home/.ecryptfs/user
# chown -R user:user /home/.ecryptfs/user
# ln -s /home/.ecryptfs/user/private /home/user/.private
# chmod 700 /home/user
|
注意:最后一步原文使用的是500權限,這里改成了700。
第一次掛載加密目錄:
1
|
# mount -t ecryptfs /home/user/.private /home/user
|
eCryptfs 會詢問一些加密的選項,其中 Cypher(加密方法)和 Key byte 可自行選擇:
1
2
3
4
5
6
7
|
Key type: passphrase
Passphrase: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Cypher: twofish
Key byte: 32
Plaintext passtrough: yes
Filename encryption: yes
Add signature to cache: yes
|
一定要記住密碼,雖然可能并不怎么會用到。
在mount命令的輸出中找到這次掛載使用的參數,經過一些變更,把類似于以下的設置添加到/etc/fstab中:
1
|
/home/user/.private /home/user ecryptfs rw,user,noauto,exec,ecryptfs_sig=XYZ,ecryptfs_cipher=twofish,ecryptfs_key_bytes=32,ecryptfs_passthrough,ecryptfs_fnek_sig=XYZ,ecryptfs_unlink_sigs 0 0
|
注意:在登錄掛載時,noexec、nosuid和nodev將會是默認選項。這里加上exec選項來覆蓋掉noexec,這樣加密的 $HOME 中才支持執(zhí)行可執(zhí)行文件。
掛載時生成了/root/.ecryptfs目錄。我們先在里邊保存些文件:
1
2
3
4
|
# touch /root/.ecryptfs/auto-mount
# ecryptfs-wrap-passphrase /root/.ecryptfs/wrapped-passphrase
Passphrase to wrap: [輸入加密口令]
Wrapping passphrase: [輸入用戶的登錄口令]
|
現在,使用用戶的登錄口令可以從文件/root/.ecryptfs/wrapped-passphrase中得到 eCryptfs 的加密口令。即使加密口令很強,如果登錄口令弱的話,文件信息還是會泄漏的。所以,得選個強的登錄口令,不然就不要玩登錄時自動掛載加密 $HOME 了。
或者,你也可以玩點有趣的,把這個wrapped-passphrase文件放在 U 盤里,只留下一個指向 U 盤里的此文件的軟鏈接。然后配置好 U 盤自動掛載,就做成了個簡單的「U 盾」!
好了,現在卸載 $HOME:
1
|
# umount /home/user
|
自動掛載
先把 eCryptfs 的那個在/root下的目錄弄回來:
1
2
3
|
# mv /root/.ecryptfs /home/.ecryptfs/user
# chown -R user:user /home/.ecryptfs/user/.ecryptfs
# ln -s /home/.ecryptfs/user/.ecryptfs /home/user/.ecryptfs
|
接下來,創(chuàng)建一個掛載用的腳本,暫時叫它/home/profile.sh吧。它將被寫到用戶登錄時的自動執(zhí)行腳本中,如~/.profile,或者~/.zprofile,如果你用 Zsh 的話。
1
2
3
4
5
6
7
8
9
|
if [ -r "$HOME/.ecryptfs/auto-mount" ]; then
grep -qs "$HOME ecryptfs" /proc/mounts
if [ $? -ne 0 ]; then
mv $HOME/.Xauthority /tmp
mount -i "$HOME"
cd "$HOME"
mv /tmp/.Xauthority $HOME
fi
fi
|
注意到這里加入了對~/.Xauthority文件的處理,不然從圖形界面登錄時,執(zhí)行掛載命令后,會因授權文件不見了而失敗。之前把 $HOME 的權限設置成700也是為了這個。單純地允許寫~/.Xauthority不行,因為 xauth 需要創(chuàng)建臨時文件以防止此文件同時被多個進程修改。
現在,我們需要在用戶登錄時自動 unwrap 之前創(chuàng)建的那個wrapped-passphrase文件。在/etc/pam.d/login中添加幾行(注意順序):
1
2
3
4
5
6
7
8
|
#%PAM-1.0
#...
auth required pam_unix.so nullok
auth required pam_ecryptfs.so unwrap
#...
password required pam_ecryptfs.so
#password required pam_unix.so sha512 shadow use_authtok
#...
|
好了,我們先手動試試:
1
2
3
4
|
# su user
$ ecryptfs-insert-wrapped-passphrase-into-keyring /home/user/.ecryptfs/wrapped-passphrase
Passphrase: [輸入用戶密碼]
$ mount -i /home/user
|
如果正確掛載的話,接下來就可以開始建設你的新 $HOME 了,比如把你以前的各種文件復制過去,等等。注意不要在加密的目錄內進行 BT 下載哦。你可以建立個/home/.ecryptfs/user/public目錄然后軟鏈接到 $HOME 內來使用。
我同時還修改了/etc/pam.d/slim,似乎這樣才能在使用 slim 登錄時也有效。
呃,還沒有結束呢。得把之前的/home/profile.sh文件弄進來。這里演示時只是創(chuàng)建了一個新的.profile文件。如果你已經有了此文件的話,一定不要將其加密,而要將其與此掛載腳本合并。它只能不加密,否則掛載后會出現兩個.profile(一個加密了的,一個未加密、passthrough 來的),從而導致一些問題。
1
2
3
4
5
|
# umount /home/user
# chmod 600 /home/profile.sh
# chown user:user /home/profile.sh
# mv /home/profile.sh /home/.ecryptfs/user/private/.profile
# ln -s /home/.ecryptfs/user/private/.profile /home/user/.profile
|
好了,到此一切結束。
【編輯推薦】