如何使用 gpg 保护 Linux 中的文件

在共享的 Linux 机器或环境下工作时,有时需要隐藏或保护一些包含敏感信息的文件,幸运的是,有一个名为 GnuPG 的工具,大多数 Linux 发行版都已预装,我们可以方便地使用这个工具,这篇文章介绍了我们如何在一个非常基本的场景中轻松快速地使用它。

Encrypt and decrypt a file with passphrase

# encrypt and enter a strong passphrase
$ gpg -c filename

# decrypt without entering passphrase as it's cached
$ gpg -d filename.gpg

Optional optimal options

# disable passphrase cache so that decryption requires passphrase
$ gpg -c --no-symkey-cache filename
$ gpg -d --no-symkey-cache filename
# another option is add a line no-symkey-cache to ~/.gnupg/gpg.conf
# so that there is no need to provide command line option every time:
# ~$ cat .gnupg/gpg.conf
# no-symkey-cache

# delete original unencrypted file and
# remove .gpg extension of the encrypted one
$ rm filename
$ mv filename.gpg filename

# change to hidden file
$ mv filename .filename

参考资料

linux security