Linux openssl command
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:
- Create and manage SSL certificates
- Encrypt/decrypt files
- Generate key pairs
- Test SSL connections
- Calculate the hash value
- Verify digital signatures
Basic Syntax
The basic syntax of the openssl command is:
openssl command [command_options] [command_args]
Where:
command: The OpenSSL subcommand to execute (such as genrsa, req, x509, etc.)command_options: Option parameters for the subcommandcommand_args: Arguments for the subcommand
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:
-out: Specify the output file2048: Key length (in bits)-pubout: Output the public key
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:
-new: Create a new request-key: Specify the private key file-days: Certificate validity period (in days)-x509: Output a certificate in X.509 format
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:
-aes-256-cbc: Use the AES-256-CBC algorithm-salt: Add a random salt to enhance security-in: Input file-out: Output file-d: Decryption mode
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:
-connect: Specify the host and port-showcerts: Display the server certificate chain
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
- Key protection: Set appropriate permissions on private key files (such as 600) to avoid leakage
- Algorithm selection: Avoid using insecure algorithms (such as MD5, SHA1)
- Password strength: Use a strong password when encrypting
- Certificate validity: Renew expired certificates regularly
- 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
- Generate a 4096-bit RSA key pair
- Create a self-signed certificate valid for 2 years
- Encrypt a text file and decrypt it with the same password
- 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