查当前系统密钥
在生成新的SSH密钥对之前,最好检查 CentOS 客户端计算机上的现有SSH密钥。
ls -l ~/.ssh/id_*.pub
如果命令的输出返回类似cannot access /root/.ssh/id_*.pub: No such file or directory,则意味着您的客户端计算机上没有SSH密钥,您可以继续执行下一步并生成SSH密钥对。
生成密钥
生成一个新的4096位SSH密钥对,并填写注释
ssh-keygen -t rsa -b 4096 -C "dai_linux_centos"
提示 生成公钥和密钥对中
输入文件地址来决定在哪里保存文件(可以直接Enter跳过,默认保存在/www/.ssh/id_rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/www/.ssh/id_rsa):
提示输入密码(如果是自动化的就不要输入密码,如果是手动拉取推送就用)
Enter passphrase (empty for no passphrase):
生成过程
[www@iZj6c5r46uy6phsb91zarsZ root]$ ls -l ~/.ssh/id_*.pub
ls: cannot access '/home/www/.ssh/id_*.pub': No such file or directory
[www@iZj6c5r46uy6phsb91zarsZ root]$ ssh-keygen -t rsa -b 4096 -C "dai_linux_centos"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/www/.ssh/id_rsa):
/home/www/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/www/.ssh/id_rsa.
Your public key has been saved in /home/www/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:kHOWSUJrK3cu3BXspDRbWMxacDhLSjzy24OIaNWf274 dai_linux_centos
The key's randomart image is:
+---[RSA 4096]----+
| oo o=o |
| . +=+B+ |
| .+*+O== |
| . ooBoB . |
| .....++S o |
|... .+o*o. |
|. o =. |
| o . |
| .E. |
+----[SHA256]-----+
[www@iZj6c5r46uy6phsb91zarsZ root]$
使用密钥对
打开存储密钥的文件夹(当前是www用户)
cd /home/www/.ssh
部署公钥到仓库,直接复制即可
vim id_rsa.pub