Skip to content

/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

bash
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.pem

Output

subject=CN=localhost
X509v3 Subject Alternative Name:
    DNS:localhost, IP Address:127.0.0.1
cert.pem
key.pem

Options explained

OptionMeaning
-x509output a self-signed certificate instead of a CSR
-newkey ec -pkeyopt ec_paramgen_curve:P-256new EC P-256 key (use rsa:3072 for RSA)
-nodesdo not encrypt the private key (no passphrase)
-keyout / -outfiles for the private key / the certificate
-days 365validity 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