Skip to content

How to Generate a CSR and Private Key Securely

A practical guide to generating a certificate signing request and private key, including key choices, file permissions, name fields and checks before you submit.

10 min readUpdated Nov 21, 2026By XPASS Editorial Team

How to Generate a CSR and Private Key Securely

A certificate signing request, usually called a CSR, is the first real step in getting an SSL certificate. It is the document you hand to a certificate authority to say: here is my public key, and here are the names I want the certificate to cover. The private key that belongs to that public key never leaves your control. Everything else about HTTPS depends on that arrangement staying intact.

That is why generating the pair carefully is worth a few minutes. Mistakes at this stage are quiet. A key created with weak permissions, a request that lists the wrong hostnames, or a lost key will not show up as errors until you try to install the certificate later. This guide shows how to create the key and request with OpenSSL, what each field means, how to store the key, and what to check before you submit. Later steps are covered in installing SSL via Plesk and installing SSL on Apache, and you will use the chain checks in how to verify an SSL certificate chain once the certificate is in place.

What a CSR contains, and what it does not

A CSR is a small text block that starts with -----BEGIN CERTIFICATE REQUEST-----. Inside it are:

  • Your public key, derived from your private key.
  • The subject details, such as organisation name, country and the common name.
  • Requested extensions, most importantly the Subject Alternative Name list, which holds the hostnames the certificate should cover.
  • A signature made with your private key, which proves that you hold the matching key.

The CSR does not contain the private key. It is public information, so you can paste it into an order form or email it without risk. The private key is the secret. Anyone who obtains it can impersonate your server for as long as the certificate is valid.

Modern browsers rely on the Subject Alternative Name list, and not the common name, to match a certificate to a hostname. Many certificate authorities copy the names from the order form and not from the CSR, but it is still good practice to include the correct names in the request so the files stay consistent.

Before you start: decisions to make

Which hostnames? List every name visitors will type, for example example.com and www.example.com. If you need a name covering many subdomains, a wildcard certificate may fit, but that is chosen when you order and may involve a different validation. The SSL page shows which certificate types are available once you know your list.

Where will the key be generated? The best place is the machine that will use it, or a trusted administration host. Generating on your own laptop and then copying the key is acceptable if you use a secure transfer and delete stray copies. Avoid online generators that create the key for you, because you cannot know who else sees it.

Which key type and size? RSA with a 2048-bit key is the most compatible choice and is accepted nearly everywhere. A 3072 or 4096-bit key is also accepted but makes handshakes somewhat heavier. ECDSA keys on the P-256 curve are smaller and fast, and widely supported by current software. Check what your certificate authority and your software accept.

Passphrase or not? Protecting the key file with a passphrase helps if the file is copied. The cost is that the service may ask for the passphrase at each start, which complicates automatic restarts. Many production servers store an unencrypted key and protect it with strict permissions, restricted accounts and disk encryption. Decide deliberately and write down the choice.

Step 1: Create a protected working folder

Start in a directory that only you can read. Setting a restrictive umask means new files are private by default:

umask 077
mkdir -p ~/csr-work && cd ~/csr-work

With umask 077, files you create are readable only by your account. This prevents a window in which the key exists with open permissions before you tighten them.

Name the files after the domain and the year so you can tell them apart later, for example example.com-2026.key and example.com-2026.csr.

Step 2: Generate the private key

For RSA:

openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out example.com-2026.key

For an ECDSA key on P-256:

openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out example.com-2026.key

If you want a passphrase, add -aes256 to the command and OpenSSL will prompt you to enter and confirm one. Choose a long phrase and store it in a password manager, not in a file next to the key.

Set strict permissions straight away, even if umask is already set:

chmod 600 example.com-2026.key

You can inspect the key without printing the secret by running openssl pkey -in example.com-2026.key -noout -text | head, but there is rarely a reason to do so. Never paste the contents of the key file into chat tools, tickets or web forms.

Step 3: Create the CSR

OpenSSL can read the subject details from the command line and the hostnames from an option. A common command with a modern OpenSSL version looks like this:

openssl req -new -key example.com-2026.key -out example.com-2026.csr \
  -subj "/C=TH/O=Example Co., Ltd./CN=example.com" \
  -addext "subjectAltName=DNS:example.com,DNS:www.example.com"

The fields mean:

Field Meaning
C Two-letter country code of the organisation.
O Organisation name, when the certificate type requires it.
CN Common name; use the main hostname.
subjectAltName The full list of hostnames the certificate should cover.

Some certificate types ask for only the common name and ignore other fields; others verify organisation details. Follow the instructions of the issuer you chose. Do not include an email address or other personal data unless the issuer asks for it, because the fields may become visible in the issued certificate.

If your OpenSSL version does not support -addext, create a small configuration file with a [req] section and a subjectAltName entry and pass it with -config. The result is the same.

Step 4: Check the request before you submit

Always read back what you are about to send:

openssl req -in example.com-2026.csr -noout -text -verify

Look for three things. The output should say that the signature verifies OK. The subject should show the names you intended. The Subject Alternative Name list should contain every hostname you need, spelled correctly. A typo here means a certificate that does not match the site.

Then confirm that the CSR and the key belong together. One simple way is to compare the public key derived from each:

openssl pkey -in example.com-2026.key -pubout | sha256sum
openssl req -in example.com-2026.csr -noout -pubkey | sha256sum

The two hashes must be identical. If they differ, the request was created with a different key, and the certificate issued from it will not work with this key file.

