git config --global user.name "mayee"
git config --global user.email "maye_e@qq.com"
查看全局配置:
git config --global --list
显示:
user.name=mayee
user.email=maye_e@qq.com
生成 ssh 密钥:
ssh-keygen -t ed25519 -C "deepmi" # 使用 ed25519 算法生成秘钥
连续三次按回车,提示密钥被存储在/Users/用户目录/.ssh/id_ed25519.pub中,然后用cat命令查看密钥。
若需要使用多个 SSH 密钥对,在Enter file in which to save the key步骤时,输入一个新的文件名称就可以避免覆盖已有的密钥对。
deepmi是生成的 sshkey 的名称,并不约束或要求具体命名为某个邮箱。现网的大部分教程均讲解的使用邮箱生成,其一开始的初衷仅仅是为了便于辨识所以使用了邮箱。
创建密钥前先确认是否对目录有读写权限:
icacls "C:\Users\maye\.ssh\github\Ma-yeah"
需要确认只有自己有访问权限,如果其他用户也有访问权限,需要重置:
icacls "C:\Users\maye\.ssh\github\Ma-yeah" /reset
默认情况下,密钥会生成在C:\Users\maye\.ssh\目录下,但是我们可能会在GitHub、Gitee、GitLab等多个平台有多个账号。默认生成秘钥时,如果不指定保存路径,那么就会覆盖之前的密钥。
例如,当我在我的微星电脑micro-star上想为我的 GitHub 账号Ma-yeah生成一个 SSH 密钥,可以按如下步骤操作:
- 在
C:\Users\maye\.ssh\目录下手动创建目录,最终目录结构为C:\Users\maye\.ssh\github\Ma-yeah; - 打开
Git Bash输入ssh-keygen -t rsa -C "micro-star"; - 按一次回车后,会询问你秘钥保存的位置,默认为
/c/Users/maye/.ssh/id_rsa,此时修改为/c/Users/maye/.ssh/github/Ma-yeah/id_rsa; - 然后连续回车即可;
创建完密钥后还需要设置,编辑C:\Users\maye\.ssh\目录下的config文件(注意文件没有后缀,如果文件不存在则创建),添加如下内容:
# GitHub
Host github.com
IdentityFile C:\Users\maye\.ssh\Github\Ma-yeah\id_ed25519
User git
# GitLab
Host gitlab.com
IdentityFile C:\Users\maye\.ssh\Gitlab\Mayee\id_ed25519
User git
之后测试 SSH 连接是否成功:
生成 ssh 密钥也可以使用ssh-keygen -t rsa -C "deepmi",表示使用 RSA 算法生成秘钥。
如果 SSH 连接的是私有服务器,则在客户机上创建密钥对假设ssh-keygen -t ed25519 -C "client",假设存储密钥自定义路径是/c/Users/maye/.ssh/trae/ed25519,之后将密钥复制到服务器假设ssh-copy-id -i /c/Users/maye/.ssh/trae/ed25519.pub mayee@192.168.0.1,如果生成密钥时没有修改位置则可以省略掉-i参数,然后就可以通过ssh -i /c/Users/maye/.ssh/trae/ed25519 mayee@192.168.0.1连接到服务器了,如果不想在连接时指定参数,则可以通过上面的方式在config文件中配置连接服务器主机对应的私钥位置。
说明:ssh key的类型有四种,分别是dsa、rsa、ecdsa、ed25519。根据数学特性,这四种类型又可以分为两大类,dsa/rsa是一类,ecdsa/ed25519是一类,后者算法更先进。dsa因为安全问题,已不再使用了,ecdsa因为政治原因和技术原因,也不推荐使用,rsa是目前兼容性最好的,应用最广泛的 key 类型,在用ssh-keygen工具生成 key 的时候,默认使用的也是这种类型。不过在生成 key 时,如果指定的 key size 太小的话,也是有安全问题的,推荐 key size 是 3072 或更大。ed25519是目前最安全、加解密速度最快的 key 类型,由于其数学特性,它的 key 的长度比rsa小很多,优先推荐使用。它目前唯一的问题就是兼容性,即在旧版本的 SSH 工具集中可能无法使用。不过据我目前测试,还没有发现此类问题。
因此,优先选择ed25519,否则选择rsa。
以下是原文:
OpenSSH supports several signing algorithms (for authentication keys) which can be divided in two groups depending on the mathematical properties they exploit:
DSA and RSA, which rely on the practical difficulty of factoring the product of two large prime numbers, ECDSA and Ed25519, which rely on the elliptic curve discrete logarithm problem. (example)Elliptic curve cryptography (ECC) algorithms are a more recent addition to public key cryptosystems. One of their main advantages is their ability to provide the same level of security with smaller keys, which makes for less computationally intensive operations (i.e. faster key creation, encryption and decryption) and reduced storage and transmission requirements.
OpenSSH 7.0 deprecated and disabled support for DSA keys due to discovered vulnerabilities, therefore the choice of cryptosystem lies within RSA or one of the two types of ECC.
RSA keys will give you the greatest portability, while #Ed25519 will give you the best security but requires recent versions of client & server[2]. #ECDSA is likely more compatible than Ed25519 (though still less than RSA), but suspicions exist about its security (see below).