一、生成密钥对
[root@localhost ~]# ssh-keygen #输入指令生成密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):#指定密钥文件名,默认直接回车
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y #我这里已经存在密钥,所以选择覆盖
Enter passphrase (empty for no passphrase): #为密钥文件设置密码,空密码直接回车
Enter same passphrase again:
已成功生成密钥文件
二、在服务器安装公钥
[root@host ~]$ cd .ssh
[root@host .ssh]$ cat id_rsa.pub >> authorized_keys
输入以上两个命令进行公钥的安装
[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh
为文件和目录加权限
三、配置ssh,打开密钥登录功能
[root@host ~]$ vi /etc/ssh/sshd_config
打开文件进行配置
RSAAuthentication yes
PubkeyAuthentication yes