Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux openssl command

Linux openssl command

Linux Command Reference Linux Command Reference


OpenSSL is a powerful open-source cryptography toolkit that provides various encryption algorithms, certificate management features, and SSL/TLS protocol implementations. It is the de facto standard tool for handling encryption tasks on Linux.

The main functions of OpenSSL include:


Basic Syntax

The basic syntax of the openssl command is:

openssl command [command_options] [command_args]

Where:


Common subcommands and examples

1. Generate an RSA key pair

Generate a 2048-bit RSA private key:

openssl genrsa -out private.key 2048

Extract the public key from the private key:

openssl rsa -in private.key -pubout -out public.key

Parameter Description:

2. Create a self-signed certificate

Generate a CSR (Certificate Signing Request):

openssl req -new -key private.key -out cert.csr

Generate a self-signed certificate (valid for 365 days):

openssl req -x509 -new -key private.key -days 365 -out cert.crt

Parameter Description:

3. File encryption and decryption

Encrypt a file using AES-256-CBC:

openssl enc -aes-256-cbc -salt -in plaintext.txt -out encrypted.enc

Decrypt a file:

openssl enc -d -aes-256-cbc -in encrypted.enc -out decrypted.txt

Parameter Description:

4. Compute file hashes

Compute a SHA-256 hash:

openssl dgst -sha256 filename.txt

Compute an MD5 hash:

openssl dgst -md5 filename.txt

5. Test an SSL connection

Test a remote server's SSL certificate:

openssl s_client -connect example.com:443 -showcerts

Parameter Description:


Advanced usage

1. Create a PKCS#12 certificate

Package the certificate and private key into a PKCS#12 file:

openssl pkcs12 -export -in cert.crt -inkey private.key -out cert.p12

2. View certificate information

View detailed certificate information:

openssl x509 -in cert.crt -text -noout

3. Verify the certificate chain

Verify the integrity of the certificate chain:

openssl verify -CAfile ca.crt cert.crt


Security considerations

  1. Key protection: Set appropriate permissions on private key files (such as 600) to avoid leakage
  2. Algorithm selection: Avoid using insecure algorithms (such as MD5, SHA1)
  3. Password strength: Use a strong password when encrypting
  4. Certificate validity: Renew expired certificates regularly
  5. Random number generation: Ensure the system has enough entropy for cryptographic operations

Frequently Asked Questions

Q1: How do I check the OpenSSL version?

openssl version

Q2: How do I generate a more secure ECC key?

openssl ecparam -genkey -name secp384r1 -out ecc.key

Q3: How do I convert certificate formats?

Convert from PEM to DER:

openssl x509 -in cert.pem -outform der -out cert.der


Practical Exercise

  1. Generate a 4096-bit RSA key pair
  2. Create a self-signed certificate valid for 2 years
  3. Encrypt a text file and decrypt it with the same password
  4. Check the SSL certificate information of websites you often visit

By mastering the openssl command, you will be able to handle a variety of encryption and security-related tasks and build a solid foundation for system security and administration.


Linux Command Reference Linux Command Reference