Use the openssl req command to generate a self-signed certificate with a private key, specifying subject details, key size, and expiration date. This creates a digitally signed certificate valid for testing, local development, or internal services.
OpenSSL is a toolkit for managing cryptographic keys and certificates. When you run openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes, you generate a certificate that signs itself using RSA encryption. The certificate includes public key data, issuer and subject fields, and a signature verifying its authenticity.
This process does not involve a Certificate Authority (CA). Instead, the entity creating the certificate acts as its own CA. The resulting file pair-cert.pem and key.pem-can be used to secure HTTPS endpoints, configure email servers, or test TLS connections locally.
The openssl req command performs several steps. First, it generates a 2048-bit RSA private key. The -nodes flag disables password protection on the key. Next, it prompts for X.509 certificate fields such as country, organization, and common name. These values populate the certificate's subject field.
After collecting input, OpenSSL constructs a certificate request. The -x509 flag tells OpenSSL to output a self-signed certificate rather than a certificate signing request (CSR). The -days 365 option sets the validity period to one year. Finally, the certificate is signed using the private key, completing the creation process.
When a client connects to a service using this certificate, it validates the chain of trust. Since no external CA issued the certificate, the client must explicitly trust the self-signed certificate. Browsers display warnings unless the certificate is manually added to the trusted store.
During execution, OpenSSL asks for:
localhost.Use -subj to bypass prompts. For example:
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/C=US/ST=California/L=San Francisco/O=MyOrg/CN=localhost"
Valid key sizes include 1024, 2048, and 4096 bits. Use 2048 or higher for security. Smaller keys may be vulnerable to brute-force attacks.
Self-signed certificates fall into two categories based on purpose:
| Type | Use Case | Validity Period | Security Consideration |
|---|---|---|---|
| Development Testing | Local web servers, API testing, mobile app debugging | 30-90 days | Not trusted by browsers; requires manual trust setup |
| Internal Systems | Internal websites, IoT devices, corporate applications | 1-3 years | Can be distributed via enterprise PKI or internal CA |
| Temporary Deployment | Short-term services, CI/CD pipelines, containerized apps | 7-30 days | Automated rotation reduces risk of long-lived exposure |
A common mistake is assuming self-signed certificates are inherently insecure. They are cryptographically sound if generated with strong keys and proper practices. However, they lack third-party validation, which prevents automatic trust in most environments.
For example, a developer setting up a Node.js server with HTTPS can use openssl create self signed certificate to test TLS functionality without buying a domain-validated certificate. The server loads cert.pem and key.pem to enable encrypted communication.
Many users confuse self-signed certificates with CSR files. A CSR (Certificate Signing Request) is a request sent to a CA for a signed certificate. It contains public key and identity information but not the private key. A self-signed certificate includes both the public certificate and the private key.
Another misconception is that self-signed certificates cannot be used in production. While they're not suitable for public-facing websites due to browser warnings, they are widely used in internal networks. According to NIST SP 800-52 Rev. 2, self-signed certificates are acceptable for internal systems when properly managed.
Some assume the certificate must be renewed annually. That's not required. You can set any duration between 1 day and 10 years. Setting a shorter lifespan improves security through regular rotation. For example, a 30-day validity period forces renewal every month, reducing the window of exposure if the private key is compromised.
Suppose you're configuring a test environment for a web application running on port 8443. You want to verify HTTPS behavior without a domain or CA.
Run this command:
openssl req -x509 -newkey rsa:2048 -keyout server.key -out server.crt -days 60 -nodes -subj "/C=US/ST=NY/L=New York/O=Test Org/CN=test.local"
This generates a 60-day certificate valid for test.local. The private key is stored in server.key, and the certificate in server.crt. Your server configuration can then reference both files.
On macOS, you may need to import the certificate into the System keychain. Open Keychain Access, drag server.crt into the login keychain, and mark it as trusted for HTTPS.
I've used this method to test a React app with HTTPS during development. After importing the certificate, Chrome accepted the connection without warning. The app ran securely behind a proxy, mimicking a production setup.
To inspect a certificate, use openssl x509 -in cert.pem -text -noout. This displays the subject, issuer, validity dates, and public key algorithm.
For extracting certificate details from a running service, use openssl s_client -connect localhost:8443 -servername test.local. This connects to the server and outputs the certificate chain.
Always validate the certificate's fingerprint before trusting it. Run openssl x509 -in cert.pem -fingerprint -sha256 -noout to get a hash for verification.
Understanding how to openssl create self signed certificate enables secure local testing, internal system deployment, and rapid prototyping. The command is consistent across Linux, macOS, and Windows Subsystem for Linux.