78

I have tried to execute keytool from Java bin directory but I get an error with warning bash: keytool: command not found.

root@xxxxxx]# keytool -genkey -alias mypassword -keyalg RSA

bash: keytools: command not found
Bruno
  • 119,590
  • 31
  • 270
  • 376
Xcalibur
  • 1,865
  • 1
  • 12
  • 9

13 Answers13

96

These are the steps which solved my problem:

  • Search the path where my java was installed

    find / -name jre
    
  • Move to java directory (where jre was installed on my server)

    cd /path/to/jre/bin
    
  • Create SSL certificate with keytool command

    ./keytool -genkey -alias [mypassword] -keyalg [RSA]
    
Chaminda Bandara
  • 2,067
  • 2
  • 28
  • 31
Xcalibur
  • 1,865
  • 1
  • 12
  • 9
  • 1
    @user217929 how can we solve this problem by adding `keytool` to `$PATH` so that the command can be executed from any directory? – qualebs May 23 '17 at 09:10
  • 9
    Add `export PATH=$PATH:/path/to/jre/bin` to your `.bashrc` file. – Daniel Gray Aug 22 '17 at 12:04
  • 1
    note that the keytool is not in the jre folder, but in the bin folder – Allie Feb 06 '20 at 07:05
  • People who are facing this issue inside docker image( I faced it in logstash 7.16.1), can use RUN cd ../.. && find / -iname keytool to find the correct path of it and change it in Dockerfile. Also, these debugging steps will be applicable to cacert file. – Moksh Dec 16 '21 at 06:30
  • `alias keytool=$JAVA_HOME/bin/keytool` – madebydavid May 04 '22 at 11:49
20

Use

./keytool -genkey -alias mypassword -keyalg RSA
Michael
  • 10,063
  • 18
  • 65
  • 104
11

This worked for me

sudo apt install openjdk-8-jre-headless
Dharman
  • 30,962
  • 25
  • 85
  • 135
JMan
  • 131
  • 1
  • 4
7

Ensure jre is installed.

cd /path/to/jre/bin/folder

As keytool file is present in the bin folder of jre, give path till bin as in the command above.

Then you can do:

keytool -genkey -alias aliaskeyname -keyalg RSA -keystore C:\mykeystore

The additional option -keystore will help you to specify the path where you want the generated self signed certificate.

learner_19
  • 3,851
  • 1
  • 19
  • 8
7

If you are looking for keytool because you are working with Android studio / Google Firebase, there is a keytool bundled with Android Studio. After extracting the zip file, the path to keytool is android-studio/jre/bin.

P.R.
  • 3,785
  • 1
  • 27
  • 47
6

It seems that calling sudo update-alternatives --config java effects keytool. Depending on which version of Java is chosen it changes whether or not keytool is on the path. I had to chose the open JDK instead of Oracle's JDK to not get bash: /usr/bin/keytool: No such file or directory.

AldaronLau
  • 1,044
  • 11
  • 15
6

find your jre location ::sudo find / -name jre And then :: sudo update-alternatives --install /usr/bin/keytool keytool /opt/jdk/<jdk.verson>/jre/bin/keytool 100

MSR SHAHEEN
  • 148
  • 1
  • 5
3

Keytool comes with your Java library. So you have to execute the Keytool command from your /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/bin directory. Or you can add JAVA_HOME to your environmental variables (Windows) or ~/.bash_profile (Linux)

Gene
  • 10,819
  • 1
  • 66
  • 58
2

You tried:

sudo apt-get install oracle-java6-installer --reinstall

and:

sudo update-alternatives --config keytool
Hpsaturn
  • 2,702
  • 31
  • 38
1

You could also put this on one line like so:

/path/to/jre/bin/keytool -genkey -alias [mypassword] -keyalg [RSA]

Wanted to include this as a comment on piet.t answer but I don't have enough rep to comment.

See the "signing" section of this article that describes how to access the keytool.exe without changing your working directory to the path: https://flutter.dev/docs/deployment/android#signing-the-app

Note that they say you can type in space separated folder names like /"Program Files"/ with quotes but I found in bash i had to separate with back slashes like /Program\ Files/.

AaronHolland
  • 1,595
  • 1
  • 16
  • 32
Robert Silver
  • 72
  • 1
  • 1
  • 5
1

If the jre is installed on your machine properly then look for keytool in jre or in jre/bin

  1. to find where jre is installed, use this

    sudo find / -name jre

  2. Then look for keytool in path_to_jre or in path_to_jre/bin

  3. cd to keytool location

  4. then run ./keytool

  5. Make sure to add the the path to $PATH by

    export PATH=$PATH:location_to_keytool

  6. To make sure you got it right after this, run

    where keytool

  7. for future edit you bash or zshrc file and source it

vikas
  • 61
  • 1
1

If you are not using openjdk, use the below commands to set your keytool.

sudo update-alternatives --install "/usr/bin/keytool" "keytool" "/usr/lib/jvm/java8/jdk1.8.0_251/bin/keytool" 1

AND

sudo update-alternatives --set keytool /usr/lib/jvm/java8/jdk1.8.0_251/bin/keytool

This worked for me!

VicXj
  • 165
  • 1
  • 6
0

Please follow the steps:

  1. first set the domain using setDomain.sh command go to domain/bin location and execute ./setDomain.sh command

  2. go to java/bin folder and execute keytool command.

keytool -genkey -keyalg RSA -kaysize 2048 -alias name -kaystore file.jks

MLavoie
  • 9,671
  • 41
  • 36
  • 56
shyam k
  • 1
  • 1