Linux commonly uses base64 to encode and decode data. This method of encoding provides a reliable way for data to be transmitted and stored. The encoding process will convert binary data to ASCII characters, making it usable by a variety of services (such as OpenSSL) that require readable ASCII character transmission as opposed to binary. Afterwards, the data can be decoded back to binary data. In this tutorial, you will see how to use the
base64 command to decode and encode data on a Linux system.
In this tutorial you will learn:
- How to encode and decode data with base64 in terminal
- How to encode and decode base64 data from file
|Requirements, Conventions or Software Version Used
|Any Linux distro
|Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
base64 Encode and Decode Example Commands
base64 utility should be installed by default on all major Linux distributions. Let’s see some examples on how we can use the
base64 Linux command to encode and decode data from the command line.
- Using the
base64command with no further options will encode terminal input or the contents of a file to base64 format. Let’s look at a simple example of converting a text string via standard input from the
$ echo "Welcome to linuxconfig.org" | base64 V2VsY29tZSB0byBsaW51eGNvbmZpZy5vcmcK
The output (second line) is our base64 encoded data.
- Now we can take the encoded data we received above, and use the
--decodeoption to decode it back to its original format.
$ echo V2VsY29tZSB0byBsaW51eGNvbmZpZy5vcmcK | base64 -d Welcome to linuxconfig.org
- We can also encode an input file into the base64 format by using the following command syntax:
$ base64 input-file.txt VGhpcyBpcyBteSBpbnB1dCBmaWxlIQo=
Or to output the encoded data into its own file:
$ base64 input-file.txt > output-file.txt
- Next, let’s use the
--decodeoption to decode a file from base64 back to its original format:
$ base64 -d output-file.txt This is my input file!
The decoded contents are shown as output in our terminal.
Although at first glance it can seem like base64 is obscuring our text strings just like encryption would do, base64 is NOT encryption and should not be used as a substitute for protecting sensitive information. This data can be easily decoded and base64 is not meant to make the original data indecipherable.
In this tutorial, we saw how to use the
base64 command to encode and decode data on a Linux system. As some programs and system services require base64 encoding for their input, it is handy for a system administrator to be able to quickly encode a binary data to base64 to use as input. The same command can also be used to convert the encoded data back to human readable format.