Extract .key and .crt files from JKS file

JKS file is a Java keystore. Using the Java keytool program, run the following commands

Export the .der file

keytool -export -alias sample -file sample.der -keystore my.jks

Convert the .der file to unencrypted PEM (crt file)

openssl x509 -inform der -in sample.der -out sample.crt

Export the .p12 file

keytool -importkeystore -srckeystore my.jks -destkeystore keystore.p12 -deststoretype PKCS12

Convert the .p12 file to unencrypted PEM (key file)

openssl pkcs12 -in keystore.p12 -nodes -nocerts -out server.key

 

More info here: http://www.gtopia.org/blog/2010/02/der-vs-crt-vs-cer-vs-pem-certificates/

AWS EC2 Linux swap file creation

The Amazon EC2 Linux instances come without a swap file. The steps below show how to add a swap file to an EC2 Linux server.

dd if=/dev/zero of=/mnt/swapfile bs=1M count=4096
chown root:root /mnt/swapfile
chmod 600 /mnt/swapfile
mkswap /mnt/swapfile
swapon /mnt/swapfile
swapon -a

To make these changes permanent (reboot proof). Add the following line to /etc/fstab

vi /etc/fstab
/mnt/swapfile swap swap defaults 0 0

wget: how to handle urls with ampersands

The url that you want to get with wget looks like this i.e. has & in the query string.

http://hostname/path?account=abc&user=joe

Calling wget like this will not work

wget http://hostname/path?account=abc&user=joe

But the following will work nicely

URL="http://hostname/path?account=abc&user=joe"; wget "$URL"

Linux Tip: Search & Replace text in multiple files recursively

Here is how to find and replace text across multiple files recursively.

The following command will recursively find all files with extension .html starting with the current folder. In each file, the text apple will be replaced with orange.

find . -name "*.html" -print | xargs sed -i 's/apple/orange/g'