/commands/openssl/openssl-req-self-signed-certificate
openssl req -x509: create a self-signed certificate
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · OpenSSL 3.5.7 9 Jun 2026 (Library: OpenSSL 3.5.7 9 Jun 2026)updated 2026-10-20
- tool
- OpenSSL
- versions
- OpenSSL 1.1.1+ (-addext), OpenSSL 3.x
- os
- linux, macos, windows
openssl req -x509 creates a self-signed certificate directly, without a separate CSR step. Add -newkey to generate the key in the same command, -nodes to leave the key unencrypted, and -addext subjectAltName=... because browsers and most TLS clients match the host name against SAN, not CN.
One command
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes \
-keyout key.pem -out cert.pem -days 365 \
-subj "/CN=localhost" \
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1" 2>/dev/null
openssl x509 -in cert.pem -noout -subject -ext subjectAltName
ls key.pem cert.pemOutput
subject=CN=localhost
X509v3 Subject Alternative Name:
DNS:localhost, IP Address:127.0.0.1
cert.pem
key.pemOptions explained
| Option | Meaning |
|---|---|
| -x509 | output a self-signed certificate instead of a CSR |
| -newkey ec -pkeyopt ec_paramgen_curve:P-256 | new EC P-256 key (use rsa:3072 for RSA) |
| -nodes | do not encrypt the private key (no passphrase) |
| -keyout / -out | files for the private key / the certificate |
| -days 365 | validity period |
| -subj "/CN=localhost" | subject without interactive prompts |
| -addext "subjectAltName=..." | SAN entries clients actually check |
Trust it locally
A self-signed certificate is its own issuer, so clients reject it until you trust it explicitly: curl --cacert cert.pem https://localhost:8443/, or import it into the OS or browser trust store. For anything beyond local development, create a private CA and sign leaf certificates with it.
Sources
Related pages
errors · opensslunable to get local issuer certificate (OpenSSL verify error 20)OpenSSL error 20, unable to get local issuer certificate, means the issuing CA is missing from the trust store or the chain. Add the CA or intermediate.cheat sheets · opensslOpenSSL cheat sheet: keys, CSRs, certificates and TLS checksOpenSSL cheat sheet: generate keys and CSRs, inspect certificates, check that key and certificate match, convert PEM, DER and PFX, verify chains and test TLS.codes · opensslOpenSSL verify error codes: 2, 10, 18, 19, 20, 21, 62OpenSSL X509 verify errors explained: 10 certificate has expired, 18 self-signed, 19 self-signed in chain, 20 unable to get local issuer, 62 hostname mismatch.