赞
踩
CentOS 7 上创建SSH密钥
在生成新的SSH密钥对之前,最好检查 CentOS 客户端计算机上的现有SSH密钥。
请运行以下命令,该命令将列出所有公钥
ls -l ~/.ssh/id_*.pub
如果命令的输出返回类似cannot access /root/.ssh/id_*.pub: No such file or directory,则意味着您的客户端计算机上没有SSH密钥,您可以继续执行下一步并生成SSH密钥对。
1、首先生成一个新的4096位SSH密钥对,并将您的电子邮件地址作为注释:
ssh-keygen -t rsa -b 4096 -C "llhui20@sina.com"
2、系统将提示您指定文件名:
Enter file in which to save the key (/home/username/.ssh/id_rsa):
按Enter键接受默认文件位置和文件名。
3、系统会要求您输入安全密码。 是否要使用密码短语取决于您。 如果您选择使用密码,您将获得额外的安全层。
Enter passphrase (empty for no passphrase):
如果您不想使用密码短语,请按Enter键
# 整个交互过程: [root@liulihui /]# ls -l ~/.ssh/id_*.pub ls: cannot access /root/.ssh/id_*.pub: No such file or directory [root@liulihui /]# ssh-keygen -t rsa -b 4096 -C "llhui20@sina.com" Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:RpNQDtJe92E5qovCrKhoO4CiN4Jn0Y3LT6SPbTKFg4E llhui20@sina.com The key's randomart image is: +---[RSA 4096]----+ | ..o.. . | | ..+... = | | . . .=. + o | |E . .. .. . | |. + +. S. | |+ o =oo.. | |= =.+.. . | |=+= O=o . | |**+o.*+ | +----[SHA256]-----+ [root@liulihui /]#
查看生成SSH密钥
[root@liulihui /]# ls ~/.ssh/id_*
/root/.ssh/id_rsa /root/.ssh/id_rsa.pub
查看生成SSH私钥
[root@liulihui /]# cat /root/.ssh/id_rsa -----BEGIN RSA PRIVATE KEY----- MIIJKgIBAAKCAgEAxjHraoiyeyFSY82RQLonNtowp/z/dtuprWmX99FaD6Yp8wZ7 679xzDjkCS3p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Kos/Mj3KfnsPZL4xzPE BFVs80299d9+OY3VfmdBhfh+hingTNsgMwbnU7bQ24h0vkFjdSzTcF2fNHcogueA WR6Jn4RiGgqOZgH8pJoF7vFQa30uWb29EdDYS6wh3Zbt3JhkYFvuedZ+9U1SxOdR gfIX7ARhCySOIRxZLCasgyS2YPV9zDHzkwqV44uTotqoIKwMSPGV87WXpCtGgAq2 PrAWxUkCggEAByr9M26jNhml2d56Ad+7K5ry84FI685acTGTJn4y987f5XCo72l4 gO2/O3Bk9PHcjI9tsT5YJiv/uk/GqZZWJLu5DJ0OxjFnsV6orJ/u0vF8vJBqEu6n Or+inM8vK1vCNpsi8APqDDt9vHOyLMQJuOvPxta2eqzE2UibCvdeeSMt1P3wbZFa 1MSDkYkeQzFxGLXOq42XFab/pZvousYMfFH/FLWpYW9o5AuvQZKRa1tnz6BgLmQ3 UvpWXSZTAwiL35ii1DVzmJrohTSYLNgRl3PBfBjjTbrlp7oqvYWaZI2yC37ZF2XE ndXSLp+pfySN1+SbbL0cWJaDPxE6A8il0QKCAQEAym74/0nG3sdLPZ4Q+vmGqFq8 8fih1a0mcDbPrCnMQ/GzQpQ/ky5Gtk0MBTn2nSeM1q+zYfsmkl1JJzNAK2v5s0O7 3MkRLPBopcjLaQkOPJg8mLmEn3c4o5bmSVgs3ggtcgxUkLkAVBrtFxlmJTSJF/ue kex71pBw3EZf7MX2uslArEfqcjgLLUUB0hOuqg/stZK5eZiNrRaLe7/7mrjMSuag rCoTlMJFQ0bCaPigWbnQ57RqRfBSMZvuRnQUwdDFqVEqMH0VXEE3bbh3ptedPhfO Q258R8FtSMC9Ye10wIAtATgshjQFbvLGOQvCTZc42ZXpaTgdZnt1fOuXlX/txg== -----END RSA PRIVATE KEY-----
既然已生成SSH密钥对,下一步是将公钥复制到要管理的服务器。
将公钥复制到远程服务器的最简单和推荐的方法是使用名为ssh-copy-id的实用程序。 在您的本地机器终端类型:
[root@liulihui /]# ssh-copy-id root@192.168.133.128
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.133.128's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.133.128'"
and check to make sure that only the key(s) you wanted were added.
如果本地计算机上没有ssh-copy-id实用程序,请使用以下命令复制公钥:
cat ~/.ssh/id_rsa.pub | ssh root@192.168.133.128 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
使用SSH密钥登录服务器
完成上述步骤后,您应该能够登录到远程服务器而不会被提示输入密码。
要验证它,请尝试通过SSH登录到您的服务器:
ssh root@192.168.133.128
如果您没有为私钥设置密码,则会立即登录。 否则,系统将提示您输入密码。
# 测试
[root@liulihui ~]# ssh root@192.168.133.128
Last login: Thu Jan 5 00:56:46 2023 from 192.168.133.128
[root@liulihui ~]#
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。