Linux中使用eCryptfs加密主目錄
本文根據(jù)回憶記述在 Arch Linux 上為某一新用戶建立使用 eCryptfs 加密的 $HOME 目錄并使之在登錄時(shí)自動(dòng)解密掛載的過(guò)程。大量參考了 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權(quán)限,這里改成了700。
第一次掛載加密目錄:
1
|
# mount -t ecryptfs /home/user/.private /home/user
|
eCryptfs 會(huì)詢問(wèn)一些加密的選項(xiàng),其中 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
|
一定要記住密碼,雖然可能并不怎么會(huì)用到。
在mount命令的輸出中找到這次掛載使用的參數(shù),經(jīng)過(guò)一些變更,把類似于以下的設(shè)置添加到/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
|
注意:在登錄掛載時(shí),noexec、nosuid和nodev將會(huì)是默認(rèn)選項(xiàng)。這里加上exec選項(xiàng)來(lái)覆蓋掉noexec,這樣加密的 $HOME 中才支持執(zhí)行可執(zhí)行文件。
掛載時(shí)生成了/root/.ecryptfs目錄。我們先在里邊保存些文件:
1
2
3
4
|
# touch /root/.ecryptfs/auto-mount
# ecryptfs-wrap-passphrase /root/.ecryptfs/wrapped-passphrase
Passphrase to wrap: [輸入加密口令]
Wrapping passphrase: [輸入用戶的登錄口令]
|
現(xiàn)在,使用用戶的登錄口令可以從文件/root/.ecryptfs/wrapped-passphrase中得到 eCryptfs 的加密口令。即使加密口令很強(qiáng),如果登錄口令弱的話,文件信息還是會(huì)泄漏的。所以,得選個(gè)強(qiáng)的登錄口令,不然就不要玩登錄時(shí)自動(dòng)掛載加密 $HOME 了。
或者,你也可以玩點(diǎn)有趣的,把這個(gè)wrapped-passphrase文件放在 U 盤里,只留下一個(gè)指向 U 盤里的此文件的軟鏈接。然后配置好 U 盤自動(dòng)掛載,就做成了個(gè)簡(jiǎn)單的「U 盾」!
好了,現(xiàn)在卸載 $HOME:
1
|
# umount /home/user
|
自動(dòng)掛載
先把 eCryptfs 的那個(gè)在/root下的目錄弄回來(lái):
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
|
接下來(lái),創(chuàng)建一個(gè)掛載用的腳本,暫時(shí)叫它/home/profile.sh吧。它將被寫到用戶登錄時(shí)的自動(dòng)執(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
|
注意到這里加入了對(duì)~/.Xauthority文件的處理,不然從圖形界面登錄時(shí),執(zhí)行掛載命令后,會(huì)因授權(quán)文件不見了而失敗。之前把 $HOME 的權(quán)限設(shè)置成700也是為了這個(gè)。單純地允許寫~/.Xauthority不行,因?yàn)?xauth 需要?jiǎng)?chuàng)建臨時(shí)文件以防止此文件同時(shí)被多個(gè)進(jìn)程修改。
現(xiàn)在,我們需要在用戶登錄時(shí)自動(dòng) unwrap 之前創(chuàng)建的那個(gè)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
#...
|
好了,我們先手動(dòng)試試:
1
2
3
4
|
# su user
$ ecryptfs-insert-wrapped-passphrase-into-keyring /home/user/.ecryptfs/wrapped-passphrase
Passphrase: [輸入用戶密碼]
$ mount -i /home/user
|
如果正確掛載的話,接下來(lái)就可以開始建設(shè)你的新 $HOME 了,比如把你以前的各種文件復(fù)制過(guò)去,等等。注意不要在加密的目錄內(nèi)進(jìn)行 BT 下載哦。你可以建立個(gè)/home/.ecryptfs/user/public目錄然后軟鏈接到 $HOME 內(nèi)來(lái)使用。
我同時(shí)還修改了/etc/pam.d/slim,似乎這樣才能在使用 slim 登錄時(shí)也有效。
呃,還沒(méi)有結(jié)束呢。得把之前的/home/profile.sh文件弄進(jìn)來(lái)。這里演示時(shí)只是創(chuàng)建了一個(gè)新的.profile文件。如果你已經(jīng)有了此文件的話,一定不要將其加密,而要將其與此掛載腳本合并。它只能不加密,否則掛載后會(huì)出現(xiàn)兩個(gè).profile(一個(gè)加密了的,一個(gè)未加密、passthrough 來(lái)的),從而導(dǎo)致一些問(wèn)題。
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
|
好了,到此一切結(jié)束。
【編輯推薦】