{"id":560,"date":"2022-08-01T01:11:00","date_gmt":"2022-08-01T01:11:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=560"},"modified":"2022-08-01T01:11:00","modified_gmt":"2022-08-01T01:11:00","slug":"create-self-signed-certificates-openssl","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=560","title":{"rendered":"How to Create Self-Signed Certificates using OpenSSL"},"content":{"rendered":"<p>In this guide, we have given step-by-step guides on how to <strong>create self-signed certificates<\/strong> using the OpenSSL utility. You can create self-signed certificates using commands or automate them using a shell script by following this guide.<\/p>\n<p><a href=\"https:\/\/www.openssl.org\/?ref=devopscube.com\" rel=\"noreferrer noopener\">Openssl <\/a>is a handy utility to create self-signed certificates. You can use OpenSSL on all the operating systems such as <strong>Windows, MAC, and Linux flavors<\/strong>.<\/p>\n<h2 id=\"what-is-a-self-signed-certificate\">What is a Self Signed Certificate?<\/h2>\n<p>A self-signed certificate is an SSL\/TSL certificate not signed by a public or private certificate authority. Instead, it is signed by the creator&#8217;s own personal or root CA certificate.<\/p>\n<p>Here is what we do to request paid SSL\/TLS certificate from a <strong>well-known Certificate Authority<\/strong> like Verisign or comodo.<\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-9-37.png\" class=\"kg-image\" alt=\"SSL\/TLS certificate request flow.\" loading=\"lazy\" width=\"869\" height=\"816\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-9-37.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-9-37.png 869w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<ol>\n<li>Create a <strong>certificate signing request (CSR)<\/strong> with a private key. A CSR contains details about location, organization, and FQDN (Fully Qualified Domain Name).<\/li>\n<li>Send the CSR to the trusted CA authority.<\/li>\n<li>The CA authority will send you the SSL certificate signed by their root certificate authority and private key.<\/li>\n<li>You can then validate and use the SSL certificate with your applications.<\/li>\n<\/ol>\n<p>But <strong>for a self-signed certificate<\/strong>, here is what we do.<\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-8-50.png\" class=\"kg-image\" alt=\"self-signed SSL\/TLS certificate creation workflow.\" loading=\"lazy\" width=\"872\" height=\"1044\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-8-50.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-8-50.png 872w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<ol>\n<li>Create our own root CA certificate &amp; CA private key (We act as a CA on our own)<\/li>\n<li>Create a server private key to generate CSR<\/li>\n<li>Create an SSL certificate with CSR using our root CA and CA private key.<\/li>\n<li>Install the CA certificate in the browser or Operating system to avoid security warnings.<\/li>\n<\/ol>\n<h2 id=\"need-for-our-own-certificate-authority\">Need For Our Own Certificate Authority<\/h2>\n<p>Most browsers &amp; operating systems hold a copy of root CA certificates of all the trusted certified Certificated Authorities. That&#8217;s the reason the browsers won&#8217;t show any security messages when you visit standard websites that use SSL from a trusted and <strong>well-known commercial Certificate authority.<\/strong><\/p>\n<p>The following image shows the <strong>root CA present in the Firefox<\/strong> browser by default.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-4-47.png\" class=\"kg-image\" alt=\"default root CA certificate available in browsers.\" loading=\"lazy\" width=\"620\" height=\"604\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-4-47.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-4-47.png 620w\"><\/figure>\n<p>At the same time, if you use a  self-signed certificate, your browser will throw a security warning. The reason is browsers only trust SSL from a trusted Certificate authority. For example,<\/p>\n<pre><code>Your connection is not private\nAttackers might be trying to steal your information from demo.apps.mlopshub.com (for example, passwords, messages or credit cards)<\/code><\/pre>\n<p>But you can force browsers &amp; operating systems to accept our own certificate authority. So you won&#8217;t see the security warning once you install the CA certificate and add it to the trusted list. You can also share the CA certificate with your development team to install in their browsers as well.<\/p>\n<p>Also, you can use this CA to create <strong>more than one SSL certificate<\/strong>.<\/p>\n<h2 id=\"create-certificate-authority\">Create Certificate Authority<\/h2>\n<p>As discussed earlier, we need to create our own root CA certificate for browsers to trust the self-signed certificate. So let&#8217;s create the root CA certificate first.<\/p>\n<p>Let&#8217;s create a directory named <code>openssl<\/code> to save all the generated keys &amp; certificates.<\/p>\n<pre><code>mkdir openssl &amp;&amp; cd openssl<\/code><\/pre>\n<p>Execute the following <code>openssl<\/code> command to create the <code>rootCA.key<\/code>and <code>rootCA.crt<\/code>. Replace <code>demo.mlopshub.com<\/code> with your domain name or IP address.<\/p>\n<pre><code>openssl req -x509 \\\n            -sha256 -days 356 \\\n            -nodes \\\n            -newkey rsa:2048 \\\n            -subj \"\/CN=demo.mlopshub.com\/C=US\/L=San Fransisco\" \\\n            -keyout rootCA.key -out rootCA.crt <\/code><\/pre>\n<p>We will use the <code>rootCA.key<\/code>and <code>rootCA.crt<\/code> to sign the SSL certificate.<\/p>\n<p><strong>Note<\/strong>: If you get the following error, comment <code>RANDFILE = $ENV::HOME\/.rnd<\/code> line in <code>\/etc\/ssl\/openssl.cnf<\/code><\/p>\n<pre><code>Can't load \/home\/vagrant\/.rnd into RNG<\/code><\/pre>\n<h2 id=\"create-self-signed-certificates-using-openssl\">Create Self-Signed Certificates using OpenSSL<\/h2>\n<p>Follow the steps given below to create the self-signed certificates. We will sign out certificates using our own root CA created in the previous step.<\/p>\n<h3 id=\"1-create-the-server-private-key\">1. Create the Server Private Key<\/h3>\n<pre><code>openssl genrsa -out server.key 2048<\/code><\/pre>\n<h3 id=\"2-create-certificate-signing-request-configuration\">2. Create Certificate Signing Request Configuration<\/h3>\n<p>We will create a <code>csr.conf<\/code> file to have all the information to generate the CSR. Replace <code>demo.mlopshub.com<\/code> with your domain name or IP address.<\/p>\n<pre><code>cat &gt; csr.conf &lt;&lt;EOF\n[ req ]\ndefault_bits = 2048\nprompt = no\ndefault_md = sha256\nreq_extensions = req_ext\ndistinguished_name = dn\n\n[ dn ]\nC = US\nST = California\nL = San Fransisco\nO = MLopsHub\nOU = MlopsHub Dev\nCN = demo.mlopshub.com\n\n[ req_ext ]\nsubjectAltName = @alt_names\n\n[ alt_names ]\nDNS.1 = demo.mlopshub.com\nDNS.2 = www.demo.mlopshub.com\nIP.1 = 192.168.1.5\nIP.2 = 192.168.1.6\n\nEOF<\/code><\/pre>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\u26a0\ufe0f<\/div>\n<div class=\"kg-callout-text\">In the above config file, only use the alt_names you use.<\/p>\n<p>For example, if you are using DNS use:<br \/>DNS.1 = demo.mlopshub.com<br \/>DNS.2 = www.demo.mlopshub.com<\/p>\n<p>If you are using IP, use:<br \/>IP.1 = 192.168.1.5<br \/>IP.2 = 192.168.1.6<\/div>\n<\/div>\n<h3 id=\"3-generate-certificate-signing-request-csr-using-server-private-key\">3. Generate Certificate Signing Request (CSR) Using Server Private Key<\/h3>\n<p>Now we will generate <code>server.csr<\/code> using the following command.<\/p>\n<pre><code>openssl req -new -key server.key -out server.csr -config csr.conf<\/code><\/pre>\n<p>Now our folder should have three files. <code>csr.conf<\/code>, <code>server.csr<\/code> and  <code>server.key<\/code><\/p>\n<h3 id=\"4-create-a-external-file\">4. Create a external file<\/h3>\n<p>Execute the following to create <code>cert.conf<\/code>  for the SSL certificate. Replace <code>demo.mlopshub.com<\/code> with your domain name or IP address.<\/p>\n<pre><code>cat &gt; cert.conf &lt;&lt;EOF\n\nauthorityKeyIdentifier=keyid,issuer\nbasicConstraints=CA:FALSE\nkeyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment\nsubjectAltName = @alt_names\n\n[alt_names]\nDNS.1 = demo.mlopshub.com\n\nEOF<\/code><\/pre>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\ud83d\udca1<\/div>\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note<\/strong><\/b>: If you are using IP instead of DNS, use the following line:<\/p>\n<p>IP.1 = 192.168.1.5<\/p>\n<p>Instead of:<\/p>\n<p>DNS.1 = demo.mlopshub.com<\/p><\/div>\n<\/div>\n<h3 id=\"5-generate-ssl-certificate-with-self-signed-ca\">5. Generate SSL certificate With self signed CA<\/h3>\n<p>Now, execute the following command to generate the SSL certificate that is signed by the <code>rootCA.crt<\/code>  and <code>rootCA.key<\/code> created as part of our own Certificate Authority.<\/p>\n<pre><code>openssl x509 -req \\\n    -in server.csr \\\n    -CA rootCA.crt -CAkey rootCA.key \\\n    -CAcreateserial -out server.crt \\\n    -days 365 \\\n    -sha256 -extfile cert.conf<\/code><\/pre>\n<p>The above command will generate <code>server.crt<\/code> that will be used with our <code>server.key<\/code> to <strong>enable SSL in applications.<\/strong><\/p>\n<p>For example, the following config shows the Nginx config using the server certificate and private key used for SSL configuration.<\/p>\n<pre><code>server {\n\nlisten   443;\n\nssl    on;\nssl_certificate    \/etc\/ssl\/server.crt;\nssl_certificate_key    \/etc\/ssl\/server.key;\n\nserver_name your.domain.com;\naccess_log \/var\/log\/nginx\/nginx.vhost.access.log;\nerror_log \/var\/log\/nginx\/nginx.vhost.error.log;\nlocation \/ {\nroot   \/home\/www\/public_html\/your.domain.com\/public\/;\nindex  index.html;\n}\n\n}<\/code><\/pre>\n<h2 id=\"install-certificate-authority-in-your-browseros\">Install Certificate Authority In Your Browser\/OS<\/h2>\n<p>You need to install the <code>rootCA.crt<\/code> in your browser or operating system to avoid the security message that shows up in the browser when using self-signed certificates.<\/p>\n<p>Installing self-signed CA certificates differs in Operating systems. For example, in MAC, you can add the certificate by double-clicking it and adding it to the keychain. Check the respective Operating system guide on installing the certificate.<\/p>\n<ol>\n<li><a href=\"https:\/\/support.apple.com\/en-in\/guide\/keychain-access\/kyca2431\/mac?ref=devopscube.com\" rel=\"noreferrer noopener\">For MAC check this guide<\/a><\/li>\n<li><a href=\"https:\/\/support.google.com\/chrome\/a\/answer\/3505249?hl=en&#038;ref=devopscube.com\" rel=\"noreferrer\">Adding certificate to chrome on Windows<\/a><\/li>\n<\/ol>\n<h2 id=\"shell-script-to-create-self-signed-certificate\">Shell Script To Create Self-Signed Certificate<\/h2>\n<p>If you want to create self-signed certificates quite often, you can make use of the following shell script. You just need to execute the script with the domain name or IP that you want to add to the certificate.<\/p>\n<p>Save the following shell script as <code>ssl.sh<\/code><\/p>\n<pre><code>#! \/bin\/bash\n\nif [ \"$#\" -ne 1 ]; then\n  echo \"Error: No domain or IP address provided\"\n  echo \"Usage: $0 &lt;domain_or_ip&gt;\"\n  exit 1\nfi\n\nDOMAIN=$1\n\n# Create root CA &amp; Private key\n\nopenssl req -x509 \\\n            -sha256 -days 356 \\\n            -nodes \\\n            -newkey rsa:2048 \\\n            -subj \"\/CN=${DOMAIN}\/C=US\/L=San Fransisco\" \\\n            -keyout rootCA.key -out rootCA.crt\n\n# Generate Private key\n\nopenssl genrsa -out ${DOMAIN}.key 2048\n\n# Detect if input is IP\nif [[ $DOMAIN =~ ^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$ ]]; then\n  ALT_NAMES=\"[ alt_names ]\nIP.1 = ${DOMAIN}\nIP.2 = 192.168.1.5\nIP.3 = 192.168.1.6\"\nelse\n  ALT_NAMES=\"[ alt_names ]\nDNS.1 = ${DOMAIN}\nDNS.2 = www.${DOMAIN}\"\nfi\n\n# Create csf conf\n\ncat &gt; csr.conf &lt;&lt;EOF\n[ req ]\ndefault_bits = 2048\nprompt = no\ndefault_md = sha256\nreq_extensions = req_ext\ndistinguished_name = dn\n\n[ dn ]\nC = US\nST = California\nL = San Fransisco\nO = MLopsHub\nOU = MlopsHub Dev\nCN = ${DOMAIN}\n\n[ req_ext ]\nsubjectAltName = @alt_names\n\n$ALT_NAMES\nEOF\n\n# create CSR request using private key\n\nopenssl req -new -key ${DOMAIN}.key -out ${DOMAIN}.csr -config csr.conf\n\n# Create a external config file for the certificate\n\ncat &gt; cert.conf &lt;&lt;EOF\n\nauthorityKeyIdentifier=keyid,issuer\nbasicConstraints=CA:FALSE\nkeyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment\nsubjectAltName = @alt_names\n\n$ALT_NAMES\n\nEOF\n\n# Create SSl with self signed CA\n\nopenssl x509 -req \\\n    -in ${DOMAIN}.csr \\\n    -CA rootCA.crt -CAkey rootCA.key \\\n    -CAcreateserial -out ${DOMAIN}.crt \\\n    -days 365 \\\n    -sha256 -extfile cert.conf<\/code><\/pre>\n<p>Set the script executable permission by executing the following command.<\/p>\n<pre><code>chmod +x ssl.sh<\/code><\/pre>\n<p>Execute the script with the domain name or IP. For example,<\/p>\n<pre><code>.\/ssl.sh demo.mlopshub.com<\/code><\/pre>\n<p>The script will create all the certificates and keys we created using the <code>individual commands<\/code>. The SSL certificate and private keys get named with the domain name you pass as the script argument. For example, <code>demo.mlopshub.com.key<\/code> &amp; <code>demo.mlopshub.com.crt<\/code><\/p>\n<h2 id=\"what-are-the-benefits-of-using-a-self-signed-certificate\">What are the benefits of using a self-signed certificate?<\/h2>\n<p>There are several benefits of using a self-signed certificate:<\/p>\n<ol>\n<li>You don&#8217;t need to rely on a third party to sign your certificate.<\/li>\n<li>You can create and use your own certificate authority.<\/li>\n<li>You don&#8217;t have to pay for a certificate from a CA.<\/li>\n<li>You have more control over your certificates.<\/li>\n<\/ol>\n<h2 id=\"what-are-the-drawbacks-of-using-a-self-signed-certificate\">What are the drawbacks of using a self-signed certificate?<\/h2>\n<p>There are also several drawbacks of using a self-signed certificate:<\/p>\n<ol>\n<li>Your users will need to <strong>install the certificate in their browsers<\/strong> or applications.<\/li>\n<li>Your users will need to <strong>trust your certificate authority<\/strong> manually.<\/li>\n<li>They unsafe for public facing applications.<\/li>\n<li>None of the browsers or operating systems trust the self-signed certificates unless the user installs them.<\/li>\n<li>Prone to man-in-the-middle attacks.<\/li>\n<\/ol>\n<p>In general, self-signed certificates are a good option for applications in which you need to prove your own identity. They&#8217;re also a <strong>good option for development and testing environments<\/strong>. However, they shouldn&#8217;t be used for production applications.<\/p>\n<h2 id=\"self-signed-certificates-in-organizations\">Self-Signed Certificates in Organizations<\/h2>\n<p>Many organizations use self-signed certificated for their internal applications that are not internet-facing. These certificates are generated using the organization&#8217;s internal PKI infrastructure.<\/p>\n<p><a href=\"https:\/\/devopscube.com\/what-is-devops-what-does-it-really-mean\/\">DevOps<\/a> teams and developers can request SSL certificates from the PKI infrastructure to be used in applications.<\/p>\n<h2 id=\"self-signed-certificate-faqs\">Self-Signed Certificate FAQ&#8217;s<\/h2>\n<p>Following are the frequently asked questions about self-signed certificate.<\/p>\n<h3 id=\"how-to-create-self-signed-certificated-on-windows\">How to create self-signed certificated on Windows?<\/h3>\n<p>You can create a <strong>self-signed <\/strong>certificate on <strong>windows<\/strong> using Openssl. The OpenSSL commands are the same for all operating systems. You can follow this guide to create a <strong>self-signed <\/strong>certificate on <strong>windows<\/strong> using this guide.<\/p>\n<h3 id=\"how-do-i-get-a-self-signed-certificate\">How do I get a self-signed certificate?<\/h3>\n<p>Self-signed certificate can be generated by you using tools like openSSL or CDSSL PKI toolkit.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this guide, we have learned how to <strong>create self-signed SSL certificates<\/strong> using OpenSSL.<\/p>\n<p>For production use cases, if you don&#8217;t want to spend money on SSL certificates, you can try out <a href=\"https:\/\/letsencrypt.org\/?ref=devopscube.com\" rel=\"noreferrer noopener\">Letsencrypt<\/a>.<\/p>\n<p>Hope this self-signed SSL guide was helpful with the script to automate the certificate generation. Do let us know if you face any issues.<\/p>\n<p>Also, SSL\/TLS is one of the important topics in DevOps. You can check out the <a href=\"https:\/\/devopscube.com\/become-devops-engineer\/\" rel=\"noreferrer noopener\">how to become a devops engineer<\/a> blog to know more.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/create-self-signed-certificates-openssl\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Create Self-Signed Certificates using OpenSSL \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/create-self-signed-certificates-openssl\/<\/p>\n","protected":false},"author":1,"featured_media":561,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=560"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/560\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/561"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}