Two jobs share almost the same command and get confused constantly. To get a certificate from a public CA you generate a private key plus a Certificate Signing Request (CSR) and submit the CSR: openssl req -new -newkey rsa:2048 -nodes -keyout key.pem -out req.csr. To get a certificate right now for a lab, an internal service, or a dev box, you self-sign in one shot with -x509 and skip the CA entirely. Both paths start from the same private key, and both now share one non-negotiable requirement: every name the certificate is meant to cover must appear in the subjectAltName (SAN) extension, because modern browsers ignore the Common Name completely. This guide covers both paths, the SAN detail that trips everyone, and how to prove a key, CSR, and certificate actually belong together.
What you’ll accomplish
- Generate an unencrypted RSA (or ECDSA) private key and a CSR carrying the correct subject and SAN.
- Add
subjectAltNametwo ways: the modern-addextflag, and a config-file section that also works on OpenSSL older than 1.1.1. - Inspect a CSR and a certificate to confirm exactly what they contain before you submit or deploy.
- Produce a self-signed certificate in one command for internal or development use.
- Verify that a private key, its CSR, and the resulting certificate all match, and fix the common SAN and mismatch failures.
Prerequisites
- OpenSSL installed. Check the build with
openssl version. Some macOS machines symlinkopensslto LibreSSL, whose extension handling and-addextsupport differ; for anything below install the OpenSSL project build (brew install openssl@3) and call that binary. - A shell and a working directory you control. Everything here writes files into the current directory; treat
key.pemas a secret and keep it out of version control. - The hostname(s) the certificate must cover. The examples use
example.comandwww.example.com; substitute your own.
Confirm the version first, because the -addext flag needs OpenSSL 1.1.1 or newer:
openssl version
OpenSSL 3.0.13 30 Jan 2024
If you see OpenSSL 1.0.2 or LibreSSL, skip -addext and use the config-file method in step 3.
How it works
A private key is generated first and stays on your host. From it you build a CSR: a small signed document that carries the subject (your organization and Common Name), the public half of the key, and, crucially, the requested extensions including the SAN list. You sign the CSR with the private key itself, which proves to the CA that you hold the key for the public key inside it. From there the paths diverge.
On path A you send the CSR (never the private key) to a Certificate Authority. The CA validates that you control the names, signs a certificate, and returns it; that certificate is trusted by browsers because the CA’s root is already in every trust store. On path B you skip the CA and sign the certificate with your own key using -x509. That works instantly and costs nothing, but no one else trusts your key, so a self-signed certificate is for internal services, local development, and testing, never a public website. The SAN requirement is the same on both paths: since roughly 2017 every mainstream browser matches the hostname only against subjectAltName and ignores the Common Name, so a certificate without a SAN fails validation even when the CN is perfect.
Step-by-step
1. Generate a private key and CSR together (the classic form). -newkey rsa:2048 creates a fresh 2048-bit RSA key, -nodes leaves it unencrypted (no passphrase, which is what you want for a key a service loads unattended), -keyout writes the key, and -out writes the CSR. Without -addext this produces a CSR with no SAN, so treat this as the shape of the command; the next step adds the SAN you actually need.
openssl req -new -newkey rsa:2048 -nodes -keyout key.pem -out req.csr \
-subj "/C=CA/ST=Quebec/L=Montreal/O=Example Inc/CN=example.com"
.....+++++
....................+++++
writing new private key to 'key.pem'
-----
Passing -subj fills the distinguished name non-interactively; omit it and OpenSSL prompts you field by field. You now have key.pem and req.csr, but no SAN yet.
2. Add the SAN with -addext (OpenSSL 1.1.1 and newer). This is the one-shot form you will use most. The -addext "subjectAltName=..." flag writes the SAN directly into the CSR. List every name the certificate must serve, including the bare domain and each subdomain:
openssl req -new -newkey rsa:2048 -nodes -keyout key.pem -out req.csr \
-subj "/C=CA/ST=Quebec/L=Montreal/O=Example Inc/CN=example.com" \
-addext "subjectAltName=DNS:example.com,DNS:www.example.com"
.....+++++
.................+++++
writing new private key to 'key.pem'
-----
For an IP-based service the SAN entry uses IP: instead of DNS:, for example -addext "subjectAltName=DNS:example.com,IP:203.0.113.10".
3. Add the SAN with a config file (portable, works on OpenSSL below 1.1.1). Older OpenSSL has no -addext, and a config file is also cleaner when the name list is long or reused. Create csr.cnf:
[ req ]
default_bits = 2048
prompt = no
default_md = sha256
distinguished_name = dn
req_extensions = req_ext
[ dn ]
C = CA
ST = Quebec
L = Montreal
O = Example Inc
CN = example.com
[ req_ext ]
subjectAltName = @alt_names
[ alt_names ]
DNS.1 = example.com
DNS.2 = www.example.com
Then generate the key and CSR, pointing -config at the file and telling req to include the extension section:
openssl req -new -newkey rsa:2048 -nodes -keyout key.pem -out req.csr \
-config csr.cnf
.....+++++
..............+++++
writing new private key to 'key.pem'
-----
The req_extensions = req_ext line is what pulls the [ req_ext ] section (and its subjectAltName) into the CSR. Leave it out and the [ alt_names ] block is silently ignored.
4. Inspect the CSR before you submit it. Never send a CSR you have not read. -verify checks that the CSR’s self-signature is valid, and -text decodes the contents so you can confirm the subject and, most importantly, the SAN:
openssl req -in req.csr -noout -text -verify
Certificate request self-signature verify OK
Certificate Request:
Data:
Version: 1 (0x0)
Subject: C=CA, ST=Quebec, L=Montreal, O=Example Inc, CN=example.com
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Attributes:
Requested Extensions:
X509v3 Subject Alternative Name:
DNS:example.com, DNS:www.example.com
If the Subject Alternative Name block is absent, stop: the CSR has no SAN and the issued certificate will fail in browsers. Go back to step 2 or 3. On OpenSSL 1.1.1 the verify line reads verify OK rather than self-signature verify OK; both mean the same thing.
5. Generate a self-signed certificate directly (path B, internal/dev only). Adding -x509 turns the same req command into a certificate factory: it creates the key, builds the request in memory, and signs the certificate with that key, all at once. -days sets the lifetime. Note that -x509 needs -addext to carry the SAN into the certificate:
openssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out cert.pem -days 365 \
-subj "/C=CA/ST=Quebec/L=Montreal/O=Example Inc/CN=example.com" \
-addext "subjectAltName=DNS:example.com,DNS:www.example.com"
.....+++++
...........+++++
writing new private key to 'key.pem'
-----
You now have key.pem and a cert.pem you can point Nginx, a dev server, or an internal service at immediately. It will trip a browser warning on the public internet because nothing trusts your key; that is expected, not a bug.
6. Use ECDSA instead of RSA (optional). ECDSA keys are smaller and faster and are fine for both paths. Swap the -newkey argument and set the curve; prime256v1 (also called P-256) is the safe default:
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \
-keyout ec-key.pem -out req.csr \
-subj "/C=CA/O=Example Inc/CN=example.com" \
-addext "subjectAltName=DNS:example.com,DNS:www.example.com"
writing new private key to 'ec-key.pem'
-----
Everything downstream (inspection, submission, self-signing) is identical; only the key type changed.
Validate it works
Prove the three artifacts belong together before you deploy anything. First read the certificate itself and confirm the SAN survived:
openssl x509 -in cert.pem -noout -text | grep -A1 "Subject Alternative Name"
X509v3 Subject Alternative Name:
DNS:example.com, DNS:www.example.com
Then confirm the private key and the certificate are actually a pair. For RSA, compare the modulus of each; matching hashes mean they belong together:
openssl x509 -in cert.pem -noout -modulus | openssl sha256
openssl rsa -in key.pem -noout -modulus | openssl sha256
SHA2-256(stdin)= 5f2b...c91a
SHA2-256(stdin)= 5f2b...c91a
For an ECDSA key the -modulus trick does not apply; compare the public keys instead, which works for any key type:
openssl x509 -in cert.pem -noout -pubkey | openssl sha256
openssl pkey -in key.pem -pubout | openssl sha256
SHA2-256(stdin)= a83e...44d7
SHA2-256(stdin)= a83e...44d7
Matching output on either pair means the key, the CSR you built from it, and the certificate all line up.
Troubleshooting
- Certificate works in curl but browsers show
NET::ERR_CERT_COMMON_NAME_INVALID. The certificate has no SAN, or the SAN does not list the name in the address bar. Confirm withopenssl x509 -in cert.pem -noout -text | grep -A1 "Subject Alternative Name"; if it is empty, the certificate was issued from a CSR that lacked a SAN. Reissue: rebuild the CSR with-addext(step 2) or the config file (step 3), including every hostname. The Common Name alone is never enough on a modern browser. key values mismatchwhen the server loads the pair. Thekey.pemandcert.pemare not from the same key, usually because a later command overwrotekey.pemwith a fresh key whilecert.pemwas signed by the old one. Run the modulus (RSA) or pubkey (any type) comparison in the Validate section; if the hashes differ, the pair is wrong. Regenerate both together, or find the original key.unable to load Private Key. Almost always a format or content problem: the file is a certificate rather than a key, is truncated, or is a PKCS#8/encrypted key OpenSSL cannot read without the passphrase. Check what it is withopenssl pkey -in key.pem -noout -text; if that errors too, the file is not a usable private key. Watch for a stray-----BEGIN CERTIFICATE-----header, which means you pointed the key flag at the cert.- You wanted an unencrypted key but OpenSSL keeps asking for a passphrase. You omitted
-nodes, so the key was written encrypted. A service that cannot type a passphrase at boot will fail to start. Strip the passphrase from an existing key withopenssl rsa -in key.pem -out key.pem(it prompts once for the current passphrase and writes an unencrypted key), or regenerate with-nodes. On OpenSSL 3.x,-nodesstill works but the documented spelling is-noenc; both are accepted. - The self-signed certificate is not trusted (
self-signed certificate/ browser warning). This is expected: nothing trusts your key. Do not try to make the public internet accept it. For a machine you control, add the certificate to that machine’s local trust store so your own tools stop warning, for example on Debian/Ubuntu copycert.peminto/usr/local/share/ca-certificates/(renamed to end in.crt) and runupdate-ca-certificates. That trusts it only on that host. If untrusted certificates are unacceptable, you need a real CA (path A) or an internal CA whose root you distribute.
When this is the wrong approach
Self-signing is the wrong tool for anything the public reaches: browsers will warn, and users learning to click through warnings is a real security cost. For a public website, get a free certificate from a real CA instead; Let’s Encrypt with certbot automates issuance and renewal so you never hand-run these commands in production. If you already have certificates and are debugging why a client rejects one, the problem is usually the chain, not the CSR, covered in verify a TLS certificate chain with OpenSSL. And before you request a certificate from a particular CA, check that your domain’s CAA records actually permit that CA to issue, or the request is refused no matter how correct your CSR is. The authoritative reference for every flag here is the OpenSSL documentation, specifically the openssl-req manual page.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.