Skip to content

Instantly share code, notes, and snippets.

@PunGrumpy
Forked from developius/README.md
Last active July 20, 2023 13:11
Show Gist options
  • Save PunGrumpy/d0e2557b5e3aa3251987b37b9c333ac8 to your computer and use it in GitHub Desktop.
Save PunGrumpy/d0e2557b5e3aa3251987b37b9c333ac8 to your computer and use it in GitHub Desktop.
Setup SSH keys for use with GitHub/GitLab/BitBucket etc

πŸ”‘ SSH Keypair Setup for GitHub (or GitLab/BitBucket, etc)

🏞️ Create the Repository

Make sure there is at least one file in it (e.g., README.md).

πŸ—οΈ Generate an SSH Key Pair (private/public)

Generate a new SSH key using RSA:

ssh-keygen -t rsa -C "[email protected]"

Generate a new SSH key using RSA with even better security:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

Generate a new SSH key using ED25519:

ssh-keygen -t ed25519 -C "[email protected]"

βš™οΈ Create SSH Config

Bash or MacOS:

touch ~/.ssh/config

Windows:

type nul > ~/.ssh/config

Add the following content to the config file:

Host *
  AddKeysToAgent yes
  IdentityFile ~/.ssh/[key_generate_file]

or you can specific host

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/[key_generate_file]

πŸ“‹ Copy the Contents of the Public SSH Key

Copy the contents of the file ~/.ssh/id_rsa.pub to your SSH keys in your GitHub account settings (https://github.com/settings/keys).

πŸš€ Copy the Public SSH Key to GitHub

Paste the contents of your SSH public key into your GitHub account settings (https://github.com/settings/keys).

πŸ§ͺ Test SSH Key

Run the following command to test your SSH key setup:

result:

Hi [Your GitHub Name]! You've successfully authenticated, but GitHub does not provide shell access.

πŸ”„ Update Repository Remote URL

Change directory into the local clone of your repository (if you're not already there) and run:

git remote set-url origin [email protected]:username/your-repository.git

Now try editing a file (e.g., README) and then do:

git add .
git commit -m "Update README.md"
git push

If it works without asking for a username or password, your SSH key is correctly configured. βœ…

πŸ“ƒ Script for create SSH Key Pair

wget https://gist.github.com/PunGrumpy/d0e2557b5e3aa3251987b37b9c333ac8/raw/2488cc971b83e693cd209f628ff6e4a3b6d3efc9/generate_ssh_key_pair.sh -O generate_ssh_key_pair.sh
chmod +x generate_ssh_key_pair.sh
./generate_ssh_key_pair.sh

I hope you find these changes helpful! Let me know if you have any further questions or suggestions. Happy coding! 🌟

#!/bin/bash
echo "πŸ” SSH Keypair Setup πŸš€"
echo "Enter your email: "
read email
echo "Enter your host name: "
read hostname
echo "Enter your user name (default git): "
read username
echo "Enter your key type (rsa, ed25519): "
read keytype
echo "Enter your key size (default 4096): "
read keysize
echo "Enter your key comment: "
read keycomment
echo "Enter your key passphrase (default empty): "
read keypassphrase
echo "Enter your key file name (default id_rsa): "
read keyfilename
echo "Enter your key file path (default ~/.ssh): "
read keyfilepath
echo "Enter your config file path (default ~/.ssh/config): "
read configfilepath
if [ -z "$username" ]
then
username="git"
fi
if [ -z "$keytype" ]
then
keytype="rsa"
fi
if [ -z "$keysize" ]
then
keysize=4096
fi
if [ -z "$keypassphrase" ]
then
keypassphrase=""
fi
if [ -z "$keyfilename" ]
then
keyfilename="id_rsa"
fi
if [ -z "$keyfilepath" ]
then
keyfilepath="~/.ssh"
fi
if [ -z "$configfilepath" ]
then
configfilepath="~/.ssh/config"
fi
ssh-keygen -t "$keytype" -b "$keysize" -C "$keycomment" -f "$keyfilepath/$keyfilename" -N "$keypassphrase"
touch "$configfilepath"
if [ -z "$hostname" ]
then
echo "Host *" >> "$configfilepath"
else
echo "Host $hostname" >> "$configfilepath"
fi
echo " AddKeysToAgent yes" >> "$configfilepath"
echo " IdentityFile $keyfilepath/$keyfilename" >> "$configfilepath"
echo " User $username" >> "$configfilepath"
echo "Your SSH Key Pair is created successfully! πŸŽ‰"
echo "Your SSH Key Pair is located at $keyfilepath/$keyfilename"
echo "Your SSH Config is located at $configfilepath"
echo "Your SSH Public Key is copied to your clipboard"
if [ -x "$(command -v pbcopy)" ]
then
pbcopy < "$keyfilepath/$keyfilename.pub"
else
cat "$keyfilepath/$keyfilename.pub"
fi
echo "Now you can add the public key to your server or services. Happy coding! πŸš€"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment