AM335X 嵌入式Linux系统搭建ssh server

一,环境准备

  1.Ubuntu 虚拟机:22.04

   2.AM335X 开发板

二, 编译链官网及下载地址

  Linaro GCC 编译器arm-linux-gnueabihf 4.9版本

三,安装编译器
   1. Ubuntu新建文件夹用于保存下载交叉编译器

1 /Desktop$ makdir linux &&cd linux
2 :~/Desktop$ wget https://releases.linaro.org/components/toolchain/binaries/4.9-2017.01/arm-linux-gnueabihf/gcc-linaro-4.9.4-2017.01
3 sudo mkdir /usr/local/arm
4 sudo chmod 777 /usr/local/arm
5 tar -vxf gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf.tar.xz -C /usr/local/arm

  2.配置环境变量  

1 :~/Desktop$ sudo vi /etc/profile
2 在末尾添加以下内容
3 export PATH=$PATH://usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin
4 :~/Desktop$ source /etc/profile

 3.安装其他库

sudo apt-get install lsb-core lib32stdc++6

4.验证编译器是否安装成功(查看版本号命令)

arm-linux-gnueabihf-gcc -v

 

 四.交叉编译Zlib,openssl,openssh

1.获取 版本:zlib-1.2.13,openssl-1.0.1g,openssh-6.0p1  

zlib:
https://www.zlib.net/zlib-1.2.13.tar.gz

openssl:
https://www.openssl.org/source/openssl-1.0.2g.tar.gz

openssh:
https://mirrors.aliyun.com/pub/OpenBSD/OpenSSH/portable/openssh-6.0p1.tar.gz?spm=a2c6h.25603864.0.0.686840adwV8SAL

将下载好的压缩包放在虚拟机根目录 

2.交叉编译

  交叉编译openssl 

1 mkdir ssh
2 cd openssl-1.0.2g
3 sd@2023:~/Desktop/debugssh/openssl-1.0.2g$ ./config --prefix=/home/sd/Desktop/ssh/openssl.install  os/compiler:arm-linux-gnueabihf-gcc
4 make
5 make install

 

 需要改成:

./config --prefix=/home/sd/Desktop/ssh/openssl.install  os/compiler:arm-linux-gnueabihf-gcc

交叉编译zlib

tar zxvf zlib-1.2.13.tar.gz
cd /zlib-1.2.13
./configure --prefix=/home/sd/Desktop/debugssh/zlib-1.2.13

#修改Makefile中的某些变量值,指定自己的交叉编译工具(本人编译器:gcc-linaro-4.9.4)

1 CC=arm-linux-gnueabihf-gcc
2 CPP=arm-linux-gnueabihf-g++
3 AR=arm-linux-gnueabihf-ar
4 LDSHARED=arm-linux-gnueabihfi-gcc
5 保存退出执行
1 make
2 make install

 交叉编译openssh

1 tar zxvf openssh-6.0p1.tar.gz
2 cd /openssh-6.0p1
3 sd@2023:~/Desktop/debugssh/openssh-6.0p1$ ./configure --host=arm-linux-gnueabihf --with-libs --with-zlib=/home/sd/Desktop/debugssh/zlib-1.2.13 --with-ssl-dir=/home/sd/Desktop/ssh/openssl.install --disable-etc-default-login CC=arm-linux-gnueabihf-gcc AR=arm-linux-gnueabihf-gcc-ar

 

 make 

 

目标板操作

 1.复制文件从ubuntu到linux 目标板

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

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

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

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

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

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

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

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

2.生成证书/密码对(note:仅需要一次)在目标板上生成:      

root@am335x:/usr/local/etc#pwd
/usr/local/etc
root@am335x:/usr/local/etc#../bin/ssh-keygen -t dsa -f ssh_host_dsa_key -N ""
root@am335x:/usr/local/etc#../bin/ssh-keygen -t rsa -f ssh_host_rsa_key -N ""
root@am335x:/usr/local/etc#../bin/ssh-keygen -t rsa1 -f ssh_host_key -N ""
root@am335x:/usr/local/etc#../bin/ssh-keygen -t ecdsa -f ssh_host_ecdsa_key -N ""
修改权限:chmod 0600 ssh_host_dsa_key ssh_host_key ssh_host_rsa_key ssh_host_ecdsa _key
root@am335x:/usr/local/etc#ls -l total 180 -rw-r--r-- 1 root root 136218 Jan 1 01:06 moduli -rw-r--r-- 1 root root 1630 Jan 1 18:24 ssh_config -rw------- 1 root root 668 Jan 1 18:12 ssh_host_dsa_key -rw-r--r-- 1 root root 601 Jan 1 18:12 ssh_host_dsa_key.pub -rw------- 1 root root 227 Jan 1 18:13 ssh_host_ecdsa_key -rw-r--r-- 1 root root 173 Jan 1 18:13 ssh_host_ecdsa_key.pub -rw------- 1 root root 976 Jan 1 18:09 ssh_host_key -rw-r--r-- 1 root root 641 Jan 1 18:09 ssh_host_key.pub -rw------- 1 root root 1679 Jan 1 18:10 ssh_host_rsa_key -rw-r--r-- 1 root root 393 Jan 1 18:10 ssh_host_rsa_key.pub -rw-r--r-- 1 root root 3395 Jan 1 18:37 sshd_config

3.修改启动脚本

   --使用命令 passwd 配置 root 用户密码,若 /etc 文件夹中没有 shadow 文件,密码会直接存在 /etc/passwd 文件中

root@am335x:/etc#cat passwd
daemon:x:1:1:daemon:/usr/sbin:/bin/false
bin:x:2:2:bin:/bin:/bin/false
sys:x:3:3:sys:/dev:/bin/false
sync:x:4:100:sync:/bin:/bin/sync
mail:x:8:8:mail:/var/spool/mail:/bin/false
www-data:x:33:33:www-data:/var/www:/bin/false
operator:x:37:37:Operator:/var:/bin/false
nobody:x:65534:65534:nobody:/home:/bin/false
dbus:x:100:101:DBus messagebus user:/run/dbus:/bin/false
sshd:*:74:74:Privilege-separated SSH:/var/empty/sshd:/usr/sbin/nologin
root:x:0:0:root:/root:/bin/sh

---在嵌入式系统中添加 /var/empty 文件夹,其所有者需要是 root 用户,若不是,需使用命令修改

chown -R root:root /var/empty

---修改sshd_config

# HostKey for protocol version 1
HostKey /usr/local/etc/ssh_host_key
# HostKeys for protocol version 2
HostKey /usr/local/etc/ssh_host_rsa_key
HostKey /usr/local/etc/ssh_host_dsa_key
HostKey /usr/local/etc/ssh_host_ecdsa_key

PermitRootLogin    yes―――――允许根用户登陆
PermitEmptyPasswords yes――――允许使用空密码
UsePrivilegeSeparation   no――――把安全级别降低,因为不会连接互联网
Subsystem	sftp	/usr/local/libexec/sftp-server

--- /etc/passwd 文件末尾添加

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

完成上述步骤,可使用 /usr/local/bin/sshd 命令运行   

root@am335x:/usr/sbin#/usr/local/bin/sshd &
[1] 2447

PC通过SSH链接开发板,显示成功连接上:

ps命令,查看sshd已正常启动 ,可通过客户端进行ssh连接

 

 

 

 

         

posted @ 2023-03-08 11:13  dwtmjps  阅读(454)  评论(0编辑  收藏  举报