移植openssh到arm-linux

需要的源码

编译openssh需要zlib和openssl的环境,所以需要下载zlib、openssl和openssh三个源码。

openssh源码的INSTALL文件里会描述当前版本支持的zlib和openssl版本,例如:

You will need working installations of Zlib and OpenSSL.

Zlib 1.1.4 or 1.2.1.2 or greater (ealier 1.2.x versions have problems):
http://www.gzip.org/zlib/

OpenSSL 0.9.6 or greater:
http://www.openssl.org/

我使用版本的是 zlib-1.2.11 、openssl-0.9.8h 、openssh-5.6.p1

交叉编译源码

我在x64的Ubuntu18上编译,目标板是armhf架构的Linux

编译器版本:arm-none-linux-gnueabi-gcc version 4.8.3

创建zlib和openssl的安装目录,之后编译openssh的时候会依赖这些安装目录。

zlib

  • 解压源码压缩包,进入源码路径

  • 生成Makefile,并指定安装目录

    ./configure --prefix=/home/xxx/work/ssh_for_arm/lib/zlib-1.2.11
    
  • 修改生成的Makefile,指定编译工具。将文件中gcc和ar改为对应的arm交叉编译工具,有多处gcc和一处ar需要修改。

  • make

  • make install

openssl

  • 解压源码,进入源码路径

  • 执行初始化,指定目标板架构、编译工具、安装目录

    ./Configure linux-elf-arm -DB_ENDIAN linux:'arm-none-linux-gnueabi-gcc' --prefix=/home/xxx/work/ssh_for_arm/lib/openssl-0.9.8h
    
  • make

  • make install

  • 编译时可能会报错

    smime.pod around line 272: Expected text after =item, not a number
    smime.pod around line 276: Expected text after =item, not a number
    smime.pod around line 280: Expected text after =item, not a number
    smime.pod around line 285: Expected text after =item, not a number
    smime.pod around line 289: Expected text after =item, not a number
    POD document had syntax errors at /usr/bin/pod2man line 71.
    Makefile:544: recipe for target 'install_docs' failed
    make: *** [install_docs] Error 255
    

    可以暂时重命名/usr/bin/pod2man文件,重新编译

openssh

  • 解压源码,进入源码路径

  • 执行初始化,指定目标架构、编译依赖环境的路径、编译器

    ./configure --host=arm-none-linux --with-libs --with-zlib=/home/xxx/work/ssh_for_arm/lib/zlib-1.2.11 --with-ssl-dir=/home/xxx/work/ssh_for_arm/lib/openssl-0.9.8h --disable-etc-default-login CC=arm-none-linux-gnueabi-gcc AR=arm-none-linux-gnueabi-ar
    
  • make

  • 不要make install,因为编译出的ssh是arm版本的,在x86上也无法使用。

  • 生成key文件:分别执行,生成四个文件

    ssh-keygen -t rsa -f ssh_host_rsa_key -N ""
    ssh-keygen -t dsa -f ssh_host_dsa_key -N ""
    ssh-keygen -t ecdsa -f ssh_host_ecdsa_key -N ""
    ssh-keygen -t dsa -f ssh_host_ed25519_key -N ""
    

移植到目标板

复制所需的文件

在目标板上确保三个文件夹存在,如果没有就创建

/usr/libexec  
/usr/local/etc  
/usr/local/bin

需要复制到目标板/usr/local/bin/的文件

#在openssh源码目录下
scp 、sftp、ssh、sshd、ssh-add、ssh-agent、ssh-keygen、ssh-keyscan

需要复制到目标板/usr/local/etc/的文件

#在openssh源码目录下
moduli、ssh_config、sshd_config

#这四个key文件需要确保权限为600
ssh_host_rsa_key、ssh_host_dsa_key、ssh_host_ecdsa_key和ssh_host_ed25519_key

需要复制到目标板/usr/libexec/的文件

#在openssh源码目录下
sftp-server、ssh-keysign

修改目标板卡配置

在/etc/passwd文件增加两行

sshd:x:74:74:Privilege-separated
SSH:/var/empty/sshd:/sbin/nologin

开启sshd

在目标板上执行

/usr/local/bin/sshd
#sshd可能需要库libz.*和libcrypto.*。一般文件系统里会有

将上述命令设置到开机启动

posted @ 2021-09-20 13:07  WuYunTaXue  阅读(767)  评论(0编辑  收藏  举报