Centos7设置私钥登录

生成SSH公钥和私钥

ssh-keygen -t rsa
#注意生成时设置密钥的密码,这样在利用证书登录时就会先验证密码

导入公钥

#导入公钥到服务器用户ssh证书列表
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

#本地scp下载私钥
scp root@172.1.1.1:~/.ssh/id_rsa C:/Users/user/Documents/
#删除服务器公钥和私钥
rm -f ~/.ssh/id_rsa*

注意:
文件~/.ssh/authorized_keys 必须是 600权限

设置文件和文件夹读写权限

chmod -R 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

配置sshd_config文件

#设置允许证书登录
:set nu     #可显示行号,第43行
PubkeyAuthentication yes
#重启ssh服务
systemctl restart sshd.service

关闭密码登录(如果有需要)

确保证书登录成功后再配置
vi /etc/ssh/sshd_config
PasswordAuthentication no
systemctl restart sshd.service

私钥登录和下载文件

#登录
ssh -i ./id_rsa root@ip
#上传
scp -i ./id_rsa file root@ip:/root
#下载
scp -i ./id_rsa root@ip:/root/1.txt c:/1.txt

其他Linux系统可作为参考

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/243868.html

(0)
上一篇 2022年4月11日 22:10
下一篇 2022年4月11日 22:10

相关推荐

发表回复

登录后才能评论