Step 5: Submit the CSR and store the key

Open the .csr file in a text editor and copy its complete contents, including the BEGIN and END lines, into the certificate authority's order form. Or upload the file if the form allows it. Check that no extra spaces or line breaks were added by the browser.

While you wait for the issuer, keep the private key where it is. Do not delete it, rename it carelessly or generate a second key and mix up the files. Back up the key in an encrypted form to a location with restricted access, such as an encrypted archive on a secure drive or a secrets manager. When the certificate arrives you will pair it with this key. If you create the request in a control panel, the panel stores the key for you, and the process in the Plesk guide shows where that happens.

A plain example

A small company, Example Co., wants HTTPS for example.com and www.example.com on its own Linux server.

  1. The administrator logs in to the server, sets umask 077 and creates a working folder.
  2. They generate an RSA 2048-bit key named example.com-2026.key and confirm its permissions are 600.
  3. They create the CSR with both hostnames in the Subject Alternative Name list.
  4. They run the verification command and the public key comparison. The signature verifies, the names are right, and the hashes match.
  5. They paste the CSR into the issuer's form and make an encrypted backup of the key.
  6. When the certificate arrives days later, they install it with the matching key on the web server, following a guide such as the one for Apache, and then verify the chain from outside.

At no point does the key leave the server, and nobody sends it by email.

Limits and what to watch for

A CSR does not prove the site is safe. It only starts the issuing process. The certificate authority decides how to validate your control of the names, and the type of validation depends on the certificate you order.

Do not reuse a key forever. Many administrators generate a new key for every renewal. Rotating keys limits the damage if an old copy leaked without anyone noticing. Whether you reuse or rotate is a policy choice; if you reuse, protect the key even more carefully.

Key exposure means replacement. If the private key may have been exposed, for example by being committed to a repository, sent over chat or left in a public folder, ask the issuer to revoke or replace the certificate and generate a new key. Do not simply hope that nobody found it.

Do not store keys in code repositories. Add key file patterns to ignore lists, and scan repositories for accidentally committed keys.

Wrong names are costly. A typo in the Subject Alternative Name list produces a certificate that does not match the site. Fixing it means a new request and often a reissue, so check the names before you submit.

Shared hosting and panels differ. Some hosting accounts do not give shell access. In that case generate the request inside the control panel, where the key is stored on the server and you never handle the file. Not everything described here applies to such setups, but the principles do.

Permissions after copying. Copying a key to the web server with a tool that resets permissions, or from a drive that does not support Unix modes, can leave it readable by others. Check the mode on the destination.

A short checklist

  1. Work in a private folder with a restrictive umask.
  2. Generate the key on a trusted machine and set mode 600.
  3. Decide on passphrase and key type deliberately.
  4. Put every needed hostname in the Subject Alternative Name list.
  5. Verify the CSR signature and compare the public key hashes.
  6. Submit only the CSR, never the key.
  7. Keep an encrypted backup of the key until the certificate is installed and working, and keep one afterwards for as long as your policy requires.
  8. After installation, check the chain from outside the server.

Summary

Generate the private key in a protected folder, set strict permissions, create a CSR that lists every hostname in the Subject Alternative Name field, verify the signature, compare the public keys and send only the request to the certificate authority. Keep the key secret, backed up in an encrypted form and out of repositories, and replace it if it may have been exposed. Once you know what names you need, the SSL page lists the certificate options available at the moment.

Frequently asked questions

What is a CSR?

A certificate signing request is a text file that contains your public key and the identity details you want on the certificate, such as the hostnames. You send it to a certificate authority, which signs it and returns a certificate. It does not contain the private key.

Is it safe to send a CSR by email?

Yes, a CSR holds only public information, so sending it to a certificate authority is normal. The private key is a different matter: it must never be sent to anyone.

Should I put a passphrase on the private key?

A passphrase protects the key file if it is copied. The trade-off is that a service may need the passphrase at every start. Many servers use an unencrypted key protected by strict file permissions and access control; choose what fits your environment and document it.

RSA or ECDSA key: which should I choose?

Both are widely supported. RSA with a 2048-bit or larger key is the most compatible choice. ECDSA keys, such as P-256, are smaller and fast. Check what your certificate authority and your software accept before deciding.

What do I do if the private key is lost?

The certificate cannot be used without its key. Generate a new key and CSR, ask the issuer to reissue or replace the certificate, and install the new pair.

About the author

Bob Kanokphon Thongdee, founder of XPASS VPN

Bob Kanokphon Thongdee

  • Founder of XPASS VPN
  • Co-founder of eSIMonline.asia
  • More than 7 years in online marketing
  • Founder of Cojourney Visa and Cojourney Travel

Need an SSL certificate?

Pick the certificate type that fits your site and see the price before you check out.

SSL from $14.99

See SSL certificates

Keep reading

SSL Guides

How to Install an SSL Certificate via Plesk

A step-by-step guide to installing an SSL certificate in Plesk, from creating the request or uploading files to assigning the certificate, redirecting HTTP and checking the result.

9 min read

SSL Guides

How to Verify an SSL Certificate Chain

A certificate can look valid in one browser and fail in another because the chain is incomplete. This guide shows how to read, test and repair a chain.

10 min read

SSL Guides

How to Install an SSL Certificate on Apache

A clear walkthrough for installing an SSL certificate on Apache, covering mod_ssl, the HTTPS virtual host, the certificate chain, redirects and verification.

10 min read