Skip to content

Instantly share code, notes, and snippets.

@ailequal
Last active June 12, 2023 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ailequal/b74811385f4047b34ad590d138c9ffcf to your computer and use it in GitHub Desktop.
Save ailequal/b74811385f4047b34ad590d138c9ffcf to your computer and use it in GitHub Desktop.
SSH key setup

linux-ssh preliminary setup

Nothing (usually).

macos-ssh preliminary setup

macOS won't automatically add our keys (especially if protected by a passphrase) to the ssh agent. In order to fix this, we will open Automator and create a new shell script: Automator > Application > Run Shell Script. The content is the following:

# add all keys inside "~/.ssh" into the ssh-agent and discard the output
ssh-add -A 2> /dev/null

Save it, put it inside the Applications folder and set it to auto run when logging in from the system preferences.

generate ssh key

# start the generation process (eg: ailequal_github) and set a passphrase (optionally)
ssh-keygen -t rsa -b 4096 -C "username_service"

# create a config file for ssh
touch ~/.ssh/config

# depending on the key nature and your computer os, fill the config file correctly

# add these settings for github (linux)
Host username_github
  HostName github.com
  User git
  IdentityFile ~/.ssh/username_github_id_rsa
  AddKeysToAgent yes
  IdentitiesOnly yes

# add these settings for github (macos)
Host username_github
  HostName github.com
  User git
  IdentityFile ~/.ssh/username_github_id_rsa
  AddKeysToAgent yes
  UseKeychain yes
  IdentitiesOnly yes

# add these settings for server (linux)
Host username_server
  HostName 192.168.xxx.xxx
  Port 22
  User username
  IdentityFile ~/.ssh/username_server_id_rsa
  AddKeysToAgent yes
  IdentitiesOnly yes

# add these settings for server (macos)
Host username_server
  HostName 192.168.xxx.xxx
  Port 22
  User username
  IdentityFile ~/.ssh/username_server_id_rsa
  AddKeysToAgent yes
  UseKeychain yes
  IdentitiesOnly yes

# add the key to the ssh-agent (usually this step is not needed)
ssh-add -K ~/.ssh/username_service_id_rsa

# copy the public key into the clipboard using the id as reference
xclip -sel clip < ~/.ssh/username_service_id_rsa.pub # linux
pbcopy < ~/.ssh/username_service_id_rsa.pub # macos

# finally paste the public ssh key into the relative server instance
# usually in "~/.ssh/authorized_keys"

# keep in mind that when we will use our key, the passphrase will be required
# which can be stored inside the keychain and automatically retrieved during the user login

# if you are having troubles, double check the following permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub

# test ssh connection
ssh -T username@username_service

migrate ssh key

Simply copy and paste the ".ssh" folder.

cheatsheet

# show all keys added to the ssh-agent
ssh-add -l

# add all keys inside "~/.ssh" into the ssh-agent
ssh-add -A

# add a specific key to the ssh-agent
ssh-add -K ~/.ssh/username_service_id_rsa

# delete all the keys from the ssh-agent
ssh-add -D

# test ssh connection
ssh -T username@username_service

# set global username
git config --global user.name "username"

# set global email address
git config --global user.email "email"

# set global gitignore
git config --global core.excludesfile ~/.gitignore_global

# echo global username
git config --global user.email

# set local parameter for a single repository
git config user.name "Alan Turing"

# include local .gitconfig only into the current repository
git config --local include.path ../.gitconfig

extra

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