Create a local CA and a TLS certificate with OpenSSL
Create a local certificate authority, issue a server certificate with SAN, verify it and install the CA on a Linux workstation.
This article shows how to create a local certificate authority (CA) with OpenSSL, then a TLS server certificate signed by that CA. It is useful for HTTPS in development or for internal services that should not be exposed publicly.
Create the certificate authority
Generate the CA private key (protected by a passphrase):
mkdir -p ~/certs && cd ~/certs
openssl genrsa -aes256 -out myCA.key 4096
Generate the self-signed CA certificate:
openssl req -x509 -new -nodes -key myCA.key -sha256 -days 1825 -out myCA.pem
Create a server certificate
Generate the server private key:
openssl genrsa -out server.key 2048
Create the extensions file. It must list the DNS names (SubjectAltName) covered by the certificate, because browsers ignore the CN alone:
# server.ext
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = app.example.test
DNS.2 = www.example.test
Create the certificate signing request (CSR), then sign it with the CA:
openssl req -new -key server.key -out server.csr
openssl x509 -req -in server.csr -CA myCA.pem -CAkey myCA.key -CAcreateserial \
-out server.crt -days 825 -sha256 -extfile server.ext
The -CAcreateserial option creates myCA.srl, which keeps track of the serial numbers issued.
Verify the certificate
Check that the certificate is signed by the CA and valid for a TLS server:
openssl verify -purpose sslserver -CAfile myCA.pem -verbose server.crt
Check that the private key matches the certificate. The two hashes must be identical:
openssl x509 -noout -modulus -in server.crt | openssl sha256
openssl rsa -noout -modulus -in server.key | openssl sha256
Display the readable contents of the certificate, without the most verbose fields:
openssl x509 -in server.crt -noout -text \
-certopt no_subject,no_header,no_version,no_serial,no_signame,no_validity,no_issuer,no_pubkey,no_sigdump,no_aux
Install the CA on the workstation
For browsers and system tools to accept certificates issued by the CA, install myCA.pem in the system trust store.
| Distribution | Commands |
|---|---|
| Arch Linux | sudo trust anchor --store myCA.pem, then sudo trust list to check |
| Debian / Ubuntu | copy myCA.pem to /usr/local/share/ca-certificates/myCA.crt, then sudo update-ca-certificates |
| Fedora / RHEL | copy myCA.pem to /etc/pki/ca-trust/source/anchors/, then sudo update-ca-trust |
Inspect the certificate of a remote site
To display the chain presented by a remote server:
openssl s_client -showcerts -connect www.example.com:443 </dev/null
Export to PKCS#12 (.p12 / .pfx)
Some applications, such as Windows servers, expect a PKCS#12 file containing the certificate and its key:
openssl pkcs12 -export -name servercert -in server.crt -inkey server.key -out server.p12
openssl pkcs12 -info -in server.p12
Notes
- Protect
myCA.key: anyone holding this key can issue certificates that the workstation will accept. Do not install the CA on machines that do not need it. - The domain names in the examples (
*.example.test) are placeholders. Replace them with the real service names. - A certificate issued by a local CA is trusted only on machines where that CA is installed.