1.生成SSH公钥私钥

  • 进入文件夹中(注:所有"#“表示说明)
cd ~/.ssh/

# 其中-t rsa是指定rsa加密算法, -C "asus"是添加描述asus

ssh-keygen -t rsa -C "asus"
# 说明: 默认是保存在/Users/你的用户名/.ssh/下, 如果你指定了名字则保存在当前路径下.
# 指定的必须是文件且是绝对路径
# 在这里我已经在 ~/.ssh/中, 默认会保存在这里
Enter file in which to save the key (/Users/username(你的用户名)/.ssh/id_rsa): asus_rsa


# 这里提示输入密码, 直接Enter表示不要密码

Enter passphrase (empty for no passphrase):

# 与上面输入的密码保持一致, 如果上面没输入,这里也不要输入

Enter same passphrase again:
  • 提示这些信息就表示生成成功
Your identification has been saved in /Users/username/.ssh/asus_rsa.
Your public key has been saved in /Users/username/.ssh/asus_rsa.pub.
The key fingerprint is:
SHA256:WNwJoo/Rr0TvpnQIdA4YisZrF+7ZM8lKNB4JLpJXALA asus
The key's randomart image is:
+---[RSA 2048]----+
|+.o   . .        |
|oo + o o o .     |
|E+..* + o o      |
|o.+ooO =         |
|++.Bo = S        |
|+.= *o.+         |
|   = *+ +        |
|  . ..o+         |
|   .  .          |
+----[SHA256]-----+
  • 查看生成的文件asus_rsa密钥, asus_rsa.pub公钥
ls ~/.ssh/

2.配置服务器公钥, 可以远程登录

  • 上传公钥到服务器中, windows下xshell,mac 终端terminal
# -i指定公钥文件, -p指定服务器端口

ssh-copy-id -i local_centos7_rsa.pub [email protected] -p 22
输入用户密码就可以输入成功了, 显示成功的信息

Number of key(s) added: 1

Now try logging into the machine, with: “ssh -p ‘22’ ‘’” and check to make sure that only the key(s) you wanted were added.

  • 添加配置
vim ~/.ssh/config
添加如下配置, Host表示主机别名, User是登录的用户名, HostName表示服务器ip地址或网址, PreferredAuthentications首先认证方式,IdentityFile私钥文件路径, Port服务器端口

多个用户时Host设置必须唯一, 禁止设置为ip地址

Host locale_centos7
 User jenkins
 HostName 10.211.55.9
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/local_centos7_rsa
 Port 22
  • 尝试登陆服务器
ssh locale_centos7
却提示公钥的权限不安全

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: UNPROTECTED PRIVATE KEY FILE! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Permissions 0644 for ‘/Users/username/.ssh/local_centos7_rsa.pub’ are too open. It is required that your private key files are NOT accessible by others. This private key will be ignored. Load key “/Users/username/.ssh/local_centos7_rsa.pub”: bad permissions : Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

  • 更改~/.ssh/local_centos7_rsa.pub文件权限
chmod 400 ~/.ssh/local_centos7_rsa.pub
  • 再次尝试登陆即是正常(ssh -v locale_centos7中-v会打印连接日志)
ssh locale_centos7
传输文件的时候也能使用scp about.md mini_jenkins:/var/lib/jenkins/test, 将about.md文件上传到使用服务器/var/lib/jenkins/test文件夹下

3.github使用ssh公钥

  • 复制公钥信息~/.ssh/local_centos7_rsa.pub 存入github keys设置中

  • 增加配置文件~/.ssh/config

这个Host的别名,要有一个github.com, github默认ssh名字是github.com, 为了方便使用

Host github.com
 User git
 HostName github.com
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/local_centos7_rsa
  • 测试和拉取公共类型的github代码
ssh -T g[email protected]

git clone [email protected]:Charles-one/actiger.git
  • 多个github用户

添加配置./.ssh/config

Host work.github.com
 User git
 HostName github.com
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/wrok_rsa

测试和拉取公共类型的github代码就变成

对比下, Host默认使用的github.com方便

ssh -T [email protected]

git clone [email protected]:Charles-one/actiger.git