SSH相互认证的问题 (authorized_keys )

今天公司做容灾测试的2台LINUX服务器的SSH相互认证出现了问题,重建公密钥老是提示输入密码。 

最后发现,可能是由于谁修改了/home/dbra3的权限(没修改前是777,我改后为了700) 

[dbra3@hzmc .ssh]$  chmod  700  ~/.ssh  
[dbra3@hzmc .ssh]$ /usr/bin/ssh-keygen -t rsa 
Generating public/private rsa key pair. 
Enter file in which to save the key (/home/dbra3/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/dbra3/.ssh/id_rsa. 
Your public key has been saved in /home/dbra3/.ssh/id_rsa.pub. 
The key fingerprint is: 
5d:ec:ad:a3:0e:29:4b:2a:f6:67:2e:da:41:09:0e:2c dbra3@hzmc 
The key's randomart image is: 
+--[ RSA 2048]----+ 
|                 | 
|.          .     | 
|E..         o    | 
|.o . .   . o .   | 
|  . o   S . . .  | 
|   .     .   .   | 
|    . o o   o    | 
|  o..+oo . . .   | 
| ..++=o  .o      | 
+-----------------+ 
[dbra3@hzmc .ssh]$ touch ~/.ssh/authorized_keys 
[dbra3@hzmc .ssh]$ ssh 172.16.4.28 
The authenticity of host '172.16.4.28 (172.16.4.28)' can't be established. 
RSA key fingerprint is 4d:51:34:7e:e8:18:58:b2:0f:f9:14:78:7d:da:3e:fb. 
Are you sure you want to continue connecting (yes/no)? yes 
Warning: Permanently added '172.16.4.28' (RSA) to the list of known hosts. 
dbra3@172.16.4.28's password: 



[dbra3@hzmc ~]$ su - root 
Password: 
[root@hzmc ~]# cd /home 
[root@hzmc home]# ls -l 
drwxrwxrwx  5 dbra3  dbra3     4096 Dec 30  2009 dbra3 
drwxrwxrwx  2 root   root     16384 Sep 22  2009 lost+found 
drwxrwxrwx 14 ora10g oinstall  4096 Dec 29  2009 ora10g 
drwxrwxrwx 18 oracle oinstall  4096 Dec 16  2009 oracle 
[root@hzmc home]# chmod 700 dbra3 
[root@hzmc home]#su - dbra3 
[dbra3@hzmc]$cd .ssh 
[dbra3@hzmc .ssh]$ chmod 644 ~/.ssh/authorized_keys 

就这样,ssh相互认证就可以了 
在树莓派上进行打开 `authorized_keys` 文件粘贴公钥,并执行 `nano ~/.ssh/authorized_keys` 和 `chmod 600 ~/.ssh/authorized_keys` 操作,以下是详细步骤。 ### 准备工作 在进行操作前,需要确保已生成了 SSH 密钥对,通常密钥对存放在用户目录的 `~/.ssh` 下,包含私钥(如 `id_rsa`)和公钥(如 `id_rsa.pub`) [^2]。 ### 打开 `authorized_keys` 文件并粘贴公钥 1. **检查文件是否存在**:如果 `~/.ssh/authorized_keys` 文件不存在,需要先创建它。可以使用以下命令: ```bash touch ~/.ssh/authorized_keys ``` 2. **打开文件**:使用 `nano` 文本编辑器打开 `authorized_keys` 文件,命令如下: ```bash nano ~/.ssh/authorized_keys ``` 执行该命令后,`nano` 编辑器会打开 `authorized_keys` 文件。如果文件为空,会显示一个空白的编辑界面。 3. **粘贴公钥**:将本地生成的公钥内容复制,然后在 `nano` 编辑器中,使用鼠标右键粘贴(如果支持鼠标操作)或者使用快捷键(如 `Ctrl + Shift + V`)粘贴公钥内容到文件中。公钥通常是一行以 `ssh-rsa` 或其他加密算法开头的字符串。 4. **保存并退出**:粘贴完成后,按 `Ctrl + X` 组合键,然后按 `Y` 确认保存,最后按 `Enter` 键退出 `nano` 编辑器。 ### 执行 `chmod 600 ~/.ssh/authorized_keys` 命令 `chmod` 命令用于更改文件的权限。`600` 表示文件所有者具有读写权限,而组用户和其他用户没有任何权限。执行以下命令: ```bash chmod 600 ~/.ssh/authorized_keys ``` 此操作非常重要,因为如果 `authorized_keys` 文件的权限设置不正确,SSH 服务可能会拒绝使用公钥认证,仍然要求输入密码登录 [^3]。 ### 确认操作是否正确 可以通过以下方式确认操作是否正确: - **查看文件权限**:使用 `ls -l ~/.ssh/authorized_keys` 命令查看文件权限,确保权限显示为 `-rw-------`,这表示文件权限为 `600`。 - **尝试无密码登录**:在本地机器上尝试使用 SSH 命令登录到树莓派,如果配置正确,应该可以无密码登录。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值