Skip to content

Instantly share code, notes, and snippets.

@Chengings
Last active July 26, 2022 19:19
Show Gist options
  • Save Chengings/6e2d8d3b0f3d7851e55183426b344859 to your computer and use it in GitHub Desktop.
Save Chengings/6e2d8d3b0f3d7851e55183426b344859 to your computer and use it in GitHub Desktop.

Generate an RSA 4096 bit key. Use option "-b 2048" to create 2048 bit

USAGE=''
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_"$USAGE"_"$(date +%Y-%m-%d)" -C "$(whoami)@$(hostname)_$(date +%Y-%m-%d)" 

Generate an ed25519 key with the new OpenSSH format rather than the PEM format (-o) options:

USAGE=''
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_"$USAGE"_$(date +%Y-%m-%d) -C "$(whoami)@$(hostname)_$(date +%Y-%m-%d)"

Set correct permission for keys

chmod 600 ~/.ssh/<private_key_file>
chmod 644 ~/.ssh/<public_key_file>

Add the key to the ssh-agent. An SSH agent is a program which caches your decrypted private keys and provides them to SSH client programs on your behalf.

eval "$(ssh-agent)"
ssh-add --apple-use-keychain ~/.ssh/<private_key_file> # Omit '--apple-use-keychain' for non-apple os.

Copy key to server

ssh-copy-id -i ~/.ssh/<private_key_file> user@server
pbcopy < ~/.ssh/<public_key_file>
cat ~/.ssh/<public_key_file>

(macOS only) So that your computer remembers your password each time it restarts, open (or create) the ~/.ssh/config file and add these lines to the file:

Host *
  AddKeysToAgent yes
  UseKeychain yes

Use a passphrase when possible

Generally all keys used for interactive access should have a passphrase.

Keys without a passphrase are useful for fully automated processes. They allow shell scripts, programs, and management tools to log into servers unattended. This is often used for backups and data transfers between information systems.

References

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