Set up SSH Login With Private Key

On Windows

Requirement:

  • Install OpenSSH. Please follow the instruction of this page

  • Change the directory to the main directory(EX: C:\Users\{USERNAME})

Step1: Generate SSH Key Pair

1
ssh-keygen -t rsa -f ".\.ssh\{FILE NAME OF KEY}"

Step2: Create SSH Folder

1
ssh {LOGIN USERNAME ON THE TARGET HOST}@{TARGET HOST NAME/IP} mkdir -p .ssh

Step3: Copy SSH Public Key To The Remote Host

1
cat ".\.ssh\{FILE NAME OF KEY}.pub" | ssh {LOGIN USERNAME ON THE TARGET HOST}@{TARGET HOST NAME/IP} 'cat >> .ssh/authorized_keys'

Step4: Login To The Remote Host With SSH Key

1
ssh -i ".\.ssh\{FILE NAME OF KEY}" {LOGIN USERNAME ON THE TARGET HOST}@{TARGET HOST NAME/IP}

VSCode Config

After finishing the above instruction, type the login SSH command ssh -i ".\.ssh\{FILE NAME OF KEY}" {LOGIN USERNAME ON THE TARGET HOST}@{TARGET HOST NAME/IP} to the VSCode and VSCode will generate the following configuration automatically.

Host {CUSTOMIZED HOST NAME}
  HostName {RAMOTE HOST NAME/IP}
  IdentityFile .\.ssh\{FILE NAME OF KEY}
  User {USERNAME}

If you specify the port with option -p, there will be one more line Port {LOGIN PORT} in the configuration.

Host {CUSTOMIZED HOST NAME}
  HostName {RAMOTE HOST NAME/IP}
  IdentityFile .\.ssh\{FILE NAME OF KEY}
  User {USERNAME}
  Port {LOGIN PORT}

Linux

Step1: Generate SSH Key Pair

1
ssh-keygen -t rsa -f ~/.ssh/{FILE NAME OF KEY}

Step2: Create SSH Folder

1
ssh {LOGIN USERNAME ON THE TARGET HOST}@{TARGET HOST NAME/IP} mkdir -p .ssh

Step3: Copy SSH Public Key To The Remote Host

1
ssh-copy-id -i ~/.ssh/{FILE NAME OF KEY} {LOGIN USERNAME ON THE TARGET HOST}@{TARGET HOST NAME/IP}

Step4: Login To The Remote Host With SSH Key

1
ssh -i "~/.ssh/{FILE NAME OF KEY}" {LOGIN USERNAME ON THE TARGET HOST}@{TARGET HOST NAME/IP}

Use The Proxy(Linux As Proxy/Jump Server)

Step1: Create SSH Private Key Login On The Proxy/Jump Server

On remote proxy/jump server

1
2
3
4
ssh-keygen -t rsa -f ~/.ssh/{FILE NAME OF KEY}
ssh {LOGIN USERNAME ON THE TARGET HOST}@{TARGET HOST NAME/IP} mkdir -p .ssh
ssh-copy-id -i ~/.ssh/{FILE NAME OF KEY} {LOGIN USERNAME ON THE TARGET HOST}@{TARGET HOST NAME/IP}
ssh -i "~/.ssh/{FILE NAME OF KEY}" {LOGIN USERNAME ON THE TARGET HOST}@{TARGET HOST NAME/IP}

Step2: Copy The Private SSH Key To The Local Host

Then, on local host(windows). Copy the private key to the local host

1
scp {PROXY SERVER USERNAME}@{PROXY SERVER IP}:/home/{PROXY SERVER USERNAME}/.ssh/{FILE NAME OF KEY} .\.ssh\

Step3: Connect To Remote Target Server

Connect to remote target server with windows local host.

1
ssh -o ProxyCommand="C:\Windows\System32\OpenSSH\ssh.exe -q -W %h:%p {PROXY SERVER IP}"  {LOGIN USERNAME ON THE TARGET HOST}@{TARGET HOST NAME/IP} -i ".\.ssh\{FILE NAME OF KEY}"

VSCode Config

Again, if you type the login SSH command to the VSCode, it will generate the following configuration automatically.

Host {CUSTOMIZED HOST NAME}
  HostName {RAMOTE TARGET HOST NAME/IP}
  ProxyCommand C:\Windows\System32\OpenSSH\ssh.exe -q -W %h:%p {PROXY SERVER IP}
  User {LOGIN USERNAME ON THE TARGET HOST}
  IdentityFile .\.ssh\{FILE NAME OF KEY}

Something Important

Since Window seperate the directory with \ but Linux /, all the paths on Window should add " " across the path.

Set up Github SSH Key

Step1: Generate SSH key

Type command

1
ssh-keygen

If you want to specify some features, you can use the following options

  • -t: Specify cryptosystem
  • -b: Specify the number of bits of the key
  • -C: Specify the comment
1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Then, follow the guide to complete the setting.

1
2
3
4
Generating public/private rsa key pair.
Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter to save the key in 'is_rsa' or specify other files]
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

Step2: Store Keys Into SSH Agent

Launch the SSH agent in the background.

1
eval "$(ssh-agent -s)"

Add keys into the SSH agent.

1
ssh-add -k ~/.ssh/id_rsa

If you don’t save the key in the default file ~/.ssh/id_rsa, please replace the path ~/.ssh/id_rsa with the the custom file.

Step3: Copy Public Key To Create Github SSH Keys

Show the public key

1
cat ~/.ssh/id_rsa.pub

The you may see your public key

1
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCeq5RobPZFGcoX+SIAHNghDNp1YttnANhj6gPiKwa9TN47gYmQaPZoFJJXBa5eJpLjzR8hif+4CPuqD1+xeKzQCTQ63Bg911kGHHW3RNo7PFo86hSh9yaYhGE7dD/oYsixqJnbe/ytk0SkwE8qOVkxg9o/c0S0bJOvbMr0hHNt6O8OPWFFsnFHZaY27xJv1NSjn7Q+P93sNxitviQQcYRlK8t5tbWKuF7O8WTCUz6al1iJ5SvX08BRO5TqH0lqGEkY34Lr1M2iBe1Km/ev7fZWPMs3RMSy192lDRcrBcaNF8Kgji2CxQ++GSsZ8usIUjbcywjuDS1rj3XGmi3f56/l your_email@example.com

Copy the content and paste it to the Github SSH keys.

Then, click Add SSH Key to add the SSH key.

Step4: Test SSH Keys

1
ssh -T git@github.com

If you see the following message, that means you’ve added the SSH keys to Github successfully.

1
Hi FrankCCCCC! You've successfully authenticated, but GitHub does not provide shell access.

Reference