DMARC Monitoring is coming soon: collect and analyze DMARC and TLS-RPT reports in one place. Learn more
Certificate Monitoring

Self-Signed Certificates in OpenSSL

The exact openssl command for a self-signed certificate with SANs, what each client says when it meets one, and where self-signed is fine and where it isn't.

A self-signed certificate is one whose issuer is itself. No certificate authority checked anything or signed anything, so no client trusts it until you tell that client to. That's perfectly fine for a laptop, a lab, or a service where you control every client. It's wrong for anything the public or a partner connects to. Making one takes a single openssl command, and the part most tutorials get wrong is the hostname.

The Command

This creates a private key and a self-signed certificate in one step, with the hostnames in the Subject Alternative Name (SAN) where clients actually look for them:

openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -noenc \
    -keyout key.pem -out cert.pem -days 365 \
    -subj "/CN=dev.internal" \
    -addext "subjectAltName=DNS:dev.internal,DNS:localhost,IP:127.0.0.1"
Flag What it does
-x509 Write a self-signed certificate; without it, req writes a CSR
-newkey ec -pkeyopt ec_paramgen_curve:prime256v1 Generate a new ECDSA P-256 key alongside it
-noenc Leave the private key unencrypted so a server can load it without a passphrase
-keyout / -out Where the key and the certificate go
-days 365 Validity period; OpenSSL's default is only 30 days
-subj The subject, so openssl doesn't prompt you for it
-addext "subjectAltName=..." The names the certificate is valid for. IP: entries cover connections by address

If you need RSA for an old client, swap the key options for -newkey rsa:2048. On OpenSSL 1.1.1 use -nodes in place of -noenc; OpenSSL 3.0 deprecated -nodes but still accepts it. Our RSA vs ECDSA post covers which key type to pick.

Check What You Built

Always read the certificate back before you deploy it:

$ openssl x509 -in cert.pem -noout -text
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            41:4e:23:0b:d4:a7:2d:0c:d9:b0:72:82:19:94:e2:a7:0a:eb:fe:aa
        Signature Algorithm: ecdsa-with-SHA256
        Issuer: CN=dev.internal
        Validity
            Not Before: Oct 29 18:35:09 2026 GMT
            Not After : Oct 29 18:35:09 2027 GMT
        Subject: CN=dev.internal
        ...
        X509v3 extensions:
            X509v3 Subject Key Identifier:
                A0:FE:21:2A:81:A0:14:CB:0E:7F:5F:92:B8:A8:93:D1:CB:7A:19:DD
            X509v3 Authority Key Identifier:
                A0:FE:21:2A:81:A0:14:CB:0E:7F:5F:92:B8:A8:93:D1:CB:7A:19:DD
            X509v3 Basic Constraints: critical
                CA:TRUE
            X509v3 Subject Alternative Name:
                DNS:dev.internal, DNS:localhost, IP Address:127.0.0.1

Issuer and Subject are identical, and so are the two key identifiers: the certificate vouches for itself. The CA:TRUE line surprises people. You didn't ask for it; it comes from the v3_ca section of the stock openssl.cnf, which req -x509 applies by default. Remember it, because it matters further down. If you're new to reading this output, What Is a TLS Certificate? goes through it field by field.

Why the SAN Line Isn't Optional

Plenty of older guides stop at -subj "/CN=localhost" and leave out the SAN. Chrome has ignored the CN for hostname matching since version 58 and answers with NET::ERR_CERT_COMMON_NAME_INVALID. Go refuses too, even after you've told it to trust the certificate:

Get "https://localhost:8444/": tls: failed to verify certificate: x509: certificate relies on legacy Common Name field, use SANs instead

The trap is that the same CN-only certificate passed with curl, Python 3.9 and Node 22 when we tested it, because their hostname checks still fall back to the CN. So one client accepting the cert proves nothing about the next one. Put every name in the SAN and the question goes away.

What Clients Say When They Meet One

Serve the certificate with openssl s_server, which is handy for testing without touching a real web server:

openssl s_server -accept 8443 -cert cert.pem -key key.pem -www

Then connect with whatever your stack uses. Every one of them refuses, each in its own words:

# curl
curl: (60) SSL certificate OpenSSL verify result: self-signed certificate (18)

# Python (urllib or requests)
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate (_ssl.c:1147)

# Go
tls: failed to verify certificate: x509: certificate signed by unknown authority

# Node.js
DEPTH_ZERO_SELF_SIGNED_CERT self-signed certificate

