Tech Tip: Encrypt Files with GPG


Encrypting files from the command line is simple with gpg. You can use it to encrypt and decrypt files with a password.

The command gpg is part of GnuPG. GnuPG stands for GNU Privacy Guard and is GNU's tool for secure communication and data storage. It can be used to encrypt data and to create digital signatures. It also includes an advanced key management facility. GnuPG works on Linux and UNIX like operating systems as well as for Windows and Mac OS X.

To encrypt a single file, use the -c command line option with gpg. For example, to encrypt the file, use the command:

$ gpg -c
Enter passphrase: YOUR-PASSWORD
Repeat passphrase: YOUR-PASSWORD

This will create the file Note that the original file is not deleted, so once you feel safe encrypting and decrypting files, you probably want to delete your unencrypted versions of the files. Also note that depending on your system's configuration, gpg may ask for passphrases in pop-up windows rather than at the command line.

The -c option tells gpg to encrypt with a symmetric cipher. Caution: don't forget your passphrase (password), there is no way to recover data with out the passphrase.

To decrypt the file, use the command:

$ gpg
gpg: CAST5 encrypted data
Enter passphrase: YOUR-PASSWORD

If you want to write the output to a different file, use the -o command line option:

$ gpg –o 

If you'd rather have a "text" file, rather than a binary file, use the -a option to gpg:

$ gpg -c -a
Enter passphrase: YOUR-PASSWORD
Repeat passphrase: YOUR-PASSWORD

This will create the file rather than



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

GnuPG v2

Maxim .'s picture

You could also use gpg2 with the same command line options.

open GPG files with vim

Anonymous's picture

Vim plugin for transparent editing of gpg encrypted files:

I use this a lot for my passwords. It's also a perfect way of sharing password-files with colleagues: you add various "recipients" to the gpg-file - it will then be encrypted with the keys of all recipients. You can then put the file in a SVN repository you use together with your colleagues.

Adding recipients

Anonymous's picture

At my last place of employment, this is exactly what we did with sensitive information. It allowed us to put it where we could easily access it if it were lost on our computer, but did not allow prying eyes to see something they shouldn't.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState