官方说明:https://help.github.com/articles/generating-ssh-keys/

1,为Github账户设置SSH key

文章地址:http://zuyunfei.com/2013/04/10/setup-github-ssh-key/

什么是SSH key

一直使用SSH连接服务器,但是对它的原理却不太了解。这次设置Octopress的时候,需要使用SSH 方式连接Github, 正好对SSH的工作方式做了下了解。(好像Github推荐使用HTTPS的方式访问repo, 以前Github受到过SSH密匙攻击,之后升级了SSH key的安全措施,https方式视乎更方便安全,不过Octopress的设置文档中,我并没有找到怎么使用HTTPS连接Github)

简单来说,SSH提供了两种级别的安全验证:

  1. 第一种级别是基于密码的安全验证,知道账号和密码,就可以登陆到远程主机。Team的开发工作中,就是使用这种方式登陆编译服务器,或者开发机器。因为是在内网中,这种级别的安全验证已经足够了。
  2. 第二种级别是基于Public-key cryptography (公开密匙加密)机制的安全验证,原理如下图所示:

其优点在于无需共享的通用密钥,解密的私钥不发往任何用户。即使公钥在网上被截获,如果没有与其匹配的私钥,也无法解密,所截获的公钥是没有任何用处的。

产生SSH key

根据Github提供的help文档,具体过程如下

1
2
$ cd ~/.ssh
# Checks to see if there is a directory named ".ssh" in your user directory

使用ssh-keygen产生新的key

1
2
3
4
$ ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key using the provided email
Generating public/private rsa key pair.
Enter file in which to save the key (/home/you/.ssh/id_rsa):

使用默认的文件名直接enter, 按提示输入密码(如果不提供密码,SSH将无密码连接,如果private key泄露可能会有安全问题)

1
2
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

密匙产生成功

1
2
3
4
Your identification has been saved in /home/you/.ssh/id_rsa.
Your public key has been saved in /home/you/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com

上传public key到Github账户

  1. 登录github
  2. 点击右上方的Accounting settings图标
  3. 选择 SSH key
  4. 点击 Add SSH key

在出现的界面中填写SSH key的名称,填一个你自己喜欢的名称即可,然后将上面拷贝的~/.ssh/id_rsa.pub文件内容粘帖到key一栏,在点击“add key”按钮就可以了。
添加过程github会提示你输入一次你的github密码

设置SSH使用HTTPS的403端口

在局域网中SSH的22端口可能会被防火墙屏蔽,可以设置SSH使用HTTPS的403端口。

测试HTTPS端口是否可用

1
2
3
$ ssh -T -p 443 git@ssh.github.com
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.

编辑SSH配置文件 ~/.ssh/config 如下:

1
2
3
Host github.com
Hostname ssh.github.com
Port 443

测试是否配置成功

1
2
3
$ ssh -T git@github.com
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.

多个Github账号的SSH key切换

如果在一台机器上要登陆多个Github账户,需要一些配置,虽然现在并没有用到,但是先记下来以备不时之需,过程参看这里

2,【GitHub】解决每次push代码到github都需要输入用户名和密码的方法

在github上,建立一个项目test,去主页查看可以看到

如果使用HTTPS:

Create a new repository on the command line

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/guochy2012/test.git
git push -u origin master

Push an existing repository from the command line

git remote add origin https://github.com/guochy2012/test.git
git push -u origin master

如果采用SSH:

Create a new repository on the command line

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:guochy2012/test.git
git push -u origin master

Push an existing repository from the command line

git remote add origin git@github.com:guochy2012/test.git
git push -u origin master

使用HTTPS需要每次输入密码,SSH则不用,但SSH需要配置密钥 。

关于怎么产生密钥可以参见《Generating SSH Keys》一文

3,github地址 从https改成ssh

打开命令行工具,运行 git remote set-url origin 例如:

 
1
2
3
4
 
$ git remote set-url origin git@github.com:user/repo.git
 
 

然后再次 commit,如果出现类似:

 
1
2
3
4
 
Permission denied (publickey).
 
 

字样,那么说明你的 SSH key 没有设置或已经失效(譬如升级到 Mountain Lion 系统后),请重新参照上文的官方文档进行设置即可。

4,执行pull时报错

wangkongming@AY140527171808170503Z:~/github/collect$ git pull
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/wangkongming/.ssh/id_rsa' are too open.
It is recommended that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: /home/wangkongming/.ssh/id_rsa
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

答案:http://stackoverflow.com/questions/1556119/ssh-private-key-permissions-using-git-gui-or-ssh-keygen-are-too-open

是因为给 id_rsa的权限太高了,改成700就可以了。也有人说600

You changed the permissions on the whole directory, which I agree with Splash is a bad idea. If you can remember what the original permissions for the directory are, I would try to set them back to that and then do the following

cd ~/.ssh
chmod 700 id_rsa

inside the .ssh folder. That will set the id_rsa file to rwx (read, write, execute) for the owner (you) only, and zero access for everyone else.

If you can't remember what the original settings are, add a new user and create a set of SSH keys for that user, thus creating a new .ssh folder which will have default permissions. You can use that new .ssh folder as the reference for permissions to reset your .ssh folder and files to.

If that doesn't work, I would try doing an uninstall of msysgit, deleting ALL .ssh folders on the computer (just for safe measure), then reinstalling msysgit with your desired settings and try starting over completely (though I think you told me you tried this already).

Edited: Also just found this link via Google -- Fixing "WARNING: UNPROTECTED PRIVATE KEY FILE!" on Linux While it's targeted at linux, it might help since we're talking liunx permissions and such.

======================================

以下是自己在使用git时,总结的:

1,查看当前项目远程分支的路径

git remote -v

如何向git账号上提交代码的更多相关文章

  1. git将本地代码 和服务器git@osc 上的代码 关联

    将本地代码 和服务器git@osc 上的代码 关联 要使用git 首先,你得安装一个git 下载 http://git-scm.com/downloads 安装完成后,需要简单的配置一下,打开 Git ...

  2. # .NET Core下操作Git,自动提交代码到

    .NET Core下操作Git,自动提交代码到 转自博客园(阿星Plus) .NET Core 3.0 预览版发布已经好些时日了,博客园也已将其用于生产环境中,可见 .NET Core 日趋成熟 回归 ...

  3. git学习心得之git跨分支提交代码

    最近在工作中遇到了git跨分支提交代码的问题,本地拉的是远程master分支的代码,需要将本地修改代码提交到远程temp分支. 1.在gitlab上对相应项目fork本地分支 2.更新本地代码,将远程 ...

  4. 使用Git Bash上传代码到新的分支

    1.进入想要提交的项目,点击鼠标右键,选择"Git Bash Here" 2.输入命令,查看当前所有分支 git branch -a 3.输入命令,新建分支 git checkou ...

  5. git私有仓库提交代码

    #首次提交 #克隆版本库到本地 git clone http://192.168.3.107:9002/develop/zhong.git cd zhong #创建忽略文件(忽略文件自行编辑) tou ...

  6. git 命令--上传代码

    创建密钥命令: ssh-keygen -C 'your@email.address' -t rsa 找到生成的密钥文件id_rsa.pub 地址:C:\Documents and Settings\A ...

  7. 新手第一次在GitHub上提交代码完整教程

    提交步骤: 1.创建github repository(仓库) 2.安装git客户端 3.为Github账户设置SSH key 4.上传本地项目到github 一.创建github repositor ...

  8. git push上传代码到gitlab上,报错401或403

    之前部署的gitlab代码托管平台,采用ssh方式连接gitlab,在客户机上产生公钥上传到gitlab的SSH-Keys里,则git clone下载和git push上传都没问题,这种方式很安全. ...

  9. [转]使用git命令上传代码

    http://jiajing.elastos.org/2013/04/15/%E4%BD%BF%E7%94%A8git%E5%91%BD%E4%BB%A4%E4%B8%8A%E4%BC%A0%E4%B ...

随机推荐

  1. bzoj 3196 树套树模板

    然而我还是在继续刷水题... 终于解开了区间第k大的心结... 比较裸的线段树套平衡树,比较不好想的是求区间第k大时需要二分一下答案,然后问题就转化为了第一个操作.复杂度nlog3n.跑的比较慢... ...

  2. Leetcode 400. Nth digits

    解法一: 一个几乎纯数学的解法 numbers:   1,...,9, 10, ..., 99, 100, ... 999, 1000 ,..., 9999, ... # of digits:   9 ...

  3. Leetcode Integer Replacement

    Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...

  4. 【BZOJ-2756】奇怪的游戏 最大流 + 分类讨论 + 二分

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2925  Solved: 792[Submit][Stat ...

  5. 【BZOJ-1924】所驼门王的宝藏 Tarjan缩点(+拓扑排序) + 拓扑图DP

    1924: [Sdoi2010]所驼门王的宝藏 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 787  Solved: 318[Submit][Stat ...

  6. java的 clone方法

    1.java语言中没有明确提供指针的概念与用法,而实质上每个new语句返回的都是一个指针的引用,只不过在大部分情况下开发人员不需要关心如果取操作这个指针而已. 2.在java中处理基本数据类型时,都是 ...

  7. ubuntu 通过命令将数据复制到u盘

    自己现在也不好意思和别人说自己熟悉linux了,自己用linux,也是当做桌面系统用了,很少熟悉那些linux命令结果上次linux桌面图形界面进不去了只能用命令行,经过几个晚上的折腾终于弄好,现在记 ...

  8. 【poj3461】 Oulipo

    http://poj.org/problem?id=3461 (题目链接) 题意 求一个字符串在另一个字符串中出现的次数. Solution KMP裸题,太久没写过了,都忘记怎么求next数组了..水 ...

  9. choorme 升级到最新版 adobe flash提示过期解决方案

    进入adobe flash 官网,下载最新版的adobe flash http://labs.adobe.com/downloads/flashplayer.html

  10. Bzoj2118 墨墨的等式

    Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1488  Solved: 578 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+ ...