如何使用 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