Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete already import certificate/alias by keytool command?

Tags:

java

ssl

keytool

I am trying to delete already import certificate by keytool command

 keytool -delete -noprompt -alias "initcert" -keystore keycloak.jks

But getting below exception

keytool error: java.lang.Exception: Keystore file does not exist: keycloak.jks

Same issue with

keytool -delete  -alias "initcert" -keystore keycloak.cer

issue

keytool error: java.lang.Exception: Keystore file does not exist: keycloak.cer

Now i am trying to import the certificate with same alias name

 keytool -import -noprompt -trustcacerts -alias "initcert" -file "C:\Code_Base\keycloak_certificates\keycloak_135.250.138.74_server\keycloak.cer" -keystore "C:\Program Files\Java\jdk1.8.0_152\jre\lib\security\cacerts"

But again end with

keytool error: java.lang.Exception: Certificate not imported, alias already exists

like image 339
Subodh Joshi Avatar asked Jan 11 '18 09:01

Subodh Joshi


People also ask

How do I remove alias from Keytool?

Check the contents of the trust store by entering the following in the command prompt: <JAVA_HOME>\bin\keytool -list -v -keystore truststore -storepass access . Note the alias names of the certificates you want to remove. Enter <JAVA_HOME>\bin\keytool -delete -alias <alias name> -keystore truststore.

How do I remove an alias from a keystore in Java?

After identifying the alias entry names to be removed, use keytool delete command to remove them.

How do I delete a key from Keytool?

Use the keytool -delete command to delete an existing certificate.


2 Answers

It seems you didn't write the full keystore path. The command should be like this:

keytool -delete -noprompt -alias "initcert" -keystore "C:\Path\to\your\keystore\keycloak.jks"

About the last error, as other pointed out, "cacerts" is different keystore than your keycloak where you have already imported your certificate. You can check if your alias is in there by using the following command:

keytool -list -keystore "C:\Program Files\Java\jdk1.8.0_152\jre\lib\security\cacerts"

And to delete it:

keytool -delete -noprompt -trustcacerts -alias "initcert" -keystore "C:\Program Files\Java\jdk1.8.0_152\jre\lib\security\cacerts"

Then, if you import again the certificate, the error would not appear.

Finally, one last thing, if there is an error like this

keytool error: java.io.FileNotFoundException: C:\Path\to\your\keystore\keycloak.jks (Permission denied)

You should execute the command window in Administrator mode.

like image 97
mardo Avatar answered Sep 21 '22 21:09

mardo


You can make use of KeyStore Explorer to check if exists and manage your certs easily,

KeyStore Explorer Download Link

Just open your keystore file with the explorer, do the stuff you want and save it back.

like image 32
Praveen Avatar answered Sep 21 '22 21:09

Praveen