Bitcoin Exchange Script

- Advertisement -

How to get common name (CN) from SSL certificate using openssl command

I configured and installed a TLS/SSL certificate in /etc/ssl/ directory on Linux server. I was wondering if can I find out the common name (CN) from the certificate using the Linux or Unix command line option?

Yes, you find and extract the common name (CN) from the certificate using openssl command itself.

What is the Common Name?

The common name (CN) is nothing but the computer/server name associated with your SSL certificate. For example, sxi.io or sxi.io or *.sxi.io is CN for this website. The CN usually indicate the host/server/name protected by the SSL certificate. Your SSL certificate is valid only if hostname matches the CN. Your browser can display the CN:

Fig.01: Example of host name and CN match giving out green icon

How do I get common name (CN) from SSL certificate?

The syntax is:
openssl x509 -noout -subject -in your-file.pem
openssl x509 -noout -subject -in exmaple.com.cer
openssl x509 -noout -subject -in /etc/ssl/exmaple.com.cer

For example:
$ openssl x509 -noout -subject -in /etc/ssl/glusterfs.pem
Sample outputs:

subject= /CN=gfs01

Another example:
$ openssl x509 -noout -subject -in /etc/ssl/sxi.io.crt
Sample outputs:

subject= /CN=sxi.io

Say hello to certtool command

You can install certtool using the gnutls-bin package on Debian or Ubuntu Linux. Type the following apt-get command/apt command:
$ sudo apt install gnutls-bin
Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libopts25
The following NEW packages will be installed:
  gnutls-bin libopts25
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 250 kB of archives.
After this operation, 990 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.linode.com/ubuntu xenial/main amd64 libopts25 amd64 1:5.18.7-3 [57.8 kB]
Get:2 http://mirrors.linode.com/ubuntu xenial-updates/universe amd64 gnutls-bin amd64 3.4.10-4ubuntu1.2 [192 kB]
Fetched 250 kB in 0s (14.3 MB/s)     
Selecting previously unselected package libopts25:amd64.
(Reading database ... 34082 files and directories currently installed.)
Preparing to unpack .../libopts25_1%3a5.18.7-3_amd64.deb ...
Unpacking libopts25:amd64 (1:5.18.7-3) ...
Selecting previously unselected package gnutls-bin.
Preparing to unpack .../gnutls-bin_3.4.10-4ubuntu1.2_amd64.deb ...
Unpacking gnutls-bin (3.4.10-4ubuntu1.2) ...
Processing triggers for libc-bin (2.23-0ubuntu5) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up libopts25:amd64 (1:5.18.7-3) ...
Setting up gnutls-bin (3.4.10-4ubuntu1.2) ...
Processing triggers for libc-bin (2.23-0ubuntu5) ...

Now run command as follows:
$ certtool -i < your-file.pem
$ certtool -i < /etc/ssl/sxi.io.crt | more

Sample outputs:

X.509 Certificate Information:
	Version: 3
	Serial Number (hex): 03bb567f2ab8cd904f4168e159115389351b
	Issuer: C=US,O=Let's Encrypt,CN=Let's Encrypt Authority X3
	Validity:
		Not Before: Sat Mar 04 23:01:00 UTC 2017
		Not After: Fri Jun 02 23:01:00 UTC 2017
	Subject: CN=sxi.io
	Subject Public Key Algorithm: RSA
	Algorithm Security Level: High (4096 bits)
.....
..
..

And there you have it, either use the openssl or certtool command to find out the common name (CN) from your SSL certificate.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Get real time updates directly on you device, subscribe now.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy