【嵌入式Linux】添加sshd (顺便dropbear--scp命令)

文章目录

一、文件系统支持--编译前支持

bash 复制代码
make menuconfig


二、嵌入式Linux设备端

0. 修改ssh的配置文件

bash 复制代码
vi /etc/ssh/sshd_config

找到并修改以下行,确保它们是这样的:

bash 复制代码
# 启用公钥认证
PubkeyAuthentication yes

# 指定公钥文件位置(通常这行已存在且正确)
AuthorizedKeysFile .ssh/authorized_keys

# 禁用密码认证 - 这是实现无密码登录的关键
PasswordAuthentication no

# 禁止空密码登录(可选,但推荐)
PermitEmptyPasswords no

1. 在开发板上生成SSH密钥对

首先,在您的Arm Linux开发板的终端里,运行以下命令来生成密钥对:

bash 复制代码
ssh-keygen -t ed25519 -C "for_ssh_login"
  • 系统会提示你 Enter file in which to save the key (/home/your_user/.ssh/id_ed25519):,直接按回车,使用默认路径。

  • 系统会提示 Enter passphrase (empty for no passphrase):,直接按回车,不要设置密码。这样才是真正的无密码登录。

  • 再次提示 Enter same passphrase again:,再次直接按回车。

这样就在 ~/.ssh/ 目录下生成了两个文件:私钥 id_ed25519 和公钥 id_ed25519.pub。

2. 将公钥授权给本机

接下来,把刚刚生成的公钥添加到授权密钥文件中:

bash 复制代码
# 确保 .ssh 目录存在且权限正确
mkdir -p ~/.ssh
chmod 700 ~/.ssh

# 将公钥内容添加到 authorized_keys 文件
cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys

# 设置 authorized_keys 文件的权限为 600
chmod 600 ~/.ssh/authorized_keys

3. 启动SSH服务器

bash 复制代码
/etc/init.d/sshd  start

4. 除错处理

a. 报错 procd_add_mdns

vi /etc/init.d/sshd

注释掉如下代码:

bash 复制代码
#procd_add_mdns "ssh" "tcp" "$lport"

b. 报错 Privilege separation user sshd does not exist

bash 复制代码
vi /etc/passwd

在末尾添加一行:

bash 复制代码
sshd:x:109:65534::/var/run/sshd:/usr/sbin/nologin

三、Ubuntu虚拟机端

虚拟机事先生成秘钥。

bash 复制代码
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/liefyuan/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/liefyuan/.ssh/id_rsa.
Your public key has been saved in /home/liefyuan/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:hMjV1l/QIxAi7S6C1X5w23u103cvZNI5/uCirEFsbgs liefyuan@ubuntu
The key's randomart image is:
+---[RSA 2048]----+
|      oo..oo.o   |
|   . o o+.. . +  |
|    o..o.  . o . |
|    . ooo   .    |
|   o . +So   . . |
|  . . o++ . . B  |
|     .Eo+  . *.+ |
|       o +. o.=.+|
|        o.oo ..+*|
+----[SHA256]-----+

虚拟机的秘钥位置:~/.ssh/id_rsa.pub

输入命令后三个回车

拷贝公钥到设备端:vim ~/.ssh/id_rsa.pub

设备端粘贴公钥

bash 复制代码
echo "你的密钥" >> ~/.ssh/authorized_keys
  • 杀死 sshd:killall sshd
  • 开始 sshd:/etc/init.d/sshd start

虚拟机发送文件到设备端--带秘钥

bash 复制代码
scp -i ~/.ssh/id_rsa demo root@192.168.1.106:/opt/

例子:

bash 复制代码
$ scp -i ~/.ssh/id_rsa demo root@192.168.1.106:/opt/
demo                                                                                100% 3517KB 446.0KB/s   00:07    
相关推荐
梁下轻语的秋缘1 小时前
I2S与I2C
运维·stm32·单片机·51单片机
安达发公司1 小时前
安达发|效率革命:APS自动排程,为“金属丛林”安装精准导航
大数据·运维·人工智能·aps高级排程·aps排程软件·安达发aps·aps自动排程
科士威传动2 小时前
精密仪器中的微型导轨如何选对润滑脂?
大数据·运维·人工智能·科技·机器人·自动化
TracyGC2 小时前
Linux环境-RTX5080显卡CUDA12.8下安装mmcv/mmdetection3d
linux·运维·服务器
xu_ws2 小时前
Linux下快速安装配置Redis全攻略
linux·运维·服务器
qq_2153978972 小时前
内网穿透服务 frps
运维·docker·容器
Heart_to_Yang2 小时前
企业惠普打印机添加、打印乱码与扫描失败故障通用排查手册
运维·服务器·经验分享
飘忽不定的bug2 小时前
ubuntu22.04部署ROS2-humble
linux·ubuntu·ros2
坚定信念,勇往无前3 小时前
Alibaba Cloud Linux 4.0安装mongodump
linux·运维·服务器
虹梦未来3 小时前
【运维心得】Ubuntu2404编译nginx隐藏Server信息
运维·服务器·nginx