5

openwrt dropbear配置 | 在客户端使用ssh-key密钥形式免密码登录openwrt(或其他Linux...

 2 years ago
source link: https://hellodk.cn/post/522
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

openwrt dropbear配置 | 在客户端使用ssh-key密钥形式免密码登录openwrt(或其他Linux系统)

ssh 登录服务器,习惯了使用密码登录,但到后面的确发现每次输入密码很麻烦。那么我们使用公密钥登录服务器将会一劳永逸,不仅安全,还更方便。

一般发行版本的 Linux 系统都是使用的 sshd 作为 ssh 服务端,我们将客户端的 ~/.ssh/id_rsa.pub 拷贝到服务端的 ~/.ssh/authorized_keys 即可(Ubuntu 就是这个文件),注意权限需要至少是 600,而且权限给的越小越安全。

但 openwrt 使用的 dropbear 作为 ssh 的服务端程序,我们需要将公钥拷贝到 /etc/dropbear/authorized_keys,最好保证其文件权限是 644。

首先我们需要了解到 ssh 登录服务器需要 ssh 客户端程序和 ssh 服务端程序。

如果客户端安装有 ssh 的客户端程序,使用 ssh-keygen 命令生成一对密钥——公钥和私钥,私钥保存在客户端本机,一般的文件名是 ~/.ssh/id_rsa (这是使用 rsa 算法生成的私钥),公钥要拷贝其文本到服务端的 ~/.ssh/authorized_keys,在 openwrt 上就是要拷贝到 /etc/dropbear/authorized_keys

tips:

  • 加密方式选 rsa dsa均可以,默认 dsa,使用较多的是 rsa
  • 这里会提示你输入私钥的文件名,注意要写全了文件绝对路径(并包含文件名,比如 /Users/abc/.ssh/github_id_rsa )
  • 接着上面一行,上面的是 macOS。如果是 Linux 文件名一般是 /home/abc/.ssh/github_id_rsa
  • Windows 下 借助 git bash 也可以这样生成
  • 注意:如果你不自定义私钥的文件名,将会覆盖本机已经生成的 id_rsa 文件,如果实在要覆盖,建议备份一下
  • 使用自定义的私钥的文件名的目的是为了在本机保存多个 ssh-key
  • 比如 github 的、gitlab 的、gitee 的、还有 VPS 的、像这种自己的服务器的、openwrt 路由器的等等
  • 后面还会让你输入 passphrase 个人建议是没必要设置这个密码了,如果你的环境中对安全性要求很高那还是建议设置一下。因为我的安全性要求不高,设置这种方式登录就是为了规避输入密码的问题,所以不设置了。
  • 我也尝试过设置 passphrase 后面 ssh 登录服务器时 还需要输入一遍这个密码,还是很麻烦。如果设置了这个密码不想删掉重新设置又想不输入密码登录 可以看看下面两篇文章 删除SSH私钥密码(passphrase)
ssh-keygen -t rsa

比如我输入的文件名是 /Users/huadekai/.ssh/softRouter_id_rsa

那么生成的对应的公钥是 /Users/huadekai/.ssh/softRouter_id_rsa.pub,将这个文件的文本内容拷贝到 /etc/dropbear/authorized_keys(如果没有这个文件则需要新建,建议文件权限:644)

也可以通过 openwrt luci 页面设置公钥,如下图

20210424111029.png

设置好之后需要配置一下客户端。

编辑配置文件 /Users/huadekai/.ssh/config 如果没有这个文件则新建,追加以下内容,其中

  • Host 是 ssh 的时候的别名
  • Hostname 是真正的服务端主机,要么域名要么 IP,均可以
  • IdentityFile 写成你自己的绝对路径
  • User 是使用哪个用户登录,必须是 openwrt 系统中已经存在的且具有 shell 解释器的(就是 /etc/passwd 中有被赋予 shell,是能够登录的用户名)
  • Port 指定 ssh 服务端监听的端口
Host myrouter
Hostname 10.10.10.1
IdentityFile /Users/huadekai/.ssh/softRouter_id_rsa
User root
Port 1003

下面尝试使用 ssh myrouter 登录吧~

huadekai@huadekaideMBP .ssh % ssh myrouter
_______ ________ __
| |.-----.-----.-----.| | | |.----.| |_
| - || _ | -__| || | | || _|| _|
|_______|| __|_____|__|__||________||__| |____|
|__| W I R E L E S S F R E E D O M
-----------------------------------------------------
OpenWrt SNAPSHOT, r2406-f8bd6e0 , Mask Ver.D200401
-----------------------------------------------------
root@dkRouter:~#

尝试使用 ssh t400 免密码登录我的 ubuntu 机器

huadekai@huadekaideMBP .ssh % ssh t400
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.15.0-58-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
1248 packages can be updated.
202 updates are security updates.
Last login: Sat Apr 24 11:25:54 2021 from 10.10.10.168
dk at dk-tpt400 in ~

good! 不需要输入密码直接登录系统

本文的分享结束,end.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK