【Linux git bitbucket ssh】AWS上的EC2,對 bitbucket 做 git clone 全紀錄


動機


AWS EC2 , git clone ssh設定全紀錄
等等就要跨年了,然後我在這邊弄該死的git,弄他媽該死的CICD
越來越宅了...好想看煙火喔
不過也只有在家裡有機械式鍵盤+暖氣+大螢幕+音樂+人體工學椅才有心情工作
104給的工作環境真的太糟糕了...就是一個根本不珍惜工程師的公司嘛...
不管如何,紀錄一下剛剛發生什麼事吧。

事發


首先登入先做一個git clone試試看
[ec2-user@ip-10-128-110-42 tmp]$ git clone git@bitbucket.org:ooo/xxx.git
不用講一定會失敗XD,畢竟我們什麼都沒有設定哈哈哈哈

過程


創造ssk-key


首先先去看看文件吧,Use the SSH protocol with Bitbucket Cloud
然後他叫我們先做設定檔,照著抄就對了
喔對我是AWS ec2 linux,我沒有錢買MAC。AWS帳號也用公司的,窮,幹
[ec2-user@ip-10-128-110-42 ~]$ sh-keygen -f ~/.ssh/personalid -C "personalid
在這邊我只是要做個測試,所以就什麼都不改了

檢查檔案


[ec2-user@ip-10-128-110-42 .ssh]$ ll
total 28
-rw------- 1 ec2-user ec2-user  404 Dec 19 02:50 authorized_keys
-rw-r--r-- 1 ec2-user ec2-user  884 Dec 30 05:21 known_hosts
-rw------- 1 ec2-user ec2-user 1679 Dec 31 13:58 personalid
-rw-r--r-- 1 ec2-user ec2-user  392 Dec 31 13:58 personalid.pub

創造設定檔


[ec2-user@ip-10-128-110-42 .ssh]$ vim  ~/.ssh/config
填入
Host personalid
 HostName bitbucket.org
 IdentityFile ~/.ssh/personalid
一定要有一個空白喔!我他媽的在這邊卡了好久。
阿如果發現為什麼貼上不能用了去看我前一篇。【Linux vim AWS】關閉 vim 他媽的 visual mode

把public key貼去bitbucket


[ec2-user@ip-10-128-110-42 .ssh]$ cat  ~/.ssh/personalid.pub 
ssh-rsa AAAA布拉布拉布拉YZO5Mnz personalid
不要多複製到空白,會死
然後去bitbucket貼在你的帳號下面
這裡是官方的步驟
From Bitbucket Cloud, choose avatar > Bitbucket settings from the application menu. 
The system displays the Account settings page.
Click SSH keys.
The SSH Keys page displays. If you have any existing keys, those appear on this page.
Back in your terminal window, copy the contents of your public key file.
For example, in Linux you can cat the contents.
$ cat ~/.ssh/id_rsa.pub
In Mac OSX, the following command copies the output to the clipboard:
$ pbcopy < ~/.ssh/id_rsa.pub
Back in your browser, enter a Label for your new key, for example, Default public key.
Paste the copied public key into the SSH Key field:
Press Add key.
The system adds the key to your account. Bitbucket sends you an email to confirm addition of the key. 
哈哈哈哈我討厭英文,大概就是
點頭像,按帳號設定,點左邊ssh keys,按add key,貼上剛剛那陀public key,按確定

然後再去撞牆 Bad owner or permissions on /home/ec2-user/.ssh/config


[ec2-user@ip-10-128-110-42 tmp]$ git clone git@bitbucket.org:ooo/xxx.git
Cloning into 'xxx'...
Bad owner or permissions on /home/ec2-user/.ssh/config
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
恩......發生什麼事?

改變權限


占卜過後發現檔案權限有點怪,重新設定一次
[ec2-user@ip-10-128-110-42 tmp]$ chmod 600 ~/.ssh/config

然後再去撞牆 Permission denied (publickey).


