Useful keytool commands for certificate management

keytool command


keytool command location: "java_install_location"/jre/bin/keytool
cacerts location: "java_install_location"/jre/lib/security/cacerts

(Generally this is the location of keytool command or cacerts, It may varry based on your environment)

Parameters for below examples


Alias Name/Label: "This is a cert"
Certifcate Filename: testcert.cer
Keystore Name: cacerts

Importing Certificate

keytool -import -trustcacerts -alias "Alias_Name" -file "Filename" -keystore "keystore_Name"

Example:

keytool -import -trustcacerts -alias "This is a cert" -file testcert.cer -keystore cacerts

This above command will import testcert.cer into the keystore cacerts with the label "This is a cert"

Listing Certificate

keytool -list -keystore "keystore_name"


Examples:

keytool -list -keystore cacerts

Lists all the certificates in the keystore cacerts

keytool -list -v -keystore cacerts

Lists all the details of all certificates in the keystore cacerts

keytool -list -alias "This is a cert" -keystore cacerts

Lists the certificate withe the Alias "This is a cert" in the kestore cacerts

keytool -list -v -alias "This is a cert" -keystore cacerts

Lists the certificate with the label "This is a cert" in the keystre cacerts


keytool -list -v -keystore cacerts |grep Alias

Lists the Alias of all the certificates in the keystre cacerts.

Deleting the certificate

keytool -delete -alias "Alias_Name" -keystore "Keystore_Name"


Example:
keytool -delete -alias "This is a cert" -keystore cacerts

This will delete the certificate with the alias "This is a cert" from the keystore cacerts.

1 comment: