How to generate SSH Key pair?

If you do not have ssh key on your machine and you want to generate a new ssh key try following command:

ssh-keygen -t rsa -C "your_email@example.com"

Just press to accept the default location and file name. If the .ssh directory doesn't exist, the system creates one for you.

Enter, and re-enter, a passphrase when prompted.

SSH config file

cat /etc/ssh/ssh_config
cat /etc/ssh/sshd_config

SSH Files Location

cat ~/.ssh/config
cat ~/.ssh/known_hosts
cat ~/.ssh/authorized_keys

SSH key file permission

# private key file permission
chmod 600 ~/.ssh/id_rsa

# Authorized keys file permission    
chmod 644 ~/.ssh/authorized_keys

### SSH Folder Permissions
chmod 700 ~/.ssh
chown -R $USER:$USER ~/.ssh

Adding Passphrase

If you were given an unencrypted SSH key, such as from AWS, then you probably want to add a passphrase to it with the following command:

ssh-keygen -p -f private-key.pem

Remove Passphrase

To remove passphrase from existing key and to create a new key from the existing run following command:

openssl rsa -in [OLD_KEY_PATH] -out [NEW_KEY_PATH].new

Generate public key from private key

To generate public key from private key use following command:

ssh-keygen -f [OLD_KEY_PATH] -y > [NEW_KEY_PATH].pub

How to copy ssh key to remote server

# copy all keys
ssh-copy-id user@domain.com

# copy specific key
ssh-copy-id -i [PUBLIC_KEY_PATH] user@domain.com

Convert OpenSSL format to SSH-RSA format

ssh-keygen -f my_ssh.pub -i