编译环境:Ubuntu16.04 64位
交叉编译工具:arm-hisiv500-linux-gcc

1. 源码交叉编译

openssh依赖zlib和openssl,我这里下载的是zlib-1.2.8.tar.gz、openssl-1.0.2p.tar.gz和openssh-7.9p1.tar.gz,之前使用openssl-1.1.1.tar.gz,最后在配置openssh时候总是报错缺少头文件,找了很多资料都没有解决,放弃,最后使用openssl-1.0.2p.tar.gz编译。

1.1 交叉编译zlib-1.2.8.tar.gz

prefix=/home/jerry/work/openssh/zlib.install CC=arm-hisiv500-linux-gcc ./configure --static
make
make install

这里为了避免最后需要拷贝动态库,只编译生成zlib的静态库.a,使用了–static选项,arm-hisiv500-linux-gcc是我使用的交叉编译工具,/home/jerry/work/openssh/zlib.install是最后make install的目录。

1.2 交叉编译openssl-1.0.2p.tar.gz

./Configure --prefix=/home/jerry/work/openssh/openssl.install os/compiler:arm-hisiv500-linux-gcc
make
make install

openssl-1.0.2p.tar.gz的Configure默认不生成动态库,openssl-1.1.1.tar.gz需要使用config配置,最终失败。

1.3 交叉编译openssh-7.9p1.tar.gz

./configure --host=arm-hisiv500-linux --with-libs --with-zlib=/home/jerry/work/openssh/zlib.install --with-ssl-dir=/home/jerry/work/openssh/openssl.install --disable-etc-default-login CC=arm-hisiv500-linux-gcc AR=arm-hisiv500-linux-ar
make

注意:不需要make install

2. 打包可执行程序

cp scp  sftp  ssh  ssh-add  ssh-agent  ssh-keygen  ssh-keyscan ../exe/usr/local/openssh/bin
cp sshd_config ssh_config ../exe/usr/local/openssh/etc
cp sshd ../exe/sbin
cp sftp-server  ssh-keysign ../exe/usr/local/libexec
cd ../exe/bin
ln -s /usr/local/openssh/bin/scp
ln -s /usr/local/openssh/bin/sftp
ln -s /usr/local/openssh/bin/ssh
ln -s /usr/local/openssh/bin/ssh-add
ln -s /usr/local/openssh/bin/ssh-agent
ln -s /usr/local/openssh/bin/ssh-keygen
ln -s /usr/local/openssh/bin/ssh-keyscan
cd ../
tar zcvf openssh_exe.tar.gz ./

目录结构图如下:
目录结构图

3. 移植到开发板

openssh_exe.tar.gz直接解压到开发板的根目录。
新增组1000和用户sshd,如下:

新增用户和密码及用户根目录,默认shell

这里/home/sshd最后会被当作sshd用户的根目录,权限很重要!!!
/home权限如下:
drwxr-xr-x 3 root root 0 Jan 1 00:06 home
/home/sshd权限如下:
drwxr-xr-x 3 root root 0 Jan 1 00:15 sshd
新增/home/sshd/test权限如下,用作测试:
drwxrwxrwx 2 sshd sshd 0 Jan 1 01:26 test
修改sshd用户密码

~ # passwd sshd
Changing password for sshd
New password: 
Bad password: too short
Retype password: 
Password for sshd changed by root

生成密钥

~ # cd /usr/local/etc/
/usr/local/etc # ssh-keygen -t rsa -f ssh_host_rsa_key -N ""
/usr/local/etc # ssh-keygen -t dsa -f ssh_host_dsa_key -N ""
/usr/local/etc # ssh-keygen -t ecdsa -f ssh_host_ecdsa_key -N ""
/usr/local/etc # ssh-keygen -t dsa -f ssh_host_ed25519_key -N ""

编辑配置文件,/usr/local/openssh/etc/sshd_config留作备份,实际使用的/usr/local/etc/sshd_config

/usr/local/etc # cp /usr/local/openssh/etc/sshd_config .
usr/local/etc # ls -lh
/usr/local/etc # ls -lh
total 11
-rw-------    1 root     root        1.3K Jan  1 00:12 ssh_host_dsa_key
-rw-r--r--    1 root     root         601 Jan  1 00:12 ssh_host_dsa_key.pub
-rw-------    1 root     root         505 Jan  1 00:12 ssh_host_ecdsa_key
-rw-r--r--    1 root     root         173 Jan  1 00:12 ssh_host_ecdsa_key.pub
-rw-------    1 root     root         399 Jan  1 00:12 ssh_host_ed25519_key
-rw-r--r--    1 root     root          93 Jan  1 00:12 ssh_host_ed25519_key.pub
-rw-------    1 root     root        1.8K Jan  1 00:11 ssh_host_rsa_key
-rw-r--r--    1 root     root         393 Jan  1 00:11 ssh_host_rsa_key.pub
-rw-r--r--    1 root     root        3.4K Jan  1 00:01 sshd_config

sshd_config修改如下:

PermitRootLogin yes
HostKey /usr/local/etc/ssh_host_rsa_key
HostKey /usr/local/etc/ssh_host_ecdsa_key
HostKey /usr/local/etc/ssh_host_dsa_key
HostKey /usr/local/etc/ssh_host_ed25519_key

PubkeyAuthentication yes

PasswordAuthentication yes
PermitEmptyPasswords no

AllowUsers sshd root

Subsystem sftp internal-sftp

Match group 1000
  ChrootDirectory /home/sshd
  X11Forwarding no
  AllowTcpForwarding no
  ForceCommand internal-sftp

4. 运行sshd

/usr/local/etc # /sbin/sshd

可以加入到启动脚本中运行,不需要加&后台。

5. 远程测试

在PC或者虚拟机上测试

5.1 root用户ssh

jerry@ubuntu:~/work/openssh/exe$ ssh root@192.168.1.10
root@192.168.1.10's password: 
Welcome to HiLinux.
None of nfsroot found in cmdline.
~ # 
~ # pwd
/root
~ # exit
Connection to 192.168.1.10 closed.

5.2 sshd用户ssh

jerry@ubuntu:~/work/openssh/exe$ ssh sshd@192.168.1.10
sshd@192.168.1.10's password: 
sh: using fallback suid method
Welcome to HiLinux.
~ $ pwd
/home/sshd
~ $ exit
Connection to 192.168.1.10 closed.

5.3 root用户sftp

jerry@ubuntu:~/work/openssh/exe$ sftp root@192.168.1.10
root@192.168.1.10's password: 
Connected to 192.168.1.10.
sftp> pwd
Remote working directory: /root
sftp> exit

5.4 sshd用户sftp

jerry@ubuntu:~/work/openssh/exe$ sftp sshd@192.168.1.10
sshd@192.168.1.10's password: 
Connected to 192.168.1.10.
sftp> pwd
Remote working directory: /home/sshd
sftp> cd test/
sftp> ls
sshd_config  
sftp> get sshd_config 
Fetching /home/sshd/test/sshd_config to sshd_config
/home/sshd/test/sshd_config 100% 3481     3.4KB/s   00:00
sftp> rm sshd_config 
Removing /home/sshd/test/sshd_config
sftp> put sshd_config
Uploading sshd_config to /home/sshd/test/sshd_config
sshd_config 100% 3481     3.4KB/s   00:00
sftp> exit

注意:如果登录过程提示Couldn’t open /dev/null: Permission denied,检查开发板中/dev/null权限,chmod 666 /dev/null,添加写入权限。mknod -m 666 /dev/null c 1 3

以上流程测试通过。

转载请注明出处,如有错漏之处,敬请指正。

Logo

更多推荐