1、生成秘钥
为了使用秘钥,我们必须自己生成一对秘钥
生成秘钥可以按照以下步骤
在服务器上运行
user@host :~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): # 直接回车确认路径及文件按名字,也可以自己起名字
Enter passphrase (empty for no passphrase): #输入密钥锁码,或直接按 Enter 留空秘钥密码锁,这个密码是打开秘钥使用的,在此可以直接回车
Enter same passphrase again: #再输入一遍密钥锁码
Your identification has been saved in /home/user/.ssh/id_rsa. # 私钥生成
Your public key has been saved in /home/user/.ssh/id_rsa.pub. #公钥生成
The key fingerprint is:
0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08
现在,在 user 用户的home目录中生成了一个 .ssh 的隐藏目录,内含两个密钥文件。id_rsa 为私钥,id_rsa.pub 为公钥
到此,秘钥就生成完毕了
2、关闭密码登录并开启ssh秘钥登录
编辑 /etc/ssh/sshd_config 文件
确保秘钥认证的2个配置是yes状态
RSAAuthentication yes
PubkeyAuthentication yes
重启ssh服务
sudo service sshd restart
3、使用秘钥登录
将/home/user/.ssh/id_rsa 私钥下载到client的保存,注意,私钥的权限普通用户不能读写,需要修改权限或者提权才能复制。登录时将私钥导入即可登录。
4、最后关闭密码登录
PasswordAuthentication no