Session and utilities
The openssl command
The Swiss army knife of cryptography: it generates random numbers and keys, computes fingerprints (hashes), encrypts and decrypts files and inspects TLS certificates. It is the reference tool on servers. It has many subcommands, so start with the one you need.
Most useful options
rand -hex NGenerates N random bytes in hexadecimal, useful for passwords or test secretsdgst -sha256Computes the SHA-256 hash of a fileenc -aes-256-cbc -pbkdf2Encrypts or decrypts (-d) with AES-256 and a key derived from the passwordx509 -noout -datesShows the validity dates of a certificateCopy-ready examples
$ openssl rand -hex 1632 random hexadecimal characters. That is 16 bytes, or 128 bits.Example output
3f9a1c7e5b2d4a80c6e1f7b9d0a3c521
$ openssl dgst -sha256 informe.pdfSHA-256 fingerprint of the file. Compare it with the published one to verify the download.Example output
SHA2-256(informe.pdf)= 6e1d0b7c4a9f2e38d5b1c0a7f4e9d2b68c3a1f0e5d7b9c2a4e6f8d0b1c3a5e7f
$ openssl enc -aes-256-cbc -pbkdf2 -salt -in datos.txt -out datos.encencrypts datos.txt with a password it asks for. To decrypt, add -d and the same algorithm.$ openssl x509 -in cert.pem -noout -datesstart and end validity dates of a certificate. Useful to know when it expires.Example output
notBefore=Jan 1 00:00:00 2026 GMT notAfter=Jan 1 00:00:00 2027 GMT
You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| Can't open "noexiste.pem" for reading, No such file or directory | The file given with -in does not exist or is misspelled. | Check the name with ls and the path you run the command from. |
| bad decrypt | The password is not the encryption one, or the algorithm and the -pbkdf2 option do not match the ones used to encrypt. It can also be a damaged file. | Use exactly the same -aes-256-cbc and -pbkdf2 when decrypting, with the same password. |
Typical mistakes
- For keys and secrets, use openssl rand or your system's generator. bash's $RANDOM is not cryptographic.
Differences from similar commands
- gpg: encrypts with public and private keys, designed to exchange files between people. openssl enc uses a shared password.
- sha256sum: only computes fingerprints, with no certificates or other algorithms. openssl dgst does the same and more.
- base64: encodes data to carry it as text. It encrypts nothing: anyone can decode it.
Security
Do not pass passwords on the command line with -pass pass:... or -k: they end up in the history and other users can see them in ps. Use -pass file:path or the interactive prompt. And for user passwords, do not use a plain hash such as sha256: use -pbkdf2 or a tool built for passwords.
FAQ
How do I see the certificate of a website?
With openssl s_client -connect host:443 -servername host </dev/null. The connection closes after the TLS handshake and the certificate appears in the output.
Why does openssl say bad decrypt?
It is almost always a wrong password. The message does not distinguish a wrong password from a damaged file.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course