To create a self-signed certificate with OpenSSL, run the command openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 365. This single command generates a private key and a public certificate simultaneously, allowing you to encrypt traffic via HTTPS or SSL/TLS without paying a third-party Certificate Authority (CA). This is ideal for local development, internal testing environments, or home server setups where public trust is not required.
A self-signed certificate is essentially a digital identity document that you vouch for yourself. While a browser will flag it as "Not Secure" because it isn't signed by a trusted root authority (like DigiCert or Let's Encrypt), the encryption between the client and the server remains mathematically sound. This means the data is encrypted, but the identity of the server is not verified by an external party.
What You'll Need
- A computer running Linux, macOS, or Windows.
- The OpenSSL toolkit installed. Most Linux distributions and macOS come with it pre-installed. Windows users can install it via Git Bash, WSL, or a standalone binary.
- Terminal or Command Prompt access with permissions to write files to the current directory.
- Basic knowledge of navigating directories via the command line.
Step-by-Step
- Verify OpenSSL Installation: Open your terminal or command prompt and type
openssl version. If the system returns a version number (e.g., OpenSSL 3.0.x), you are ready to proceed. If the command is not recognized, you must install OpenSSL first. On Ubuntu/Debian, usesudo apt install openssl; on macOS, usebrew install openssl. - Create a Dedicated Directory: To avoid cluttering your system and to keep your private key secure, create a folder for your certificates. Run
mkdir my-certs && cd my-certs. This ensures that your key and certificate files are stored together. - Generate the Private Key and Certificate: Execute the following command to create both the key and the certificate in one step:
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 365 -nodes
Breakdown of these flags:req -x509: Tells OpenSSL to create a self-signed certificate instead of a certificate signing request (CSR).-newkey rsa:4096: Generates a new RSA key with a strength of 4096 bits. This is currently the industry standard for high security.-keyout key.pem: Specifies the filename for the private key.-out cert.pem: Specifies the filename for the public certificate.-sha256: Uses the SHA-256 hashing algorithm, which is required by modern browsers for security.-days 365: Sets the certificate to expire in one year.-nodes: Short for "no DES." This prevents OpenSSL from encrypting the private key with a password. If you omit this, you will have to enter a password every time your web server restarts.
- Fill Out the Certificate Information: After running the command, OpenSSL will prompt you for several pieces of information. The most critical field is the Common Name (CN). This must be the exact domain name or IP address you will use to access the server (e.g.,
localhost,192.168.1.50, ormyserver.local). If the CN does not match the URL you type into the browser, you will receive a "Hostname Mismatch" error in addition to the "Untrusted Authority" warning. Other fields like Country, State, and Organization are optional for self-signed certificates and can be left blank or filled with placeholders. - Verify the Generated Files: You should now see two files in your directory:
key.pemandcert.pem. To verify the contents of the certificate and ensure the expiration date and Common Name are correct, run:openssl x509 -in cert.pem -text -noout
Look for the "Validity" section to confirm the dates and the "Subject" section to confirm your Common Name. - Secure the Private Key: The
key.pemfile is the most sensitive part of this process. Anyone with this file can impersonate your server. On Linux or macOS, restrict permissions so only the owner can read it by runningchmod 600 key.pem. - Install the Certificate on Your Server: The process varies by software. For Nginx, you would point your configuration to these files using
ssl_certificate /path/to/cert.pem;andssl_certificate_key /path/to/key.pem;. For Apache, useSSLCertificateFileandSSLCertificateKeyFiledirectives.
Common Mistakes
One of the most frequent errors is ignoring the Common Name (CN). Users often put their own name or company name in the CN field instead of the server's hostname. Because browsers check the CN against the URL in the address bar, this results in a critical error that cannot be bypassed simply by clicking "Proceed." Always use the FQDN (Fully Qualified Domain Name) or the IP address.
Another mistake is using an outdated hashing algorithm. Older tutorials may suggest using -sha1. Modern browsers (Chrome, Firefox, Safari) have deprecated SHA-1 due to vulnerabilities. Using SHA-1 will often result in the browser rejecting the certificate entirely, regardless of whether it is self-signed or not. Always use -sha256.
Lastly, users often forget to set the correct file permissions on the private key. Leaving key.pem world-readable (chmod 644 or 777) is a significant security risk. In a production-like environment, the web server process should be the only entity with read access to the key.
Tips
If you are using this for a local development environment with multiple developers, consider creating a small internal Certificate Authority (CA). Instead of signing individual certificates, you create one Root CA certificate, install that Root certificate into every developer's "Trusted Root Certification Authorities" store, and then use that Root key to sign your individual server certificates. This removes the "Not Secure" warning across the whole team.
For those who find the interactive prompts tedious, you can pass the subject information directly in the command line using the -subj flag. For example: -subj "/C=US/ST=California/L=San Francisco/O=MyOrg/CN=localhost". This is particularly useful for automation scripts or Dockerfile setups where interactive input is impossible.
When choosing the RSA key size, 2048 bits is generally sufficient for most internal tasks, but 4096 bits provides a higher margin of safety against future computing power increases. The performance hit on modern CPUs is negligible for most home or small-office applications.
Troubleshooting
If your browser still says "Your connection is not private" after installation, first check if you are using HTTPS in the URL. A common mistake is installing the certificate but attempting to connect via http:// instead of https://.
If you see a "Hostname Mismatch" error, it means the Common Name you entered during Step 4 does not match the address in your browser. To fix this, you must regenerate the certificate with the correct CN. There is no way to "edit" a certificate once it is signed; you must create a new one.
If the OpenSSL command fails with a "Permission Denied" error, ensure you have write access to the folder you are in. If you are trying to save files to a system directory like /etc/ssl/private, you will need to prefix your command with sudo.
If you encounter an error stating that the private key is encrypted and the server cannot start, you likely forgot the -nodes flag. You can either regenerate the key with -nodes or provide the password to your server software in its configuration file (though the former is easier for development).
Frequently Asked Questions
Can I use a self-signed certificate for a public website?
You can, but it is not recommended. Every visitor will see a large security warning page telling them the site is untrusted. For public sites, use a free CA like Let's Encrypt to get a browser-trusted certificate.
What is the difference between a .pem, .crt, and .key file?
These are often just different extensions for the same thing. A .key file usually contains the private key. A .crt or .pem file usually contains the public certificate. PEM is a container format that uses Base64 encoding; both .crt and .pem files are often just PEM-formatted text.
How long should a self-signed certificate last?
While you can set them to last for 10 or 20 years, it is best practice to set them for 365 to 730 days. This encourages you to rotate your keys regularly, which is a fundamental security habit.
Do I need to install the certificate on my client machine?
To remove the "Not Secure" warning, yes. You must import the cert.pem file into your browser's or operating system's trusted root store. Otherwise, the browser has no way of knowing that you trust this specific certificate.
Is RSA the only option for key generation?
No, you can also use Elliptic Curve Cryptography (ECC). ECC provides similar security to RSA but with much smaller key sizes, which can lead to slightly faster SSL handshakes. However, RSA is more universally compatible with older software.