Encrypt File in Ubuntu with GPG
Tags: gpg, encryption, ubuntu, hostnextra
Published: 2021-06-02 04:08:20

In this article, we'll explain how to encrypt file in Ubuntu with GPG.

<a href="https://gnupg.org/" target="_blank" rel="noopener">“GPG” or “GNU Privacy Guard”</a> allows you to securely encrypt files and data with a strong encryption algorithm. GPG is an excellent method to ensure secure communication between two parties. It allows sensitive information to be easily shared across an insecure network.
<h4>Prerequisites</h4>
<ul>
 	<li>An Ubuntu installed desktop, <a href="https://www.hostnextra.com/dedicated-server.html" target="_blank" rel="noopener">dedicated server</a> or <a href="https://www.hostnextra.com/vps-hosting.html" target="_blank" rel="noopener">KVM VPS</a>.</li>
 	<li>A root user access or normal user with administrative privileges to install GPG.</li>
</ul>
<p><a href="https://www.hostnextra.com/kb/encrypt-file-in-ubuntu-with-gpg/">Encrypt File in Ubuntu with GPG</a></p>
<h4>Install GPG in Ubuntu</h4>
We can install GPG using following command:
<blockquote># apt install gpg</blockquote>
<h4>Encrypt file using GPG</h4>
Following command will encrypt <em>test.txt</em>.
<blockquote># gpg -c test.txt</blockquote>
Once you run the above command, you will ask to enter a passphrase that will be used for encrypting and decrypting the file.

<img class="alignnone size-full wp-image-2260" src="https://www.hostnextra.com/kb/wp-content/uploads/2021/06/encrypt-file-in-linux.png" alt="Encrypt file in Linux" width="657" height="495" />

After you enter passphrase, file gets encrypted and one file gets create in the following format:
<blockquote>test.txt.gpg</blockquote>
To decrypt the file, run a command in the following format:
<blockquote># gpg test.txt.gpg</blockquote>
Use the passphrase you initially used to encrypt the file. Once decrypted, you will get back a “test.txt” file.
<h4>Generate a GPG key pair</h4>
You can use a pair of “public” and “private” keys to encrypt and decrypt files. To generate a GPG key pair, you can use the command below:
<blockquote># gpg --generate-key</blockquote>
Follow the instructions and enter a username and password when prompted. After generating the key pair, GPG will show the generated public key in terminal output.

To export public key to a file, run the following command:
<blockquote># gpg --armor --export --output "public_key"</blockquote>
Replace “<em>public_key</em>” with your own public key generated in the step above. Your generated private keys will be available in “<em>$HOME/.gnupg/</em>” directory.

Encrypt a file using public key of someone else, use the following command:
<blockquote># gpg --encrypt --recipient-file public_key.file test.txt</blockquote>
Note: Replace public_key.file with public key of the person you want to send the file.

Decrypt a file that was encrypted using your public key, use the following command:
<blockquote># gpg --decrypt --output test.txt test.txt.gpg</blockquote>
GPG will automatically detect your private keys during decryption as long as they are stored in the “$HOME/.gnupg/” directory.

Learn more about GPG <a href="https://guides.library.illinois.edu/data_encryption/gpgcheatsheet" target="_blank" rel="noopener">cheatsheet</a>.

In this article, we have seen how to encrypt file in Ubuntu with GPG.