public key authentication(公钥认证)是对经过敲用户名、密码方式登陆服务器的一种替代办法。这种方法更加安全更具备适应性,可是更难以配置。html
传统的密码认证方式中,你经过证实你你知道正确的密码来证实你是你。证实你知道密码的惟一方式是你告诉服务器密码是什么。这意味着若是服务器被黑掉,或者欺骗,那么一个黑客攻击者就能学习到你的密码。git
Public key authentication(公钥认证)解决了这个问题。你产生一个密钥对,该密钥对由一个public key(公钥)(每一个人均可以知道的)和一个私钥(你负责该私钥的安全,不让任何人知道)。私钥能够用于产生签名(signatures)。一个由你的私钥派生出来的签名不能被任何不知道那个私钥的人所伪造,但同时任何知道你的公钥的人都能验证一个签名是否真正是由你的那个公钥、私钥所配对生成的签名!github
你能够在本身的机器上建立上述公钥私钥key pair,随后你将公钥copy到服务器上。而后服务器询问你证实你是你时,PuTTY可使用你的私钥产生一个签名并传给服务器。服务器就能够验证那个签名的真伪了(由于服务器有公钥),而且决定是否容许你登录。如今若是服务器自己被黑掉或者被欺骗,那么攻击者没法得到你的私钥或者密码,他们仅仅可以得到一个签名而已。而签名自己是不能被重用的,因此他们什么也没法得到(缘由是签名都是在秘钥交换过程当中动态生成的)。算法
在这里有一个问题:若是你的私钥在你的本机明码保存的话,那么任何人只要可以访问到那个私钥文件那么他就可以冒充你来建立签名。因此他们所以将可以冒充你的身份登陆到系统中去。由于这个缘由,那么你的私钥一般在本地存储时是加密过的,这个私钥的加密就使用一个你设定的passphrase来加密的。为了建立一个签名,PuTTY必需要首先解密这个密码私钥,你必须经过输入正确的passphrase才能保证PuTTY可以正确解密私钥,进而建立签名。windows
上述过程使得public key authentication方式和单纯密码认证方式显得有些不是很方便:每次你登录服务器,替代输入一个简单密码的方式,而必须输入一个很长的passphrase。一个解决方案是你使用一个authentication agent,该认证代理就是一个保存拥有已经被解密过的私钥而且据此在被请求时建立签名。Putty的认证代理被称为Pageant.当你开始一个windows session,你启动Pageant而且加载你的私有秘钥(须要输入一次passphrase)。其余的session,你就能够启动Putty任意次数,而因为Pageant会在不需你任何介入的状况下自动建立你的签名,而使得公钥认证使用更加方便一些。当你关闭了windows session, pageant shuts down,而永远不会将被解密过的私钥文件放到磁盘(仅仅存在于内存中)。不少人认为这是一个安全性和便利性的很好折中。安全
有多种公钥算法,最经常使用的是RSA,但也有其余的,好比DSA(DSS),美国联邦数字签名标准等。bash
在github使用中,若是你须要git push操做的话,颇有可能就会出现:服务器
GitHub: Permission denied (publickey).session
的错误,缘由就是你没有在github服务器上建立公钥,方法参考:ssh
https://help.github.com/articles/generating-ssh-keys/
在上述github的命令执行过程当中,
若执行ssh-add /path/to/xxx.pem是出现这个错误:Could not open a connection to your authentication agent,则先执行以下命令便可:
ssh-agent bash
为了方便,将上述建立github公钥的过程罗列一下:
ls -al ~/.ssh # Lists the files in your .ssh directory, if they exist id_dsa.pub id_ecdsa.pub id_ed25519.pub id_rsa.pub
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" # Creates a new ssh key, using the provided email as a label # Generating public/private rsa key pair. Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter] Enter passphrase (empty for no passphrase): [Type a passphrase] # Enter same passphrase again: [Type passphrase again] Your identification has been saved in /Users/you/.ssh/id_rsa. # Your public key has been saved in /Users/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
# start the ssh-agent in the background ssh-agent -s # Agent pid 59566 # start the ssh-agent in the background for windows eval $(ssh-agent -s) # Agent pid 59566
# ssh-add ~/.ssh/id_rsa
To configure your GitHub account to use your SSH key:
Copy the SSH key to your clipboard. If your key is named id_dsa.pub
, id_ecdsa.pub
orid_ed25519.pub
, then change the filename below from id_rsa.pub to the one that matches your key:
clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
http://the.earth.li/~sgtatham/putty/0.55/htmldoc/Chapter8.html