➜ ~/beizi_blog touch test.txt ➜ ~/beizi_blog echo"this is a test" >test.txt ➜ ~/beizi_blog cat test.txt this is a test ➜ ~/beizi_blog git add . # 添加当前目录所有文件到暂存区
➜ ~/beizi_blog ssh-keygen -t rsa -b 4096 -C "test keys"#测试生成密钥对到当前目录 Generating public/private rsa key pair. Enter file inwhich to save the key (/Users/bz/.ssh/id_rsa): beizi_blog_rsa Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in beizi_blog_rsa Your public key has been saved in beizi_blog_rsa.pub The key fingerprint is: SHA256:yWreqJG5jxl6c+cRcfiP9zMyP1s0haTP6htro26YyP4 test keys The key's randomart image is: +---[RSA 4096]----+ | . | | . o . | | o . . . .| | . = o .| | S . o..| | o . . o . ..| | =.o..o. = .| | .oO+o+..o=++. | | ..=*=+Eooo+*+= | +----[SHA256]-----+ ➜ ~/beizi_blog ls beizi_blog_rsa beizi_blog_rsa.pub test.txt # 注:千万不能把密钥放到项目目录中,这只是测试 # 将ssh key添加到ssh代理 eval "$(ssh-agent -s)" ssh-add ./beizi_blog_rsa # 将.pub中的内容复制到剪贴板 pbcopy < ./beizi_blog_rsa.pub # macos #粘贴到git帐户ssh keys中
如果想让不同的密钥对访问不同主机,需要建立ssh config 文件
1 2 3 4 5 6 7 8 9 10 11 12
# GitHub Host github.com HostName github.com User git IdentityFile ~/.ssh/github_rsa
# GitLab Host gitlab.com HostName gitlab.com User your_username IdentityFile ~/.ssh/gitlab_rsa