Linux下SSH远程不用密码登陆

本文介绍如何通过生成SSH密钥对实现免密码登录远程主机。包括本地SSH-keygen生成密钥、将公钥复制到远程主机并设置权限等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SSH登录命令
ssh root@10.168.10.1
这时候会让你输入密码

命令格式为:ssh 用户@主机IP

不用密码登陆的过程如下:

原理是在远程主机上添加本地生成公钥,这样会自动验证,就不需要输入密码了

1 本地SSH-keygen

[root@localhost ~]# ssh-keygen -t rsa
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:
90:b0:f3:63:1b:9e:95:42:7f:15:95:7b:19:2a:29:a0 root@localhost.localdomain


然后我们到该路径下可以看到生成的文件

[root@localhost ~]# cd ~/.ssh/
[root@localhost .ssh]# ls
id_rsa  id_rsa.pub
2 拷贝到远程的主机~/.ssh/下

这里可以用命令scp直接拷贝

[root@localhost ~]# scp ~/.ssh/id_rsa.pub root@10.168.10.1:~/.ssh/id_rsa.pub
3 登录远程主机
[root@localhost ~]# cd ~/.ssh
[root@localhost ~]# cat id_rsa.pub >> authorized_keys
4 降低权限

[root@localhost ~]# cd ..
[root@localhost ~]# chmod -R 0600 ~/.ssh

这样,再用ssh登陆就不用输入密码了



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值