Today, we are going to see how to encrypt and decrypt files from command line in Linux using a free utility named GNU Privacy Guard (shortly GPG or GnuPG). It is a command line encryption and signing tool to secure files. We can easily encrypt the important and confidential files and documents using GPG and send/receive them over Internet. There are, of course, plethora of methods, applications, tools, and utilities are available to encrypt and decrypt files in Linux. But, I find this method is the easiest one.
Encrypt And Decrypt Files Using GnuPG From Commandline In Linux
GnuPG should have come pre-installed by default in most Linux distributions. If it is not, you can install it as shown below.
On Arch Linux and its derivatives:
$ sudo pacman -S gnupg
On Debian, Ubuntu, Linux Mint:
$ sudo apt-get install gnupg
On RHEL, CentOS:
$ sudo yum install gnupg
$ sudo dnf install gnupg
$ sudo zypper in gnupg
You can check the installed GPG version at any time using command:
$ gpg --version
gpg (GnuPG) 2.2.12 libgcrypt 1.8.4 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Home: /home/sk/.gnupg Supported algorithms: Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH, CAMELLIA128, CAMELLIA192, CAMELLIA256 Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224 Compression: Uncompressed, ZIP, ZLIB, BZIP2
Once it is installed, we can encrypt a file as shown below.
$ gpg -c Inter.txt
Enter your passphrase twice and click OK to encrypt the file. Replace Inter.txt file with your own filename.
Let us verify it using ls command:
dde-dock_1000 expandstate Inter.txt.gpg syndaemon.pid dde-launcher_1000 Inter.txt pulse-QiMoBZPcQcby wpa_ctrl_439-1
As you noticed the Inter.txt file has been encrypted and saved in the current directory as Inter.txt.gpg.
Once a file encrypted, You can't view the contents of thst file without knowing the passphrase.
If you try to open it using any editor, the contents of the GPG file should look like some random numbers and symbols.
\214^M^D^G^C^B@;\252\340\246p\263\260\336\322y^A^C AZ^Hb\215z̬\256D\243\202\365\362\326V\272^HX ^X\265\224h\262f\225ȹ< /\266\306 ^YMJ\200\2030f?^Q\244Q\3376\341.\274\361Ⱦ(i\345,\304\315%#\370/\232/j a^T\205`E\ 377\307^[v^\\230_ms\371\233\2442\365g^](1P\245Z;\355\347\320r\203^B\246^^@\213w\ 271\363D`\264^Y\374\256\314^QR|P
To decrypt the file, just run:
$ gpg -o Inter.txt -d Inter.txt.gpg
It will ask you to enter the passphrase. Just enter the correct passphrase, select Ok and hit Enter..
That's it, Now you can view the decrypted file's content using any editor of your choice.
As you can see, encrypting and decrypting files using GnuPG is a piece of cake. You don't need to be a command line ninja or an experienced Linux admin to do this.
To know more details about GnuPG, run:
$ gpg -h
Or, refer the man pages:
$ man gpg
Hope this helps.