###### tags: `github` `ssh` `linux`
# Github SSH Key (make linux easy to push github rapo.)
> This page is edited from [Auther: 辛西 (Cynthia)【Git】使用 SSH 金鑰與 GitHub 連線 ](https://cynthiachuang.github.io/Generating-a-Ssh-Key-and-Adding-It-to-the-Github/)
1. Make a local ssh dir in linux
```shell
mkdir ~/.ssh
chmod 700 ~/.ssh
```
2. Use Git RSA Generator to make rsa ssh key (private & public keys)
```shell
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
```
* You will be ask some question:
1. :::info
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.:
:::success
github_rsa_key
:::
2. :::info
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.:
:::success
password
:::
3. :::info
Enter same passphrase again:
:::
Input the same thing as last answer.
:::success
password
:::
3. Then you will get your fingerprint and SHA256 token
:::info
The key fingerprint is:
SHA256: \<sha-token\> your_email@example.com
The key's randomart image is:
+---[RSA 4096]----+
| ... ... ... |
| ... ... ... |
| ... ... ... |
| ... ... ... |
| ... ... ... |
| ... ... ... |
| ... ... ... |
| ... ... ... |
| ... ... ... |
+----[SHA256]-----+
:::
4. Check and copy your the public key
```shell
cd ~/.ssh && ls
```
:::info
github_rsa_key github_rsa_key.pub
:::
```shell
cat ~/.ssh/github_rsa_key.pub
```
:::info
ssh-rsa ABC....89xyz== your_email@example.com
:::
> If you want to copy with command line: [xclip](https://hackmd.io/kSwf6CSkRHeAl1Eu1Jj2WQ#%E5%89%AA%E8%B2%BC%E7%B0%BF%E5%A5%97%E4%BB%B6-xclip)
5. Add a public key to your remote repository
https://github.com/ $\rightarrow$ `Settings` $\rightarrow$ Access $\rightarrow$ `SSH and GPG keys` $\rightarrow$ SSH keys $\rightarrow$ `New SSH keys`
* Name a good title
E.g.:
:::success
MyLabDesktopUbuntu2004
:::
* paste the public key
:::success
ssh-rsa ABC....89xyz== your_email@example.com
:::
6. Test it
Just paste the command below:
```SHELL
ssh -T git@github.com
```
You should see:
:::info
ECDSA key fingerprint is SHA256:\*\*\*\*\*\*\*\*\*\*\*\*
Are you sure you want to continue connecting (yes/no/\[fingerprint\])?
:::
input `yes` then:
:::info
Warning: Permanently added 'github.com,52.69.186.44' (ECDSA) to the list of known hosts.
:::
:::info
Hi XXX! You've successfully authenticated, but GitHub does not provide shell access.
:::
Means successfully finish.
* Troubleshooting: https://cynthiachuang.github.io/Generating-a-Ssh-Key-and-Adding-It-to-the-Github/
7. ==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:
```shell
git remote -v
```
You'll see:
:::info
origin https://github.com/YourGithubUserName/YourGithubRepo.git (fetch)
origin https://github.com/YourGithubUserName/YourGithubRepo.git (push)
:::
**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
```shell
git remote set-url origin git@github.com:YourGithubUserName/YourGithubRepo.git
```
* Change these two strings in the command:
* YourGithubUserName
* YourGithubRepo
To see the change:
```shell
git remote -v
```
You should see:
:::info
origin git@github.com:YourGithubUserName/YourGithubRepo.git (fetch)
origin git@github.com:YourGithubUserName/YourGithubRepo.git (push)
:::
8. That's all. You can push your commit to github without input the personal access token.
==**NOTICE!!**==
:::danger
**This article continues [Cynthia's blog](https://cynthiachuang.github.io/Generating-a-Ssh-Key-and-Adding-It-to-the-Github/) under the [CC BY-NC-SA 4.0 license](https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en). Please cite author, link and source for republication!**
:::
## normal use ([ref.](https://medium.com/feveral%E7%9A%84%E7%A8%8B%E5%BC%8F%E7%AD%86%E8%A8%98/%E7%94%A8%E6%86%91%E8%AD%89%E8%AE%93ssh%E9%81%A0%E7%AB%AF%E9%80%A3%E7%B7%9A%E4%B8%8D%E7%94%A8%E6%89%93%E5%AF%86%E7%A2%BC-3d187cd9f354))
```
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