在推行我的全服务器ssh登录禁止密码登录的进程中,我家里的r5s软路由还是用的密码登录,在去密码化的进程中还是比较危险的。在极端情况下还是有破解可能,所以研究了一下istoreos和openwrt的密钥更新方法,还是比较简单的。

方法一:通过Bitwarden生成密钥

在bitwarden中直接生成密钥,创建一个文本文件,将公钥内容复制到里面。

然后保存,名字设置为authorized_keys

然后保存到软路由的/etc/dropbear/目录并设置权限即可。

1
2
chmod 700 /etc/dropbear
chmod 600 /etc/dropbear/authorized_keys

bitwarden密钥管理

方法二:通过电脑本身生成密钥

首先,打开终端(Linux/Mac)或命令提示符/PowerShell(Windows),然后输入以下命令来生成SSH密钥对:

1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

系统会提示你选择保存密钥的文件路径和输入一个密码(可选)。如果你不想设置密码,直接按回车即可。

1
2
3
4
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yourusername/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

接下来,你需要将生成的公钥复制到软路由的/etc/dropbear/authorized_keys文件中。

或者,你可以手动将公钥内容复制到软路由的authorized_keys文件中:

1
cat ~/.ssh/id_rsa.pub

将输出的内容复制,然后通过SSH登录到软路由,编辑/etc/dropbear/authorized_keys文件,将公钥内容粘贴进去:

1
2
ssh root@你的软路由ip地址
echo "your_public_key" >> /etc/dropbear/authorized_keys

确保/etc/dropbear/目录和authorized_keys文件的权限设置正确,以防止未经授权的访问:

1
2
chmod 700 /etc/dropbear
chmod 600 /etc/dropbear/authorized_keys

测试SSH登录

现在,你可以尝试使用SSH密钥登录软路由,确保配置正确:

1
ssh -i 私钥文件地址 root@你的软路由ip地址

例如:

1
ssh -i ~/.ssh/id_rsa root@192.168.1.1

禁用密码登录(可选)

为了进一步增强安全性,你可以在软路由上禁用密码登录,只允许密钥登录。编辑软路由的SSH配置文件(/etc/config/dropbear

1
vim /etc/config/dropbear

找到以下行并进行修改:

1
option PasswordAuth 'no'

然后重启SSH服务:

1
/etc/init.d/dropbear restart

经过实测,关闭密码登录不会影响istoreos的网页终端登录,所以大家务必不要公网暴露网页终端登录端口。