keytool_cheatsheet - dwilson2547/wiki_demo GitHub Wiki
Here’s a Linux keytool
Cheat Sheet with common commands and examples:
keytool
is a Java-based command-line utility for managing keystores, certificates, and cryptographic keys. It is part of the Java Development Kit (JDK).
-
Linux
keytool
Cheat Sheet-
1. Common Commands
- Generate a Keystore and Key Pair
- List Contents of a Keystore
- Export a Certificate
- Import a Certificate into a Keystore
- Delete a Certificate or Key
- Change Keystore Password
- Change Key Password
- Generate a Certificate Signing Request (CSR)
- Import a CA-Signed Certificate
- Check Certificate Details
- Convert Keystore Format (JKS to PKCS12)
- List Trusted Certificates in a Keystore
- 2. Common Options
- 3. Tips
-
1. Common Commands
keytool -genkeypair -alias mydomain -keyalg RSA -keysize 2048 -keystore keystore.jks -validity 365
- Creates a keystore (
keystore.jks
) with a self-signed certificate. - Replace
mydomain
with your alias and set a password when prompted.
keytool -list -keystore keystore.jks
- Lists all entries in the keystore.
- Add
-v
for detailed output.
keytool -exportcert -alias mydomain -keystore keystore.jks -file mydomain.crt
- Exports the certificate for
mydomain
tomydomain.crt
.
keytool -importcert -alias importedcert -keystore keystore.jks -file certificate.crt
- Imports a certificate (e.g.,
certificate.crt
) into the keystore.
keytool -delete -alias mydomain -keystore keystore.jks
- Removes the entry with the alias
mydomain
.
keytool -storepasswd -keystore keystore.jks
- Prompts for the old and new keystore passwords.
keytool -keypasswd -alias mydomain -keystore keystore.jks
- Changes the password for the key associated with
mydomain
.
keytool -certreq -alias mydomain -keystore keystore.jks -file mydomain.csr
- Creates a CSR (
mydomain.csr
) for submission to a Certificate Authority (CA).
keytool -importcert -alias mydomain -keystore keystore.jks -file signed_cert.crt
- Imports a signed certificate (e.g.,
signed_cert.crt
) into the keystore.
keytool -printcert -file mydomain.crt
- Displays details of a certificate file.
keytool -importkeystore -srckeystore keystore.jks -destkeystore keystore.p12 -deststoretype PKCS12
- Converts a JKS keystore to PKCS12 format.
keytool -list -keystore $JAVA_HOME/lib/security/cacerts
- Lists trusted CA certificates in the default Java cacerts file.
- Default password:
changeit
.
Option | Description |
---|---|
-alias <alias> |
Specifies the alias for the entry. |
-keystore <file> |
Specifies the keystore file. |
-storepass <pass> |
Provides the keystore password. |
-keypass <pass> |
Provides the key password. |
-validity <days> |
Sets the validity period (default: 90 days). |
-keyalg <alg> |
Specifies the key algorithm (e.g., RSA). |
-keysize <size> |
Sets the key size (e.g., 2048). |
-
Default Keystore: If no keystore is specified,
keytool
uses$HOME/.keystore
. - Passwords: Always protect keystores and keys with strong passwords.
- Backup: Always back up your keystore file.