buildroot 添加ssh

编译官方固件后发现没有ssh登录。修改buildroot添加ssh的功能。

  • 在buildroot目录下make menucofig

  • 添加OpenSSH包

    menuconfig界面中,导航到以下选项以启用OpenSSH:

    • Target packages --->
      • Networking applications --->
      • [*] openssh
        • [*] openssh-server
        • [*] openssh-client
  • 实测在添加选项编译后,可以启动ssh服务,但是并不能使用root账户登录,需要新建一个普通用户用普通用户登录才可以,需要修改/etc/ssh/sshd_config
    添加如下代码

    PermitRootLogin yes
    PasswordAuthentication yes
如果想讲这些配置模式在系统固件中配置。有以下两种方式
使用overlay方式
  • 1、在buildroot/board/<your_board>/overlay/etc/ssh/路径下创建sshd_config文件,并添加内容,编译固件的时候会将overlay的文件夹拷贝到生成的文件系统中去。例如上边的例子可以使用以下代码
mkdir -p board/<your_board>/overlay/etc/ssh
echo "PermitRootLogin yes" > board/<your_board>/overlay/etc/ssh/sshd_config
echo "PasswordAuthentication yes" >> board/<your_board>/overlay/etc/ssh/sshd_config

这里<your_board>是你的板子的名称。你可以选择任何合适的目录名。

  • 2、在Buildroot的menuconfig中配置Overlay文件系统,使其包含自定义的文件和目录。
    导航到以下选项并设置Overlay目录:

    • System configuration --->
      • (board/<your_board>/overlay) Root filesystem overlay directories

    将board/<your_board>/overlay添加到Root filesystem overlay directories中,以包含自定义的文件和配置。

使用BR2_ROOTFS_POST_BUILD_SCRIPT方式
  • 1、编写一个自定义脚本,在buildroot 编译过程中运行脚本来放置默认文件。首先,在buildroot中,例如我这里放在board/nuvoton/ma35d1/post-build.sh
  • 2、确保脚本有执行权限,chmod +x post-build.sh
  • 3、在buildroot 的menuconfig中配置脚本。
    • System configuration --->
      • (/path/to/post-build.sh) Custom scripts to run after building target (post-build.sh)
  • 4、在脚本中包含复制文件的操作,脚本的实例如下
#!/bin/sh

# 在镜像中添加文件或进行其他操作
# 例如,添加一个README文件
echo "这是一个默认的README文件" > ${TARGET_DIR}/root/README.txt

openssh默认配置

  • OpenSSH的默认配置文件通常位于/etc/ssh/sshd_config。以下是一个典型的默认配置文件的示例,包含一些常见的配置选项:
# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2

# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 2m
PermitRootLogin prohibit-password
StrictModes yes
MaxAuthTries 6
MaxSessions 10

# RSAAuthentication yes
# PubkeyAuthentication yes
# AuthorizedKeysFile	.ssh/authorized_keys

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

# Allow TCP forwarding
AllowTcpForwarding yes
# Allow agent forwarding
AllowAgentForwarding yes
# Allow X11 forwarding
X11Forwarding yes
# Print the date and time when users log in
PrintMotd yes
# Print the banner file when users log in
#Banner none

# Allow client to request a pseudo-terminal
PermitTTY yes
# Log sftp level file transfers
Subsystem sftp /usr/lib/openssh/sftp-server

常见的配置选项说明

  • Protocol 2:指定使用SSH协议版本2。
  • HostKey:指定用于SSH协议版本2的主机密钥文件。
  • SyslogFacility:指定syslog的日志设施,默认是AUTH。
  • LogLevel:指定日志的详细程度,默认是INFO。
  • PermitRootLogin:控制root用户是否允许通过SSH登录。默认是- prohibit-password,表示禁止使用密码登录,可以使用yes、no、- without-password等值。
  • PasswordAuthentication:控制是否允许使用密码认证,默认是yes。
  • ChallengeResponseAuthentication:控制是否启用挑战响应认证,默认是no。
  • PermitEmptyPasswords:控制是否允许空密码登录,默认是no。
  • X11Forwarding:控制是否允许X11转发,默认是yes。
  • Subsystem sftp:指定sftp子系统的路径。
posted @ 2024-07-19 18:10  lxblog  阅读(7)  评论(0编辑  收藏  举报