# Java
javax.net.ssl.SSLHandshakeException: (certificate_unknown) PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Chrome shows NET::ERR_CERT_AUTHORITY_INVALID. These errors are working as designed. The client has no way to tell your certificate apart from one an attacker generated thirty seconds ago with the same command.

Trusting It Properly

The fix is to give the client the certificate as a trust anchor. Turning verification off with curl -k, verify=False, or NODE_TLS_REJECT_UNAUTHORIZED=0 also makes the error go away, and it does that by accepting every certificate from anyone, which is the one thing TLS exists to prevent. Those flags have a habit of surviving into production.

Per tool, it's an option or an environment variable:

curl --cacert cert.pem https://localhost:8443/
REQUESTS_CA_BUNDLE=/path/to/cert.pem python3 app.py      # Python requests
NODE_EXTRA_CA_CERTS=/path/to/cert.pem node app.js

System-wide, add it to the OS trust store:

# RHEL, Rocky, Alma, Fedora
cp cert.pem /etc/pki/ca-trust/source/anchors/dev.pem && update-ca-trust

# Debian, Ubuntu (the file must end in .crt)
cp cert.pem /usr/local/share/ca-certificates/dev.crt && update-ca-certificates

# macOS
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain cert.pem

Go and curl read the system store, so they pick this up. Python's requests uses the certifi bundle when it's installed from pip, so it can still need the environment variable. Java has its own cacerts keystore and needs keytool -importcert.

For a Team, Make a Tiny CA

Self-signing every service gets old fast: ten services means ten certificates for every developer to trust, and again each time one is regenerated. Create one small CA instead, have everyone trust it once, and sign service certificates from it. OpenSSL 3.0 and later can do the signing in the same req command:

# once: the CA (guard dev-ca.key)
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -noenc \
    -keyout dev-ca.key -out dev-ca.crt -days 3650 -subj "/CN=Dev Team CA"

# per service: a leaf signed by that CA
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -noenc \
    -keyout api.key -out api.crt -days 365 \
    -subj "/CN=api.dev.internal" \
    -addext "subjectAltName=DNS:api.dev.internal" \
    -addext "basicConstraints=critical,CA:FALSE" \
    -CA dev-ca.crt -CAkey dev-ca.key
$ openssl x509 -in api.crt -noout -issuer -ext basicConstraints
issuer=CN=Dev Team CA
X509v3 Basic Constraints: critical
    CA:FALSE
$ openssl verify -CAfile dev-ca.crt api.crt
api.crt: OK

That basicConstraints line is the CA:TRUE default from earlier. Leave it out and your service certificate comes out marked as a CA too, able in principle to sign certificates for any name your team's machines will trust. The same pattern, with a client CA, is the core of mutual TLS.

Where Self-Signed Is Fine, and Where It Isn't

Situation Self-signed? Why
Local development, localhost Fine You control the only client
Lab gear, test harnesses, CI Fine Pin or trust the cert in the test config
Internal service-to-service calls Fine with a private CA Every client is yours to configure
A public website No Every visitor gets a full-page warning
An API or webhook endpoint partners call No Their clients will refuse it, and you can't fix their trust stores
A mail server's MX Risky Most senders don't verify, but MTA-STS in enforce mode requires a chain to a trusted root

The MX row catches people out. Opportunistic TLS between mail servers has historically accepted almost any certificate, so a self-signed cert on port 25 mostly works. RFC 8461 says the certificate MUST chain to a root the sending server trusts, though, so once you publish an MTA-STS policy in enforce mode, senders that honor it stop delivering to you. For anything public, a free ACME certificate costs nothing and removes the question.

Self-Signed Certificates Expire Too

That -days 365 runs out a year from now, and nothing tells you. No CA sends a renewal reminder, no ACME client is watching it, and the person who ran the command may have moved on. The usual result is an internal service that starts failing every handshake on a Tuesday, a year after somebody set it up.

The other failure is a self-signed certificate turning up where it shouldn't: an appliance or load balancer that fell back to its built-in default after a bad deploy. Point our free SSL checker at a public hostname and it shows who issued what that host is serving right now.

Generator Labs Certificate Monitoring watches both kinds. Public endpoints are checked against the public trust stores. For your own certificates, import the self-signed certificate or your team CA as a Private CA and the chain check validates against it, so expiry, SAN coverage, and key strength are tracked like any other certificate, with on-premise agents reaching endpoints that never face the internet.

Start Monitoring Today

Free plan available. No credit card required.