This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:
Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.
See Instead: This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.
GPG, or GNU Privacy Guard, is a public key cryptography implementation. This allows for the secure transmission of information between parties and can be used to verify that the origin of a message is genuine.
In this guide, we will discuss how GPG works and how to implement it. We will be using an Ubuntu 12.04 VPS for this demonstration, but the tools are available on any modern Linux distribution.
A problem that many users face is how to communicate securely and validate the identity of the party they are talking to. Many schemes that attempt to answer this question require, at least at some point, the transfer of a password or other identifying credentials, over an insecure medium.
To get around this issue, GPG relies on a security concept known as public key encryption. The idea is that you can split the encrypting and decrypting stages of the transmission into two separate pieces. That way, you can freely distribute the encrypting portion, as long as you secure the decrypting portion.
This would allow for a one-way message transfer that can be created and encrypted by anyone, but only be decrypted by the designated user (the one with the private decrypting key). If both of the parties create public/private key pairs and give each other their public encrypting keys, they can both encrypt messages to each other.
So in this scenario, each party has their own private key and the other user’s public key.
Another benefit of this system is that the sender of a message can “sign” the message with their private key. The public key that the receiver has can be used to verify that the signature is actually being sent by the indicated user.
This can prevent a third-party from “spoofing” the identity of someone. It also helps to ensure that the message was transmitted in-full, without damage or file corruption.
GPG should be installed by default on Ubuntu 12.04. If it is not, you can install it with:
sudo apt-get install gnupg
To begin using GPG to encrypt your communications, you need to create a key pair. You can do this by issuing the following command:
gpg --gen-key
This will take you through a few questions that will configure your keys.
Please select what kind of key you want: (1) RSA and RSA (default)
What keysize do you want? 4096
Key is valid for? 0
Is this correct? y
Real name: your real name here
Email address: your_email@address.com
Comment: Optional comment that will be visible in your signature
Change (N)ame, ©omment, (E)mail or (O)kay/(Q)uit? O
Enter passphrase: Enter a secure passphrase here (upper & lower case, digits, symbols)
At this point, it will need to generate the keys using entropy. This is basically a term to describe the amount of unpredictability that exists in a system. GPG uses this entropy to generate a random set of keys.
It is best to open a new terminal and ssh into the VPS while this runs. Install some software, do some work, and just use the machine as much as possible to let it generate the needed entropy.
This process may take a long time, depending on how active you can make your system. There is an article here about how to generate additional entropy with haveged, which may be of use.
You need to have a way of invalidating your key pair in case there is a security breach, or in case you lose your secret key. There is an easy way of doing this with the GPG software.
This should be done as soon as you make the key pair, not when you need it. This revocation key must be generated ahead of time and kept in a secure, separate location in case your computer is compromised or inoperable. Type:
<pre> gpg --gen-revoke <span class=“highlight”>your_email@address.com</span> </pre>
You will be asked for the reason that it is being revoked. You can choose any of the available options, but since this is being done ahead of time, you won’t have the specifics.
You will then be offered to supply a comment and finally, to confirm the selections.
Afterwards, a revocation certificate will be generated to the screen. Copy and paste this to a secure location, or print it for later use:
Revocation certificate created.
Please move it to a medium which you can hide away; if Mallory gets
access to this certificate he can use it to make your key unusable.
It is smart to print this certificate and store it away, just in case
your media become unreadable. But have some caution: The print system of
your machine might store the data and make it available to others!
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: A revocation certificate should follow
iQIfBCABAgAJBQJSTxNSAh0AAAoJEIKHahUxGx+E15EP/1BL2pCTqSG9IYbz4CMN
bCW9HgeNpb24BK9u6fAuyH8aieLVD7It80LnSg/+PgG9t4KlzUky5sOoo54Qc3rD
H+JClu4oaRpq25vWd7+Vb2oOwwd/27Y1KRt6TODwK61z20XkGPU2NJ/ATPn9yIR9
4B10QxqqQSpQeB7rr2+Ahsyl5jefswwXmduDziZlZqf+g4lv8lZlJ8C3+GKv06fB
FJwE6XO4Y69LNAeL+tzSE9y5lARKVMfqor/wS7lNBdFzo3BE0w68HN6iD+nDbo8r
xCdQ9E2ui9os/5yf9Y3Uzky1GTLmBhTqPnl8AOyHHLTqqOT47arpwRXXDeNd4B7C
DiE0p1yevG6uZGfhVAkisNfi4VrprTx73NGwyahCc3gO/5e2GnKokCde/NhOknci
Wl4oSL/7a3Wx8h/XKeNvkiurInuZugFnZVKbW5kvIbHDWJOanEQnLJp3Q2tvebrr
BBHyiVeQiEwOpFRvBuZW3znifoGrIc7KMmuEUPvA243xFcRTO3G1D1X9B3TTSlc/
o8jOlv6y2pcdBfp4aUkFtunE4GfXmIfCF5Vn3TkCyBV/Y2aW/fpA3Y+nUy5hPhSt
tprTYmxyjzSvaIw5tjsgylMZ48+qp/Awe34UWL9AWk3DvmydAerAxLdiK/80KJp0
88qdrRRgEuw3qfBJbNZ7oM/o
=isbs
-----END PGP PUBLIC KEY BLOCK-----
GPG would be pretty useless if you could not accept other public keys from people you wished to communicate with.
You can import someone’s public key in a variety of ways. If you’ve obtained a public key from someone in a text file, GPG can import it with the following command:
<pre> gpg --import <span class=“highlight”>name_of_pub_key_file</span> </pre>
There is also the possibility that the person you are wishing to communicate with has uploaded their key to a public key server. These key servers are used to house people’s public keys from all over the world.
A popular key server that syncs its information with a variety of other servers is the MIT public key server. You can search for people by their name or email address by going here in your web browser:
https://pgp.mit.edu/
You can also search the key server from within GPG by typing the following:
<pre> gpg --keyserver pgp.mit.edu --search-keys <span class=“highlight”>search_parameters</span> </pre>
While you can freely distribute your generated public key file and people can use this to contact you in an encrypted way, there is still an issue of trust in the initial public key transmission.
How do you know that the person giving you the public key is who they say they are? In some cases, this may be simple. You may be sitting right next to the person with your laptops both open and exchanging keys. This should be a pretty secure way of identifying that you are receiving the correct, legitimate key.
But there are many other circumstances where such personal contact is not possible. You may not know the other party personally, or you may be separated by physical distance. If you never want to communicate over insecure channels, verification of the public key could be problematic.
Luckily, instead of verifying the entire public keys of both parties, you can simply compare the “fingerprint” derived from these keys. This will give you a reasonable assurance that you both are using the same public key information.
You can get the fingerprint of a public key by typing:
<pre> gpg --fingerprint <span class=“highlight”>your_email@address.com</span> </pre>
<pre> pub 4096R/311B1F84 2013-10-04 Key fingerprint = <span class=“highlight”>CB9E C70F 2421 AF06 7D72 F980 8287 6A15 311B 1F84</span> uid Test User <test.user@address.com> sub 4096R/8822A56A 2013-10-04 </pre>
This will produce a much more manageable string of numbers to compare. You can compare this string with the person themselves, or someone else who has access to that person.
Signing a key tells your software that you trust the key that you have been provided with and that you have verified that it is associated with the person in question.
To sign a key that you’ve imported, simply type:
<pre> gpg --sign-key <span class=“highlight”>email@example.com</span> </pre>
After you’ve signed the key, it means you verify that you trust the person is who he/she claims to be. This can help other people decide whether to trust that person too. If someone trusts you, and they see that you’ve signed this person’s key, they may be more likely to trust their identity too.
You should allow the person whose key you are signing the advantages of your trusted relationship by sending them back the signed key. You can do this by typing:
<pre> gpg --export --armor <span class=“highlight”>email@example.com</span> </pre>
You’ll have to type in your passphrase again. Afterwards, their public key, signed by you, will be spit out on the screen. Send them this, so that they can benefit from gaining your “stamp of approval” when interacting with others.
When they receive this new, signed key, they can import it, adding on the signing information you’ve generated, into their GPG database. They can do this by typing:
<pre> gpg --import <span class=“highlight”>file_name</span> </pre>
There is not really anything malicious that can happen if unknown people have your public key.
Because of this, it may be beneficial to make your public key easily available. People can then easily find your information to send you secure messages, from the very first communication.
You can send anyone your public key by requesting it from the GPG system:
<pre> gpg --armor --export <span class=“highlight”>your_email@address.com</span> </pre>
<pre> -----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1.4.11 (GNU/Linux)
mQINBFJPCuABEACiog/sInjg0O2SqgmG1T8n9FroSTdN74uGsRMHHAOuAmGLsTse 9oxeLQpN+r75Ko39RVE88dRcW710fPY0+fjSXBKhpN+raRMUKJp4AX9BJd00YA/4 EpD+8cDK4DuLlLdn1x0q41VUsznXrnMpQedRmAL9f9bL6pbLTJhaKeorTokTvdn6 5VT3pb2o+jr6NETaUxd99ZG/osPar9tNThVLIIzG1nDabcTFbMB+w7wOJuhXyTLQ JBU9xmavTM71PfV6Pkh4j1pfWImXc1D8dS+jcvKeXInBfm2XZsfOCesk12YnK3Nc u1Xe1lxzSt7Cegum4S/YuxmYoh462oGZ7FA4Cr2lvAPVpO9zmgQ8JITXiqYg2wB3 . . . </pre>
You can then copy and paste this or send this in an appropriate medium.
If you want to publish your key to a key server, you can do it manually through the forms available on most of the server sites.
Another option is to do this through the GPG interface.
Look up your key ID by typing:
<pre> gpg --list-keys <span class=“highlight”>your_email@address.com</span> </pre>
The highlighted portion is your key ID. It is a short way to reference the key to the internal software.
<pre> pub 4096R/<span class=“highlight”>311B1F84</span> 2013-10-04 uid Test User <test.user@address.com> sub 4096R/8822A56A 2013-10-04 </pre>
To upload your key to a certain key server, you can then use this syntax:
<pre> gpg --send-keys --keyserver pgp.mit.edu <span class=“highlight”>key_id</span> </pre>
You can easily encrypt and decrypt messages after you have configured your keys with the other party.
You can encrypt messages using the “–encrypt” flag for GPG. The basic syntax would be:
<pre> gpg --encrypt --sign --armor -r <span class=“highlight”>person@email.com</span> <span class=“highlight”>name_of_file</span> </pre>
The parameters basically encrypt the email, sign it with your private key to guarantee that it is coming from you, and generates the message in a text format instead of raw bytes.
You should also include a second “-r” recipient with your own email address if you want to be able to read the message ever. This is because the message will be encrypted with each person’s public key, and will only be able to be decrypted with the associated private key.
So if it was only encrypted with the other party’s public key, you would not be able to view the message again, unless you somehow obtained their private key. Adding yourself as a second recipient encrypts the message two separate times, one for each recipient.
When you receive a message, simply call GPG on the message file:
<pre> gpg <span class=“highlight”>file_name</span> </pre>
The software will prompt you as necessary.
If you have the message as a raw text stream, you can copy and paste it after you just typing gpg
without any arguments. You can press “CTRL-D” to signify the end of the message and GPG will decrypt it for you.
There are a number of procedures that you may need to use on a regular basis to manage your key database.
To list your available GPG keys that you have from other people, you can issue this command:
gpg --list-keys
Your key information can become outdated if you are relying on information pulled from public key servers. You do not want to be relying on revoked keys, because that would mean you are trusting potentially compromised keys.
You can update the key information by issuing:
gpg --refresh-keys
This will fetch new information from the key servers.
You can pull information from a specific key server by using:
<pre> gpg --keyserver <span class=“highlight”>key_server</span> --refresh-keys </pre>
Using GPG correctly can help you secure your communications with different people. This is extremely helpful, especially when dealing with sensitive information, but also when dealing with regular, every day messaging.
Because of the way that certain encrypted communications can be flagged by monitoring programs, it is recommended to use encryption for everything, not just “secret” data. That will make it more difficult for people to know when you are sending important data or just sending a friendly hello.
GPG encryption is only useful when both parties use good security practices and are vigilant about their other security practices. Educate those who you communicate regularly with about the importance of these practices if you want to have the possibility of cryptographically secure communication.
<div class=“author”>By Justin Ellingwood</div>
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
This is a great article and super helpful. I have a few nit-picks that have come from years of using GPG and making a bajillion mistakes along the way :) These are things I didn’t understand when I started and wish I had known earlier.
From the article:
Another benefit of this system is that the sender of a message can “sign” the message with their private key. The public key that the receiver has can be used to verify that the signature is actually being sent by the indicated user.
Shouldn’t that be “The sender’s public key” instead?
I get a public key from a keyserver. Now I can encrypt messages with this key. But how do I get a private key to decrypt it? It doesn’t say where I get a private key…
Amazing, concise and to the point. Thank you!
Thanks for the elaborate post. Now everyone will understand that it is far to complicated to waste time on and no one will use it.
In PGP we used to sign recipient’s public key using
pgp --sign-key "xyzzzd" --signer "######" --sig-type exportable --passphrase "######@##"
, how do I sign in gpg. What are the replacements for--signer
and--sig-type
and--exporter
to achieve same behaviorWe are required to sign the encrypted file and we found this tutorial very useful. However we are facing issues while running the following 2 commands:
gpg --sign-key email@example.com gpg --armor --export your_email@address.com
In the first command what value should be used for email@example.com? Is it the recipient’s?
Thank you, very interesting and helpful article. my key-ID is 4A79F18D. ;)
Wow stuff !!!
I receive the encrypted message file from some other system, and the decryption should be done in our system through some scheduled program (without manual intervention). So I prepared a unix script for all the purposes. Now here the problem is, to pass the ‘passphrase’ at runtime without manual intervention. Please suggest.
Thank you in advance. Kanchan
Thank you! I’m running gpgtools on a Mac but I think you are right. I see now that the option to add my key to the recipients was checked off so I unchecked it and could not decrypt the message. Thanks again!