Skip to content

Instantly share code, notes, and snippets.

@kevin-lee
Last active October 26, 2023 12:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevin-lee/2da2cbc6a54ef88386f4 to your computer and use it in GitHub Desktop.
Save kevin-lee/2da2cbc6a54ef88386f4 to your computer and use it in GitHub Desktop.
SSH Keygen

This script currently works on Mac OSX only.

Prerequisite

Run the following command to create ~/.ssh folder if it doesn't already exist and set the right access permission.

{ { { mkdir ~/.ssh && echo "No ~/.ssh found so just created" } || { echo "~/.ssh already exists"; false }  } ; chmod 700 ~/.ssh }

Once done, please check if you have the folder with the right access permission.

ls -ld ~/.ssh

should show you something like

drwx------  14 username  group  448 27 Sep 15:19 /Users/username/.ssh

Check it has rwx and /Users/YOUR_USERNAME/.ssh.

Get the Script

$ curl -Lo- https://goo.gl/7XCEtt | bash 

The script works well only for Mac OSX (for now). In your ~/.bashrc or ~/.zshrc, add the following alias so that you can easily use it.

alias simple-ssh-keygen='~/path/to/script/simple-ssh-keygen.sh' 

Usage

  • Run the following command
$ simple-ssh-keygen "your.email@address.com" 

# The filename will be id_KEY-TYPE
# e.g.) id_rsa or id_ed25519

Or

$ simple-ssh-keygen "your.email@address.com" "your-private-key-file-name" 

# The filename will be your-private-key-file-name_KEY-TYPE
# e.g.) my-github-key => my-github-key_ed25519 / my-github-key_rsa
  • Then select the key type (ed25519 is recommended, and GitHub supports it but BitBucket doesn't.)
Please select the key type.
[0] ed25519
[1] rsa
[x] Exit
  • Once it's done, your public key's copied to the clipboard which means you can simply paste it into GitHub's public key input filed by Cmd+V.
  • Then add the following lines to ~/.ssh/config (create this file if it doesn't exist yet).
Host hostname-to-be-used
  Hostname the-actual-hostname
  Port port-number (optional)
  IdentityFile your-private-key-file-path

e.g.) If it's GitHub,

Host github.com
  Hostname github.com
  Port 22
  IdentityFile ~/.ssh/your-github-private-key-file-name

e.g.) If it's GitLab

Host gitlab.com
  Hostname gitlab.com
  Port 22
  IdentityFile ~/.ssh/your-gitlab-private-key-file-name

e.g.) If you have multiple accounts on GitHub (one of which is for machine account for instance),

Host github.com
  Hostname github.com
  Port 22
  IdentityFile ~/.ssh/your-github-private-key-file-name

Host github-ci.com
  Hostname github.com
  Port 22
  IdentityFile ~/.ssh/your-github-ci-private-key-file-name

So when you clone a repository as yourself, it is

git clone git@github.com:your-username/repo-name.git

If it's the machine account for CI which is the second one,

git clone git@github-ci.com:your-username/repo-name.git
curl https://gist.githubusercontent.com/Kevin-Lee/2da2cbc6a54ef88386f4/raw/simple-ssh-keygen.sh > simple-ssh-keygen.sh;
chmod u+x simple-ssh-keygen.sh;
#!/bin/bash
THIS_SCRIPT_NAME="$(basename $0)"
NC='\033[0m'
LIGHT_BLUE='\033[1;34m'
LIGHT_GREEN='\033[1;32m'
RED="\033[0;31m"
found_in() {
IS_FOUND="NO"
for item in $2
do
if [[ $item == "$1" ]]
then
IS_FOUND="YES"
break
fi
done
echo $IS_FOUND
}
ask_and_take_answer() {
echo ""
echo -e "$1"
echo ""
read ANSWER
while [[ `found_in "$ANSWER" "$2"` == "NO" ]]
do
echo ""
echo -e "$1"
read ANSWER
done
}
DEFAULT_KEY_TYPE="ed25519"
if [[ -n "$1" ]]
then
KEY_TYPE_ED="ed25519"
KEY_TYPE_RSA="rsa"
ask_and_take_answer "Please select the key type.\n[0] $KEY_TYPE_ED\n[1] $KEY_TYPE_RSA\n[x] Exit" "0 1 x"
if [[ $ANSWER == "0" ]]
then
KEY_TYPE=$KEY_TYPE_ED
KEY_TYPE_OPTION="-t $KEY_TYPE_ED -a 128"
elif [[ $ANSWER == "1" ]]
then
KEY_TYPE=$KEY_TYPE_RSA
KEY_TYPE_OPTION="-t $KEY_TYPE_RSA -b 4096"
else
echo ""
exit
fi
FILE_NAME="id_$KEY_TYPE"
if [[ -n "$2" ]]
then
FILE_NAME="$2_${KEY_TYPE}"
else
printf "\n${RED}No key file name is given so the filename will be '${LIGHT_GREEN}$FILE_NAME${RED}'.${NC}\n"
fi
SSH_HOME="$HOME/.ssh"
if [ ! -d "$SSH_HOME" ]; then
printf "\n${LIGHT_GREEN}$SSH_HOME directory does not exist so it will be created.${NC}\n"
mkdir "$SSH_HOME"
chmod 700 "$SSH_HOME"
fi
EMAIL_ADDRESS="$1"
KEY_FILE="$SSH_HOME/$FILE_NAME"
printf "\n${LIGHT_GREEN}Generating SSH Key!${NC}\n"
printf "key type: ${LIGHT_BLUE}${KEY_TYPE}${NC}\n"
printf " email: ${LIGHT_BLUE}$EMAIL_ADDRESS${NC}\n"
printf " file: ${LIGHT_BLUE}$KEY_FILE${NC}\n"
echo ""
ssh-keygen $KEY_TYPE_OPTION -C "$EMAIL_ADDRESS" -f "${KEY_FILE}" || { printf "\n\n${RED}Key generation failed.${NC}\n\n"; exit; }
echo ""
echo "Copying the public key..."
printf "${LIGHT_GREEN}pbcopy${NC} < ${LIGHT_BLUE}${KEY_FILE}.pub${NC}\n"
echo ""
pbcopy < "${KEY_FILE}.pub"
echo "Done: Your public key has been copied to the clipboard so now you can just Cmd+V!"
echo ""
else
printf "\n${RED}Please enter your email address and the file name (optional).${NC}\n"
echo "e.g.)"
echo "$THIS_SCRIPT_NAME your.email@address.com your_key_file_name "
echo ""
echo "Or"
echo ""
echo "$THIS_SCRIPT_NAME your.email@address.com "
echo ""
fi
@kevin-lee
Copy link
Author

Please read this setup guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment