Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Networking

Linux openssl command

Reference for the Linux openssl command: hashes and HMACs, base64, encrypting files, random bytes, keys, self-signed certificates and testing TLS with s_client.

openssl is a command-line front end to the OpenSSL cryptography library. It is a toolbox of subcommands: compute hashes, encrypt and decrypt files, generate keys and certificate requests, inspect certificates, and connect to a server to see exactly what its TLS setup looks like.

The first word after openssl selects the subcommand (dgst, enc, x509, req, s_client, ...), and each one has its own options. The tool is powerful and unforgiving, so for anything that protects real data prefer well-reviewed tools such as age, gpg or your platform's key management; use openssl mainly to inspect, test and generate certificates.

Terminal
openssl SUBCOMMAND [OPTIONS]

Try it

Hashes, HMACs and Base64 behave the same on every machine:

Bash Hashes, HMAC and Base64

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Certificates, encryption and a password hash:

Bash A self-signed certificate and file encryption

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Testing a live server (s_client) needs the network, so it is read-only:

CONSOLE
$ openssl s_client -connect example.org:443 -servername example.org </dev/null 2>/dev/null \
    | openssl x509 -noout -subject -dates
subject=CN = www.example.org
notBefore=Jan 13 00:00:00 2025 GMT
notAfter=Feb  1 23:59:59 2026 GMT

Syntax

Terminal
openssl dgst -sha256 [-hmac KEY] [FILE]
openssl enc -CIPHER [-d] [-pbkdf2] [-iter N] [-salt] [-pass pass:TEXT] -in FILE -out FILE
openssl rand [-hex|-base64] NUM
openssl genpkey -algorithm RSA|EC|ED25519 [-out FILE]
openssl req -x509 -newkey rsa:2048 -nodes -keyout KEY -out CERT -days N -subj "/CN=name"
openssl x509 -in CERT [-noout] [-text|-subject|-issuer|-dates|-fingerprint]
openssl s_client -connect HOST:PORT [-servername NAME]
openssl verify -CAfile CA.pem CERT
Subcommand Purpose
dgst Message digests (-sha256, -sha512, -md5) and HMACs.
enc Symmetric encryption and Base64 (openssl base64 is shorthand).
rand Cryptographically strong random bytes.
genpkey, genrsa Generate private keys.
req Create certificate signing requests (CSRs) and self-signed certificates.
x509 Display, convert and sign certificates.
s_client Connect to a TLS server and show the handshake and certificate chain.
verify Check a certificate against a CA file.
passwd Hash passwords (-6 SHA-512 crypt, -1 MD5 crypt).
version, list Show the version and available algorithms.

Returns: 0 on success, non-zero on errors; failed decryption prints bad decrypt and exits 1.

Usage notes

  • Always use -pbkdf2 with enc and a high -iter count (for example 600000 for real use). Older versions derive the key with a weak single-pass method, and newer ones warn about it. The same -iter must be used to decrypt.
  • Do not put real passwords on the command line. -pass pass:... is visible in the process list. Use -pass file:path, -pass env:VAR or -pass stdin.
  • Base64 decoding needs a final newline in some versions: printf 'aGVsbG8=' | openssl base64 -d can print nothing, while printf 'aGVsbG8=\n' works. Add -A for single-line input without wrapping.
  • dgst output format differs between versions: (stdin)= HASH in 1.1.1 and SHA2-256(stdin)= HASH in 3.x. Use awk '{print $NF}' to extract the hash.
  • Self-signed means untrusted. Browsers and curl reject them until you add the certificate to a trust store. Use a real CA (for example Let's Encrypt) for public sites.
  • Inspecting expiry in monitoring: openssl x509 -checkend 2592000 -noout -in cert.pem exits non-zero if the certificate expires within 30 days.
  • s_client and SNI. Use -servername or you may receive the wrong certificate on shared hosts. Add </dev/null so the command exits after the handshake.
  • Weak algorithms. Avoid MD5 and SHA-1 for anything security related, and DES, RC4 or ECB mode ciphers entirely.
  • Versions. OpenSSL 3 moved some legacy algorithms into a separate provider; commands that worked on 1.1.1 can fail with "unsupported" on 3.x.