Some easy choices for encrypting files on Linux (2024)

There are numerous commands for encrypting files on Linux. When you want to limit access to file contents, you can use file permissions but file encryption makes limiting access much more effective. This post compares some of the commands for encrypting files and provides an easy script for trying them out.

Encryption means, of course, that a file that you can look at with Linux commands and tools is altered in ways that make it unusable and unreadable unless you reverse the encryption process. Encryption does not generally reduce the size of files unless compression is used as well. In fact, the encryption process might make some files larger. Some commands compress by default; others do not.

Things to keep in mind when preparing to encrypt a file include how you intend to use it (e.g., secure backups, transfer to another system), how you manage keys so that you can decrypt the file when needed, and whether the original file remains on the original system or is encrypted “in place” – you are left only with the encrypted version of the file.

NOTE: Some encryption commands can be used with public/private keys or with passwords provided at the time of the encryption. This post only shows commands using passwords/passphrases.

gpg

One of the standard and most well know tools for encrypting files on Linux is gpg. It can provide both digital encryption and signing services although, in this post, we’ll just look at encrypting files with a passphrase. Unlike some of the other tools, gpg does apply some file compression before encrypting the file contents.

If you type a command like this one, the contents of the file will be encrypted using a symmetric key. In other words, the same word or phrase will be used both to encrypt and to decrypt the file. Public/private keys can be used with the -e option.

$ gpg -c BigFile

You will be prompted twice to enter a password and the original file will remain intact as shown in this example:

$ ls -l BigFile*-rw-rw-r-- 1 shs shs 107740386 Jul 10 13:21 BigFile-rw-rw-r-- 1 shs shs 32359452 Jul 11 11:00 BigFile.gpg

Notice the nice reduction in the resultant file size and that original file is still intact.

The gpg command only works with one file at a time.

zip

The zip command is generally used to compress files and to collect files into archives for easy storage and transport. The command does, however, also support encryption. You just have the add the –encrypt option.

$ zip --encrypt BigFile.zip BigFile

Like gpg, zip does both encryption and compression, so the resultant file size should be considerably smaller than the original.

$ ls -l BigFile*-rw-rw-r-- 1 shs shs 107740386 Jul 10 13:21 BigFile-rw-rw-r-- 1 shs shs 27587355 Jul 10 14:40 BigFile.zip

Since zip is a tool intended to create archives, you can add multiple files to your encrypted bundle by adding them on the command line.

$ zip --encrypt loops.zip loop1 loop2Enter password:Verify password: adding: loop1 (deflated 4%) adding: loop2 (deflated 10%)$ ls -l loops*-rw-rw-r-- 1 shs shs 468 Jul 11 09:04 loops.zip

7z

The 7z command works like zip, but touts a surprisingly impressive compression ratio. Like zip, it can include a number of files in one encrypted archive. To invoke encryption, include the encryption password on the command line following the -p option.

$ 7z a BigFile.7z BigFile -phard2gue$$
$ ls -l BigFile*-rw-rw-r-- 1 shs shs 107740386 Jul 10 13:21 BigFile-rw-rw-r-- 1 shs shs 27674 Jul 11 12:37 BigFile.7z

ccrypt

Another tool for encrypting and decrypting files, ccrypt (based on the Rijndael block cipher) is believed to provide very strong security and, like the other commands described, is easily run on the command line.

Notice that ccrypt removes the original file (encrypts the file in place), doesn’t significantly change the file size and doesn’t alter the file’s date/time to reflect the time the encryption was performed.

$ ccrypt -e BigFile$ ls -l BigFile*-rw-rw-r-- 1 shs shs 107740418 Jul 9 10:09 BigFile.cpt

The ccrypt command can encrypt multiple files with one command, but encrypts them separately.

mcrypt

