使用public key来做SSH authentication

public key authentication(公钥认证)是对通过敲用户名、密码方式登录服务器的一种替代办法。这种方法更加安全更具有适应性,但是更难以配置。

传统的密码认证方式中,你通过证明你你知道正确的密码来证明你是你。证明你知道密码的唯一方式是你告诉服务器密码是什么。这意味着如果服务器被黑掉,或者欺骗,那么一个黑客攻击者就能学习到你的密码。

Public key authentication(公钥认证)解决了这个问题。你产生一个密钥对,该密钥对由一个public key(公钥)(每个人都可以知道的)和一个私钥(你负责该私钥的安全,不让任何人知道)。私钥可以用于产生签名(signatures)。一个由你的私钥派生出来的签名不能被任何不知道那个私钥的人所伪造,但同时任何知道你的公钥的人都能验证一个签名是否真正是由你的那个公钥、私钥所配对生成的签名!

你可以在自己的机器上创建上述公钥私钥key pair,随后你将公钥copy到服务器上。然后服务器询问你证明你是你时,PuTTY可以使用你的私钥产生一个签名并传给服务器。服务器就可以验证那个签名的真伪了(因为服务器有公钥),并且决定是否允许你登陆。现在如果服务器本身被黑掉或者被欺骗,那么攻击者无法获得你的私钥或者密码,他们仅仅能够获得一个签名而已。而签名本身是不能被重用的,所以他们什么也无法获得(原因是签名都是在秘钥交换过程中动态生成的)。

在这里有一个问题:如果你的私钥在你的本机明码保存的话,那么任何人只要能够访问到那个私钥文件那么他就能够冒充你来创建签名。所以他们因此将能够冒充你的身份登录到系统中去。因为这个原因,那么你的私钥通常在本地存储时是加密过的,这个私钥的加密就使用一个你设定的passphrase来加密的。为了创建一个签名,PuTTY必须要首先解密这个密码私钥,你必须通过输入正确的passphrase才能保证PuTTY能够正确解密私钥,进而创建签名。

上述过程使得public key authentication方式和单纯密码认证方式显得有些不是很方便:每次你登陆服务器,替代输入一个简单密码的方式,而必须输入一个很长的passphrase。一个解决方案是你使用一个authentication agent,该认证代理就是一个保存拥有已经被解密过的私钥并且据此在被请求时创建签名。Putty的认证代理被称为Pageant.当你开始一个windows session,你启动Pageant并且加载你的私有秘钥(需要输入一次passphrase)。其他的session,你就可以启动Putty任意次数,而由于Pageant会在不需你任何介入的情况下自动创建你的签名,而使得公钥认证使用更加方便一些。当你关闭了windows session, pageant shuts down,而永远不会将被解密过的私钥文件放到磁盘(仅仅存在于内存中)。很多人认为这是一个安全性和便利性的很好折中。

有多种公钥算法,最常用的是RSA,但也有其他的,比如DSA(DSS),美国联邦数字签名标准等。

在github使用中,如果你需要git push操作的话,很有可能就会出现:

GitHub: Permission denied (publickey).

的错误,原因就是你没有在github服务器上创建公钥,方法参考:

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公钥的过程罗列一下:

  • Check for SSH keys
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
  • Generate a new SSH key

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
  • Add your key to the ssh-agent

# 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

  •  Add your SSH key to your account

To configure your GitHub account to use your SSH key:

Copy the SSH key to your clipboard. If your key is named id_dsa.pubid_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

原文地址:https://www.cnblogs.com/kidsitcn/p/4539723.html