Linux distros are considered as the best operating systems in terms of security. But if you still want to password protect files and folder in Linux, then there are a couple of easy ways.
So, why exactly would one need to set password to files and folders in Linux? Well, that is non of our business to know it. But we would surely like to help you securing you files Linux by setting password on it.
Best Ways To Password Protect Files In Linux
Method 1 To Password Protect Files In Linux
There is a package called gpg installed on your Linux using which you can set password to files. But first, you need to ensure that you have this package installed. To do that, hit the below command:
# whereis gpg
When you fire this command, it would give you the location of the package. And it implies that you have this package installed. So, you can go ahead. And if it isn’t installed, then you can try out the second method.
Suppose that you want to password protect a file name, dwarka.zip. All you need to hit is the below command:
# gpg –c dwarka.zip
When you fire this command, it would ask you to enter the password twice. When its done, you can find that a new encrypted file is created namely, dwarka.zip.gpg. And in order to read this encrypted file, you need to enter the password that you have entered while creating it.
So, if you are wondering how to read it, then hit the below command:
# gpg dwarka.zip.gpg
This would prompt you to enter the password and this will transform your file back to the original format.
Method 2 To Password Protect Files In Linux
This method can be used if you not have gpg package installed. You can make use of its zip utility. Let me demonstrate you using the example of same file. To password protect a folder, make sure first you zip it.
And to zip it, fire the below command:
# zip –e –r dwarka dwarka
This would first prompt you to enter a password. And once it is done, it will keep the original file and also create a password protected zip file. If you want, you can delete the original file. And to use the zipped file, you need to hit the below command:
# unzip dwarka.zip
These are the two best methods to password protect files in Linux.
GPG is likely the best strength of the various methods listed, for encryption.
7zip also can compress and password-protect files:
$ 7z a -p outfile infile_or_folder
To extract:
$ 7z x outfile.7z
However, with 7zip and with your methods in the HowTo, each of these methods don’t allow you to read/access/write the file while it is protected.
> So, if you are wondering how to read it, then hit the below command:
Vim can password protect a (text) file with Blowfish encryption (vim 7.3 and above), or with lesser strength encryption below 7.3; however when unencrypting the file it is stored in RAM unencrypted, and probably in a .swp file as well. Meaning we’re able to take out the steps of unencrypting and reencrypting the file, but its not being operated on securely. To create:
$ vim -x someNewFile.txt
Future openings of that file (without -x) will prompt for a password, even when using GVim.
Learning about FUSE encfs and TrueCrypt are also good moves.