Try   HackMD
tags: github ssh linux

Github SSH Key (make linux easy to push github rapo.)

This page is edited from Auther: 辛西 (Cynthia)【Git】使用 SSH 金鑰與 GitHub 連線

  1. Make a local ssh dir in linux
mkdir ~/.ssh 
chmod 700 ~/.ssh
  1. Use Git RSA Generator to make rsa ssh key (private & public keys)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • You will be ask some question:
    1. Enter file in which to save the key (/home/kuihao-ubuntu/.ssh/id_rsa):

      Give these rsa keys file a name. You can pass by enter directly. Or input a good name.
      E.g.:

      github_rsa_key

    2. Enter passphrase (empty for no passphrase):

      The second and third questions ask whether to specify the key protection password. If you set the password, you have to enter the password every time you use the key afterwards. Please keep this in mind.
      You can pass by enter directly. Or input a good password.
      E.g.:

      password

    3. Enter same passphrase again:

      Input the same thing as last answer.

      password

  1. Then you will get your fingerprint and SHA256 token

    The key fingerprint is:
    SHA256: <sha-token> your_email@example.com
    The key's randomart image is:
    ±[RSA 4096]+
    | |
    | |
    | |
    | |
    | |
    | |
    | |
    | |
    | |
    ±-[SHA256]-+

  2. Check and copy your the public key
    ​​​​cd ~/.ssh && ls
    

    github_rsa_key github_rsa_key.pub

    ​​​​cat ~/.ssh/github_rsa_key.pub 
    

    ssh-rsa ABC89xyz== your_email@example.com

    If you want to copy with command line: xclip

  3. Add a public key to your remote repository
    https://github.com/
    Settings
    Access
    SSH and GPG keys
    SSH keys
    New SSH keys
    • Name a good title
      E.g.:

      MyLabDesktopUbuntu2004

    • paste the public key

      ssh-rsa ABC89xyz== your_email@example.com

  4. Test it
    Just paste the command below:
    ​​​​ssh -T git@github.com
    
    You should see:

    ECDSA key fingerprint is SHA256:************
    Are you sure you want to continue connecting (yes/no/[fingerprint])?

    input yes then:

    Warning: Permanently added 'github.com,52.69.186.44' (ECDSA) to the list of known hosts.

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

    Means successfully finish.
  5. Change the connection protocol
    After completing the key generation and configuration, you must change the connection protocol; otherwise, even if SSH is configured, it will still use the https transmission protocol.
    In the local git dir:
    ​​​​git remote -v
    
    You'll see: Changing Remote Server Repository URLs
    Github uses different server repository URLs for different protocols. So the URL will be changed from https to start with git
    ​​​​git remote set-url origin git@github.com:YourGithubUserName/YourGithubRepo.git
    
    • Change these two strings in the command:
      • YourGithubUserName
      • YourGithubRepo
        To see the change:
    ​​​​git remote -v
    
    You should see:

    origin git@github.com:YourGithubUserName/YourGithubRepo.git (fetch)
    origin git@github.com:YourGithubUserName/YourGithubRepo.git (push)

  6. That's all. You can push your commit to github without input the personal access token.

NOTICE!!

This article continues Cynthia's blog under the CC BY-NC-SA 4.0 license. Please cite author, link and source for republication!

normal use (ref.)

scp ~/.ssh/<your_local_key_name>.pub <remote-user>@140.116.xxx.yyy:~/.ssh/<xyz_key>.pub
  • <xyz_key>.pub 可改成自己想要的檔名.pub

以下命令要在遠端主機:

cat ~/.ssh/<xyz_key>.pub >> ~/.ssh/authorized_keys
sudo chmod 400 ~/.ssh/authorized_keys
sudo service ssh restart

finished