[ec2-user@ip-10-128-110-42 tmp]$ git clone ec2-user@bitbucket.org:ooo/xxx.git
Cloning into 'xxx'...
Warning: Permanently added the RSA host key for IP address '104.192.143.3' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

設定ssh_config


喔操....到底發生什麼事情了
這下占卜不下去了,我又不是星座專家
下指令看看吧
[ec2-user@ip-10-128-110-42 tmp]$ ssh -vT git@bitbucker.org
OpenSSH_6.6.1, OpenSSL 1.0.1k-fips 8 Jan 2015
debug1: Reading configuration data /home/ec2-user/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: Connecting to bitbucker.org [103.224.182.244] port 22.
debug1: connect to address 103.224.182.244 port 22: Connection refused
ssh: connect to host bitbucker.org port 22: Connection refused
咦?什麼東西被讀了

debug1: Reading configuration data /etc/ssh/ssh_config


看來微薄的知識還是有點用處
修改他吧,下面這兩個設定檔改一下
   ForwardAgent yes
   StrictHostKeyChecking no
接著reload他
[ec2-user@ip-10-128-110-42 tmp]$ sudo service sshd reload
Reloading sshd:                                            [  OK  ]

讀入設定檔


忘記寫一個步驟,有卡住自己看著辦 XDD
不負責任的亂補充
[ec2-user@ip-10-128-110-42 tmp]$ eval "$(ssh-agent -s)"
Agent pid 9281    
[ec2-user@ip-10-128-110-42 tmp]$ ssh-add ~/.ssh/personalid
Identity added: /home/ec2-user/.ssh/personalid (/home/ec2-user/.ssh/personalid)
別打我 QQ
在嘗試驗證一次
[ec2-user@ip-10-128-110-42 tmp]$ ssh-add -l
2048 92:d0:32:35:3c:0a:a7:98:37:7f:b2:bf:ef:0f:8f:79 /home/ec2-user/.ssh/personalid (RSA)
很好,完美

在占卜一次


怎麼說當然再打一次看看
[ec2-user@ip-10-128-110-42 tmp]$ ssh -Tv git@bitbucket.org
[ec2-user@ip-10-128-110-42 tmp]$ ssh -Tv git@bitbucket.org
OpenSSH_6.6.1, OpenSSL 1.0.1k-fips 8 Jan 2015
debug1: Reading configuration data /home/ec2-user/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: Connecting to bitbucket.org [104.192.143.1] port 22.
debug1: Connection established.
debug1: identity file /home/ec2-user/.ssh/id_rsa type -1

中略...反正你也不會看

debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
logged in as g23988.

You can use git or hg to connect to Bitbucket. Shell access is disabled.
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3328, received 1728 bytes, in 1.0 seconds
Bytes per second: sent 3169.9, received 1645.9
debug1: Exit status 0
看到exit code 是 0 就爽,其他都不要管了

驗證


來吧,讓我們愉快的git clone吧。
[ec2-user@ip-10-128-110-42 tmp]$ git clone git@bitbucket.org:ooo/xxx.git
Cloning into 'xxx'...
remote: Counting objects: 110, done.
remote: Compressing objects: 100% (86/86), done.
remote: Total 110 (delta 7), reused 110 (delta 7)
Receiving objects: 100% (110/110), 204.38 KiB | 284.00 KiB/s, done.
Resolving deltas: 100% (7/7), done.
Checking connectivity... done.
然後祝福天下情侶他媽的都分手
去參加101跨年晚會的人都得感冒
哇哈哈哈哈哈
在家工作好悲情阿哇哈哈哈哈哈

2016 真他媽爛年啊,好想換工作啊啊啊啊啊啊


參考


bitbucket官方教學
bitbucket官方交你在linux上設定
Bad owner or permissions on ~/.ssh/config
Permission denied (publickey).
connect to host bitbucket.org port 22: Connection refused fatal:
bitbucket官方教學Troubleshoot SSH Issues
ssh-wont-connect-after-asking-about-host-authenticity

留言