An Easy Way To Encrypt And Decrypt Files From Commandline In Linux
Today, we are going to see how to encrypt and decrypt files from command line in Linux using a free utility called GPG. GNU Privacy Guard, shortly GPG or GnuPG, 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 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 From Commandline In Linux
GnuPG should be 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, Fedora, 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 (GnuPG) 2.1.17 libgcrypt 1.7.5 Copyright (C) 2016 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 shoud 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 a file and decrypting it back 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:
Or, refer the man pages:
That’s it. Hope this helps. if you find this guide useful, please share it on your social networks and support us.