使用ecryptfs 加密文件

安装

eCryptfs - Enterprise Cryptographic Filesystem
企业级文件加密系统

eCryptfs最大的特点是用户对加密文件的操作是无缝的,就像操作正常文件一样。

$ sudo apt-get install ecryptfs-utils

使用

新建一个文件夹:ecryptfs_test
使用这个文件夹来保存要加密的文件,保证该文件夹是空的,若该文件夹在加密之前就存在一些文件,则这些文件是不会被加密的。

命令:

sudo mount -t ecryptfs real_path ecryptfs_mounted_path

mount是挂载命令。-t是指定文件类型。ecryptfs就是我们使用的加密文件类型。
real_path是真实数据存放的地方(也就是一开始创建的ecryptfs_test文件夹),ecryptfs_mounted_path是指你要把文件挂在到哪。
IBM推荐:ecryptfs_mounted_path和真实目录real_path一致,这样非授权用户不能通过元原路径访问加密文件。

执行下面命令:

sudo mount -t ecryptfs ecryptfs_test/ ecryptfs_test/

第一次执行结果如下:
passphrase:
(这是要你输入密码,自己编一个。一定要记住。另外密码是不会有任何显示的,输完回车就行)
select cipher:
(选择加密方式,不选默认是[aes]。最好记住自己的选择)
select key bytes:
(选择加密位数,不选默认是[16]。最好记住自己的选择)
enable plaintext passthrough(y/n) [n]:
(是否允许使用明文,默认是 n)
enable filename encryption (y/n) [n]:
(是否把文件名也进行加密,默认是 n。如果选择y,那么在没有解密 的情况下是无法看见文件夹内部的文件的)
如果设置的密码是第一次使用,它会提示你密码被标识为[799d2f922c0f1f26] 。当然,你的密码标识肯定不会是这个。并且告诉你挂载错误,因为/root/.ecryptfs/sig-cache.txt中没有相关记录。开始让你选择:
Would you like to proceed with the mount (yes/no)? :
(你是否希望继续进行挂载。我们输入yes,来完成加密)
Would you like to append sig [799d2f922c0f1f26] to
[/root/.ecryptfs/sig-cache.txt]
in order to avoid this warning in the future (yes/no)?:
(你是否把密码标识加到/root/.ecryptfs/sig-cache.txt中,免得下次再报警。我们输入yes)

现在可以正常操作文件夹了,这时向文件夹添加文件都是可正常访问的。

关闭解密:

sudo umount -t ecryptfs ecryptfs_test
(如果使用的是sudo mount -t ecryptfs real_path ecryptfs_mounted_path

取消挂载后ecryptfs_test文件夹可以进入,但是里面的文件进行了加密处理,查看文件显示乱码:
在这里插入图片描述

想要正常查看文件,需要ecryptfs 重新挂载

sudo mount -t ecryptfs ecryptfs_test/ ecryptfs_test/

(格式还是不变:sudo mount -t ecryptfs real_path ecryptfs_mounted_path)
passphrase:
select cipher:
select key bytes:
enable plaintext passthrough(y/n) [n]:
enable filename encryption (y/n) [n]:
上面这几相当初怎么设置的就怎么填,之后文件夹就解密了。
不用了,还是用命令解除解密状态
sudo umount -t ecryptfs ecryptfs_test
这里说一点,mount命令重启就失效了。如果你要关机了,不必使用sudo umount -t ecryptfs ecryptfs_test
命令了。因为再次开机,解密是失效的。必须重新运行命令sudo mount -t ecryptfs ecryptfs_test ecryptfs_test

参考文章:https://baike.baidu.com/item/eCryptfs/881665?fr=aladdin#2

发布了61 篇原创文章 · 获赞 44 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/HUXINY/article/details/89279219