一、设置远程密钥登录
1.生成公钥和私钥
ssh-keygen
或者(效果相同)
ssh-keygen -t rsa
运行上面的命令后,系统会出现一系列提示,可以一路回车,例如:
[root@iZm5ef9dh7rrcbptrvly9bZ .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):(回车)
Enter passphrase (empty for no passphrase):(回车)
Enter same passphrase again:(回车)
注:如果不要对私钥设置口令(passphrase),如果担心私钥的安全,可以设置一个。没有特殊需求直接回车即可。
运行结束以后, 默认在 ~/.ssh目录生成两个文件:
id_rsa :私钥
id_rsa.pub :公钥
2.切换到文件所在目录:cd ~/.ssh/
将公钥写入authorized_keys文件,执行命令:cat id_rsa.pub >> authorized_keys
3.在目标服务器上更改权限(必须)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
4.将私钥id_rsa拷贝至需要远程连接的服务器
在远程服务器执行拷贝命令:scp root@47.104.189.163:/root/.ssh/id_rsa ./
(./表示拷贝至当前目录下)
5.在终端设置快捷登录配置:vim ~/.ssh/config
Host zjk
HostName 47.104.189.163
Port 22
User root
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
在终端执行:ssh zjk 即可快速登录远程服务器
二、禁用密码登录
1、修改配置文件
vim /etc/ssh/sshd_config
#禁用密码验证
PasswordAuthentication no
#启用密钥验证
PubkeyAuthentication yes
2、重启ssh服务
#centos系统
service sshd restart
#ubuntu系统
service ssh restart
#debian系统
/etc/init.d/ssh restart
注:如果出现如下提示:Redirecting to /bin/systemctl restart sshd.service
执行命令,重启sshd服务:systemctl start sshd.service