天涼好個秋.

使用公钥登陆OpenWrt

2833 阅 0 评 问题&解决方案

以下操作均在 Bash on windows 下执行。

生成密钥

输入以下命令,输入三次回车,即可得到一对密钥。

toor@SurfaceBook:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/toor/.ssh/id_rsa): 
Created directory '/home/toor/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/toor/.ssh/id_rsa.
Your public key has been saved in /home/toor/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:U2ZM6mqTrxl0e/JWVygB9fw6RXHZBHcinGM7wegMmws 
toor@SurfaceBook
The key's randomart image is:
+---[RSA 2048]----+
|          o*o.o+B|
|        .+. O+ +=|
|        .*=. =o..|
|      E.o+o + .o.|
|      ..S.   o .o|
|     . +.o  . .o |
|      * o .. .o  |
|     . = +.    . |
|      o....      |
+----[SHA256]-----+
toor@SurfaceBook:~$ ll .ssh
总用量 4
drwx------ 0 toor toor  512 11月 30 20:37 ./
drwxr-xr-x 0 toor toor  512 11月 30 20:37 ../
-rw------- 1 toor toor 1679 11月 30 20:37 id_rsa
-rw-r--r-- 1 toor toor  398 11月 30 20:37 id_rsa.pub

上传公钥

上传公钥到路由器

ssh-copy-id root@tw -p1022

此时公钥在路由器中的位置: /root/.ssh/authorized_keys
将公钥拷贝到 dropbear 目录下,并赋予争取的权限

cp /root/.ssh/authorized_keys /etc/dropbear/
chmod 700 /etc/dropbear
chmod 600 /etc/dropbear/authorized_keys

使用私钥登陆

ssh -i ~/.ssh/id_rsa root@tw -p 1022

不出意外,已经可以免密码登陆了。

更便利

虽然省去密码,但 SSH 命令还是要敲的,那就想办法更便利些。

vim .ssh/config
# 添加如下配置
Host gee
HostName tw
Port 1022
IdentityFile ~/.ssh/id_rsa
User root

然后就可以使用 Host 指定的名称登陆了。

toor@SurfaceBook:~$ ssh gee                                                                                                                                                                                                                  


BusyBox v1.25.1 () built-in shell (ash)

***********************************************************
              __  __  _              _   ____  _   TM
             / / / / (_) _      __  (_) / __/ (_)
            / /_/ / / / | | /| / / / / / /_  / / 
           / __  / / /  | |/ |/ / / / / __/ / /  
          /_/ /_/ /_/   |__/|__/ /_/ /_/   /_/   
                  http://www.hiwifi.com/
***********************************************************
root@Hiwifi:~# 

Enjoy it

EOF