官方说明: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 4325: NOIP2015 斗地主

    4325: NOIP2015 斗地主 Time Limit: 30 Sec  Memory Limit: 1024 MBSubmit: 684  Solved: 456[Submit][Status] ...

  2. [日常训练]mod

    Description 给定$p_1,p_2,-,p_n,b_1,b_2,...,b_m$, 求满足$x\;mod\;p_1\;\equiv\;a_1,x\;mod\;p_2\;\equiv\;a_2 ...

  3. Matlab中cell存储为txt

    clc clear all [data1,data3]=textread('E:\RSWeb\mahoyt数据集\movielens\u.user','%s%*d%s%*s%*s','delimite ...

  4. Linux中/etc/hosts文件总是被自动修改

    关闭NetworkManager服务即可. 临时关闭: service  NetworkManager stop 永久关闭: chkconfig NetworkManager off 在centos6 ...

  5. 【codeforces 733F】 Drivers Dissatisfaction

    http://codeforces.com/problemset/problem/733/F (题目链接) 题意 给出一张n个点的无向图,每一条变有两个特征值:${w,c}$:分别表示这条边的权值为$ ...

  6. css3的@media媒体查询

    css3新功能,根据屏幕大小进行识别. 参考: http://www.runoob.com/cssref/css3-pr-mediaquery.html

  7. 基本概率分布Basic Concept of Probability Distributions 5: Hypergemometric Distribution

    PDF version PMF Suppose that a sample of size $n$ is to be chosen randomly (without replacement) fro ...

  8. CSS 公共样式分享

    global.css | reset.css(格式化样式) common.css(公共组件样式) layout.css(当前页面样式) 清除全站所有页面的浏览器默认样式,保证在初始样式在所有浏览器下一 ...

  9. 【Beta版本】冲刺-Day4

    队伍:606notconnected 会议时间:12月12日 目录 一.行与思 二.站立式会议图片 三.燃尽图 四.代码Check-in 一.行与思 张斯巍(433) 今日进展:协助队友完成界面的修改 ...

  10. UVA 11419SAM I AM(输出 最小覆盖点 )

    参考博客:如何找取 最小覆盖点集合 题意:R*C大小的网格,网格上面放了一些目标.可以再网格外发射子弹,子弹会沿着垂直或者水平方向飞行,并且打掉飞行路径上的所有目标,计算最小多少子弹,各从哪些位置发射 ...