Skip to content

SSH

Basic actions

Connect to a server through a Bastion:

ssh -t $BASTION_SSH_HOST ssh $FINAL_SSH_HOST

Connect using a specific private key:

ssh -i $PATH_TO_THE_PRIVATE_KEY $USER@$SSH_HOST

Skipping the known host check:

sh -o StrictHostKeyChecking=no $USER@$SSH_HOST

Executing cmds via SSH:

ssh -t $USER@$SSH_HOST $CMD

SSH Keys

Generation

Generate new SSH Keys:

ssh-keygen -t rsa -C "$UNIQUE_IDENTIFIER"

Press Enter to accept default location and then type your paraphrase twice.

2 new files were created in the ~/.ssh folder, the id_rsa and id_rsa.pub ones.

Setting the rights file permissions

Solves WARNING: UNPROTECTED PRIVATE KEY FILE!:

sudo chmod 600 $KEY_PATH

Add local public key to a remote server

  1. Display the local public key: cat ~/.ssh/id_rsa.pub
  2. Connect to the remote server
  3. Ad the local public key to the remote server authorised keys by editing the ~/.ssh/authorized_keys file

Adding a externally generated key to your ssh-agent

Start the agent and add the key:

eval $(ssh-agent)
ssh-add ~/.ssh/$KEY_PATH