The mcrypt command prompts for a password twice, leaves the original file intact and changes file permissions to that the encrypted file only provides read and write access permissions to the file owner. It offers a lot of choices with respect to encryption algorithms and also provides options for compressing the files prior to encryption (see-z and -p options. It can work with multiple files, but encrypts them separately.

Using the –list option, mycrypt will list the available encryption algorithms.

$ mcrypt --listcast-128 (16): cbc cfb ctr ecb ncfb nofb ofbgost (32): cbc cfb ctr ecb ncfb nofb ofbrijndael-128 (32): cbc cfb ctr ecb ncfb nofb ofbtwofish (32): cbc cfb ctr ecb ncfb nofb ofbarcfour (256): streamcast-256 (32): cbc cfb ctr ecb ncfb nofb ofbloki97 (32): cbc cfb ctr ecb ncfb nofb ofbrijndael-192 (32): cbc cfb ctr ecb ncfb nofb ofbsaferplus (32): cbc cfb ctr ecb ncfb nofb ofbwake (32): streamblowfish-compat (56): cbc cfb ctr ecb ncfb nofb ofbdes (8): cbc cfb ctr ecb ncfb nofb ofbrijndael-256 (32): cbc cfb ctr ecb ncfb nofb ofbserpent (32): cbc cfb ctr ecb ncfb nofb ofbxtea (16): cbc cfb ctr ecb ncfb nofb ofbblowfish (56): cbc cfb ctr ecb ncfb nofb ofbenigma (13): streamrc2 (128): cbc cfb ctr ecb ncfb nofb ofbtripledes (24): cbc cfb ctr ecb ncfb nofb ofb

The mcrypt command appears to use rijndael-128 as its default encryption algorithm. However, you can verify which has been used by using the file command on the compressed file:

$ file BigFile.bz2.ncBigFile.bz2.nc: mcrypt 2.5 encrypted data, algorithm: rijndael-128, keysize: 32 bytes, mode: cbc,

A script for trying encryption commands

This script should be called “try” and makes it easy for you to experiment with the tools covered in this post. For example, if you type “try 7z target” (where “target” is the name of the file you want to encrypt), the script will run the command to encrypt your file with 7z and show you the results. If you try to use a command that is not installed on your system, it will explain that it isn’t yet set up to use that command.

#!/bin/bash# verify that 2 arguments have been providedif [ $# != 2 ]; then echo "OOPS: command and file name required" exitfi# make sure the requested encryption command in availablewhich $1 > /dev/nullif [ $? != 0 ]; then echo "$1 not available" exit 1fi# make sure the file existsif [ ! -f $2 ]; then echo "No such file: $2" exit 2ficase $1 in gpg) gpg -c $2 ;; ccrypt) ccrypt -e $2 ;; 7z) echo -n "please provide password: " read password 7z a $2.7z $2 -p$password ;; zip) zip --encrypt $2.zip $2 ;; mcrypt) mcrypt -p $2 ;; *) echo "Sorry, this script is not yet set up for $1" exit;;esac# show the file(s)ls -l $2*

The try script is not set up to encrypt more than one target file at a time as it uses $2 (the second argument provided to the script) to specify the target file and exits if more than one file is provided as an argument. Feel free to modify or add to the script to suit your needs.

Related content

  • how-to7 ways to compare text files on Linux The diff, comm, cmp, and colordiff commands are among the many ways to compare text files on a Linux system.By Sandra Henry StockerAug 26, 20246 minsLinux
  • how-toHow to work with substrings on Linux The awk, cut, grep, expr, sed and xargs commands provide many useful options for manipulating text.By Sandra Henry StockerAug 20, 20247 minsLinux
  • opinionKey rules for Linux sysadmins After 30 years managing Linux servers, I've found these practices helped me stay focused and effective.By Sandra Henry-StockerAug 09, 20246 minsLinuxCareers
  • how-toHow to find and fix spelling errors on Linux There are a number of spelling assistants you can use on Linux systems, including aspell, enchant-2, look, and grep. Some commands will need to be installed on your system.By Sandra Henry StockerAug 01, 20246 minsLinux
  • PODCASTS
  • VIDEOS
  • RESOURCES
  • EVENTS

NEWSLETTERS

Newsletter Promo Module Test

Description for newsletter promo module.

Some easy choices for encrypting files on Linux (2024)

FAQs

What is the best way to encrypt Linux? ›

In Linux environment Linux Unified Key Setup (LUKS) is used for encrypting entire block devices, hard drive, SSDs and even removable storage drives. Full hard drive encryption is possible only during the installation of the Linux operating system. In this case it will encrypt both the swap space and system partitions.

How do you encrypt a file in Linux? ›

Right-click on the file you wish to encrypt and select Encrypt from the context menu. This action will open the Seahorse application. In the Seahorse window, you will see a list of your encryption keys. If you haven't created any keys yet, you can generate a new one by clicking on the + icon and following the prompts.

What is the best way to encrypt a file? ›

How to encrypt a file
  1. Right-click (or press and hold) a file or folder and select Properties.
  2. Select the Advanced button and select the Encrypt contents to secure data check box.
  3. Select OK to close the Advanced Attributes window, select Apply, and then select OK.

What kind of encryption does Linux use? ›

Most Linux distributions mainly use a one-way encryption algorithm, which is called Data Encryption Standard (DES) for encrypting passwords. These encrypted passwords are then stored typically in /etc/passwd or in /etc/shadow but this is less commonly.

What is the most encrypted Linux? ›

Which version of Linux is considered most secure
  • Tails. Tails is a Debian-based free and secure operating system that aims to preserve your privacy and anonymity. ...
  • Qubes. One of the top security-focused Linux distros, recommended by various privacy experts. ...
  • Kodachi. ...
  • Whonix. ...
  • Kali Linux. ...
  • Parrot OS. ...
  • Subgraph OS. ...
  • Discreete Linux.
Apr 22, 2024

How do I make Linux more secure? ›

So, if you'd like to learn some of the most important security tips for Linux system hardening, this guide is for you.
  1. Enable Strong Authentication. ...
  2. Create an SSH Key Pair. ...
  3. Keep the System Up to Date. ...
  4. Remove Unnecessary Software. ...
  5. Disable Root Login. ...
  6. Check and Close Open Ports. ...
  7. Enable a Firewall. ...
  8. Security Audits Are Important.
Sep 27, 2023

What is the best encryption method for securing files? ›

AES is widely considered invulnerable to all attacks except for brute force. Regardless, many internet security experts believe AES will eventually be regarded as the go-to standard for encrypting data in the private sector. Triple DES.

What is the safest encryption method? ›

There are two types of crypto: symmetric key and public key (asymmetric). Symmetric key cryptography comprises a single key being used for encryption / decryption, and it is useful for encrypting large amounts of data. In this category, AES is usually the most widely used and secure algorithm.

What is the best way to encrypt data? ›

The two most widely used methods for data encryption are public key, also known as asymmetric encryption, and private key, or symmetric encryption.

What Linux is more secure? ›

Best Secure Linux Comparison Table
DistributionUser FriendlinessTop 3 Security Applications
Parrot Security OSHighMetasploit Framework, Nmap, Aircrack-ng
Kali LinuxModerateNmap, Metasploit Framework, Wireshark
Qubes OSLowXen, FirewallVM, Whonix
BlackArch LinuxLowMetasploit, Wireshark, SQLmap
2 more rows
Jul 15, 2024

How to encrypt files in Linux script? ›

You can save a key to a file by running ./encrypt.sh -g > encryption. key . To encrypt a file use the -e option and specify the {input-file} (file to encrypt) and {output-file} (encrypted file). You can use a -k Key, -p Password, or leave the parameter blank in order to be prompted for a password.

Does encryption slow down Linux? ›

It will add a little CPU load when reading or writing data (every block you read or write is encrypted or decrypted on the fly), but it is negligible (you can check for example this Phoronix article with benchmarks).

Does Linux have an equivalent to BitLocker? ›

BitLocker is is a full-disk encryption software developed by Microsoft for the Windows operating systems, Microsoft did not develop a version of BitLocker for the Linux operating system, so Linux users can only use third-party BitLocker solutions to encrypt the drives, and Hasleo BitLocker Anywhere For Linux is such a ...

Is LUKS more secure than BitLocker? ›

Bitlocker is generally considered to be more secure than dm-crypt/luks, but it can be more difficult to set up and use.

What is the most secure way to encrypt data? ›

Best Encryption Algorithms
  1. AES. The Advanced Encryption Standard (AES) is the trusted standard algorithm used by the United States government, as well as other organizations. ...
  2. Triple DES. ...
  3. RSA. ...
  4. Blowfish. ...
  5. Twofish. ...
  6. Rivest-Shamir-Adleman (RSA).
Jul 16, 2024

Top Articles
Truth in Lending Act (TILA) Violations & Right of Rescission
Understanding Active Directory Trust Relationships: A Deep Dive
Bank Of America Financial Center Irvington Photos
Spectrum Gdvr-2007
Pinellas County Jail Mugshots 2023
Missed Connections Inland Empire
Georgia Vehicle Registration Fees Calculator
DL1678 (DAL1678) Delta Historial y rastreo de vuelos - FlightAware
Palace Pizza Joplin
Jcpenney At Home Associate Kiosk
2021 Lexus IS for sale - Richardson, TX - craigslist
Methodist Laborworkx
Craftology East Peoria Il
Highland Park, Los Angeles, Neighborhood Guide
Cta Bus Tracker 77
Nhl Tankathon Mock Draft
Gentle Dental Northpointe
Aps Day Spa Evesham
Walgreens Alma School And Dynamite
EASYfelt Plafondeiland
Fsga Golf
[PDF] NAVY RESERVE PERSONNEL MANUAL - Free Download PDF
11 Ways to Sell a Car on Craigslist - wikiHow
Fleet Farm Brainerd Mn Hours
Amerisourcebergen Thoughtspot 2023
27 Modern Dining Room Ideas You'll Want to Try ASAP
Package Store Open Near Me Open Now
Prévisions météo Paris à 15 jours - 1er site météo pour l'île-de-France
The value of R in SI units is _____?
Eaccess Kankakee
First Light Tomorrow Morning
Sports Clips Flowood Ms
Where Can I Cash A Huntington National Bank Check
Ducky Mcshweeney's Reviews
Unlock The Secrets Of "Skip The Game" Greensboro North Carolina
Ursula Creed Datasheet
Conroe Isd Sign In
Davis Fire Friday live updates: Community meeting set for 7 p.m. with Lombardo
The Listings Project New York
Sun Tracker Pontoon Wiring Diagram
The best specialist spirits store | Spirituosengalerie Stuttgart
Citymd West 146Th Urgent Care - Nyc Photos
Myapps Tesla Ultipro Sign In
Steam Input Per Game Setting
300 Fort Monroe Industrial Parkway Monroeville Oh
Arnold Swansinger Family
Epower Raley's
Morgan State University Receives $20.9 Million NIH/NIMHD Grant to Expand Groundbreaking Research on Urban Health Disparities
Cool Math Games Bucketball
Elizabethtown Mesothelioma Legal Question
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 5625

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.