Last active
March 3, 2025 15:39
-
-
Save kmassada/81938de78714eb4f9166 to your computer and use it in GitHub Desktop.
Quick setup for ssh, and ssh config file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set the variables | |
SSH_HOST="gitlab.com" | |
SSH_USER=`whoami` | |
SSH_PORT=22 | |
# create folder with the correct permissions | |
mkdir -p ~/.ssh/; | |
chmod 700 ~/.ssh/; | |
touch ~/.ssh/authorized_keys | |
chmod 600 ~/.ssh/authorized_keys | |
### --- Create Key Pair --- ### | |
# generate key with u2f/fido | |
ssh-keygen -t ecdsa-sk -f ~/.ssh/$SSH_USER@$SSH_HOST -b 4096 -P '' -f ~/.ssh/$SSH_USER@$SSH_HOST | |
# OR without | |
ssh-keygen -t rsa -f ~/.ssh/$SSH_USER@$SSH_HOST -b 4096 -P '' | |
### --- Import Key Pair (Previously Exported) --- ### | |
# SECRET_NAME was previously set | |
SECRET_NAME=`echo $SSH_HOST | tr . -` | |
gcloud secrets versions access latest \ | |
--secret="$SECRET_NAME" > ~/.ssh/$SSH_USER@$SSH_HOST | |
# set the permissions | |
chmod 600 ~/.ssh/$SSH_USER@$SSH_HOST | |
### --- Setup Config for SSH files --- ### | |
## cat into config | |
cat >> ~/.ssh/config << EOF | |
Host $SSH_HOST | |
HostName $SSH_HOST | |
User $SSH_USER | |
IdentityFile ~/.ssh/$SSH_USER@$SSH_HOST | |
Port $SSH_PORT | |
EOF | |
# set the permissions | |
chmod 600 ~/.ssh/config; | |
### --- Export Key: Copy to HOST --- ### | |
# copy to instance | |
ssh-copy-id -p $SSH_PORT -i ~/.ssh/$SSH_USER@$SSH_HOST.pub $SSH_USER@$SSH_HOST | |
### --- Export Key: Copy to clipboard --- ### | |
# ubuntu to clipboard | |
cat ~/.ssh/$SSH_USER@$SSH_HOST.pub | xclip | |
# macos to clipboard | |
cat ~/.ssh/$SSH_USER@$SSH_HOST.pub | pbcopy | |
### --- Export Key: Copy to GCP Secret --- ### | |
SECRET_NAME=`echo $SSH_HOST | tr . -` | |
gcloud secrets create $SECRET_NAME \ | |
--replication-policy="automatic" \ | |
--data-file=~/.ssh/$SSH_USER@$SSH_HOST | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment