查看本机硬件设备信息:常见的几个命令

 1、cat /proc/cpuinfo 查看cpu核心的信息

 2、lsusb   查看USB设备信息

 3、lspci   查看pci设备信息

 4、hal-device 查看所有硬件设备的详细信息

 

Linux系统裁剪笔记之四busybox

1.什么裁剪?

本篇文章的主要目的是让笔者和读者更深的认识Linux系统的运作方式,大致内容就是把Linux拆开自己一个个组件来组装,然后完成一个微型的Linux系统.下面,让我们来实现吧..写的不好的地方请指教.


2.原理

大家都知道,操作系统的启动流程是(主要是Linux):POST—>BIOS—>MBR—kernel-->initrd-->sbin/init,

POST,BIOS都不是我们管的,所以这里我们从MBR开始,Linux的系统引导先主要是用的grub这个软件,grub引导系统了,然后启动内核,内核调用initrd来实现最基本的操作系统.


3.实验 (所有操作均在虚拟机宿主机为redhat 5.4上实现,且定义硬盘时选IDE)


Kernel + initrd(busybox制作,提供ext3文件系统模块) + ROOTFS (busybox制作)

    硬件驱动:initrd

      initrd: 仅需要提供内核访问真正的根文件系统所在设备需要的驱动

           存储设备和文件系统相关的模块

      系统初始化rc.sysinit: 初始其它硬件的驱动程序;

       

    ROOTFS: busybox,init不支持运行级别

           /etc/inittab:格式也不尽相同 ash,hush  bash


如何编译busybox:

  编译前准备

   busybox下载:  (下载地址:https://busybox.net/)

    下载busybox:busybox-1.20.2.tar.bz2

   kernel下载:linux-2.6.38.tar.bz2

  准备一个IDE硬盘分成2个分区

   IDE:

      /dev/hda1: ext3 /boot

      /dev/hda2: ext3 /

    在宿主虚拟机添加IDE硬盘,并分2个分区,并挂载:

       mount /dev/hda1 /mnt/boot

       mount /dev/hda2 /mnt/sysroot


步骤如下:

[root@localhost ~]# ls

anaconda-ks.cfg  Desktop  etc   install.log   lib   sbin  sysrootv  bin  

busybox-1.20.2.tar.bz2  dev   init  install.log.syslog  proc  sys   testiso

[root@localhost ~]# tar -xvf busybox-1.20.2.tar.bz2

[root@localhost ~]# cd busybox-1.20.2

[root@localhost busybox-1.20.2]# make menuconfig  此命令驱动图形编译界面如下步骤:

wKiom1hQ_3PwfOlnAAIkA_t7t0M822.jpg-wh_50

敲回车进入

wKioL1hRACDgqiQSAAFpuRP0-fU356.jpg-wh_50 

选择第一选项

wKiom1hRAMvzP8wXAAPSFCRS2r8708.jpg-wh_50

其他选项可以保持默认不修改,保存退出 

先把内核文件下载好后解压 linux-2.6.38.tar.bz2 并复制里面的linux-2.6.38/include/mtd/ubi-user.h,否则编译安装busybox会报错误。

[root@localhost ~]# tar xf linux-2.6.38.tar.bz2 -C /usr/src   #解压内核文件到指定目录/usr/src

[root@localhost ~]#ls /usr/src

kernels  linux-2.6.38  redhat

[root@localhost ~]# ls /usr/src/linux-2.6.38/include/mtd

inftl-user.h  Kbuild  mtd-abi.h  mtd-user.h  nftl-user.h  ubi-user.h

[root@localhost ~]# cd /root/busybox-1.20.2

[root@localhost busybox-1.20.2]# ls

applets  configs  editors    libbb   Makefile.custom  printutils  shell

applets_sh  console-tools  examples   libpwdgrp   Makefile.flags   procps     sysklogd  arch   coreutils  findutils  LICENSE  Makefile.help    README     testsuite  archival    debianutils    include    loginutils  miscutils       runit   TODOAUTHORS     docs    init    mailutils   modutils    scripts   TODO_unicode  Config.in   e2fsprogs      INSTALL    Makefile    networking     selinux     util-linux

[root@localhost busybox-1.20.2]# cd include/

[root@localhost include]# ls

applet_metadata.h  autoconf.h   dump.h   libbb.h  rtc_.h   usage.src.h

applets.h  bb_archive.h   fix_u32.h   liblzo_interface.h  shadow_.h  volume_id.h

applets.src.h  bb_e2fs_defs.h  grp_.h   platform.h   unicode.h  xatonum.h

ar.h   busybox.h   inet_common.h  pwd_.h    usage.h    xregex.h

[root@localhost include]# mkdir mtd

[root@localhost include]# cp /usr/src/linux-2.6.38/include/mtd/ubi-user.h mtd/ 

[root@localhost include]# ls

applet_metadata.h  bb_archive.h  dump.h   mtd   unicode.h     xregex.h

applets.h   bbconfigopts_bz2.h  fix_u32.h   NUM_APPLETS.h  usage_compressed.h

applets.src.h      bbconfigopts.h      grp_.h   platform.h     usage.h

applet_tables.h    bb_e2fs_defs.h      inet_common.h       pwd_.h         usage.src.h  ar.h    busybox.h      libbb.h    rtc_.h    volume_id.h

autoconf.h    config     liblzo_interface.h  shadow_.h    xatonum.h

[root@localhost include]# ls mtd

ubi-user.h

[root@localhost include]# pwd

/root/busybox-1.20.2/include

[root@localhost include]# cd ..

[root@localhost busybox-1.20.2]# ls

applets     configs   editors    libbb    Makefile.custom  printutils  shell

applets_sh  console-tools  examples   libpwdgrp   Makefile.flags   procps     sysklogd  arch   coreutils   findutils  LICENSE     Makefile.help    README     testsuite  archival    debianutils    include    loginutils  miscutils        runit  TODO  AUTHORS     docs     init       mailutils   modutils         scripts   TODO_unicode  Config.in   e2fsprogs   INSTALL    Makefile    

networking   selinux     util-linux

[root@localhost busybox-1.20.2]# pwd

/root/busybox-1.20.2

[root@JackTest busybox-1.20.2]# make install   #编译成功后生成_install文件

 ./_install//usr/sbin/ubiupdatevol -> ../../bin/busybox

  ./_install//usr/sbin/udhcpd -> ../../bin/busybox



--------------------------------------------------

You will probably need to make your busybox binary

setuid root to ensure all configured applets will

work properly.

--------------------------------------------------


[root@localhost busybox-1.20.2]# ls

applets   busybox_unstripped.map  e2fsprogs  libbb  Makefile.help  scripts

applets_sh   busybox_unstripped.out  editors   libpwdgrp  miscutils     selinux  arch   Config.in    examples   LICENSE   modutils       shell  archival    configs   findutils  loginutils   networking     sysklogd   AUTHORS             console-tools  include    mailutils   printutils     testsuite

busybox   coreutils    init    Makefile    procps  TODO   util-linux

busybox.links    debianutils   _install   Makefile.custom  README         TODO_unicode   busybox_unstripped  docs  INSTALL    Makefile.flags   runit       

[root@localhost busybox-1.20.2]# ll _install   #查看_install文件信息
total 28
drwxr-xr-x 2 root root 4096 2016-11-29 13:50 bin
lrwxrwxrwx 1 root root   11 2016-11-29 13:50 linuxrc -> bin/busybox
drwxr-xr-x 2 root root 4096 2016-11-29 13:51 sbin
drwxr-xr-x 4 root root 4096 2016-11-29 13:51 usr
[root@localhost busybox-1.20.2]# du -sh _install

3.0M    _install     #initrd,fsfiles

[root@localhost busybox-1.20.2]# cp _install  /tmp/busybox -a  #一份用于initrd 

[root@localhost busybox-1.20.2]# cp _install  /tmp/busybox2 -a #一份用于rootfs 

[root@localhost busybox-1.20.2]# cd /tmp/busybox
[root@localhost busybox]# ll
total 28
drwxr-xr-x 2 root root 4096 2016-11-29 13:50 bin
lrwxrwxrwx 1 root root   11 2016-11-29 13:58 linuxrc -> bin/busybox
drwxr-xr-x 2 root root 4096 2016-11-29 13:51 sbin
drwxr-xr-x 4 root root 4096 2016-11-29 13:51 usr
[root@JackTest busybox]# rm linuxrc 
rm: remove symbolic link `linuxrc'? y

[root@localhost busybox]# mkdir proc sys etc dev tmp mnt/sysroot lib/modules sysroot -pv

[root@localhost busybox]# modinfo ext3    #复制ext3依赖的模块 
filename:       /lib/modules/2.6.23.1-42.fc8/kernel/fs/ext3/ext3.ko
...
depends:        jbd

[root@localhost busybox]# modinfo jbd
filename:       /lib/modules/2.6.23.1-42.fc8/kernel/fs/jbd/jbd.ko

[root@localhost busybox]# cp /lib/modules/2.6.23.1-42.fc8/kernel/fs/ext3/ext3.ko lib/modules/

[root@localhost busybox]# cp /lib/modules/2.6.23.1-42.fc8/kernel/fs/jbd/jbd.ko lib/modules/

[root@localhost busybox]# vim init
#!/bin/sh     

mount -t proc proc /proc
mount -t sysfs sysfs /sys

mdev -s

insmod /lib/modules/jbd.ko
insmod /lib/modules/ext3.ko

mount -t ext3 /dev/hda2 /mnt/sysroot

exec switch_root  /mnt/sysroot   /sbin/init     #将/mnt/sysroot根权限转交给/sbin/init

[root@localhost busybox]# chmod +x init 

[root@localhost busybox]# ls

bin  dev  etc  init  lib  proc  sbin  sys  sysroot  usr

[root@localhost busybox]# mkdir mnt/sysroot -pv

mkdir: 已创建目录 “mnt”

mkdir: 已创建目录 “mnt/sysroot”

[root@localhost busybox]# ls

bin  dev  etc  init  lib  mnt  proc  sbin  sys  sysroot  usr

[root@localhost busybox]# rm -rf sysroot

[root@localhost busybox]# ls

bin  dev  etc  init  lib  mnt  proc  sbin  sys  usr

[root@localhost busybox]# mknod dev/console c 5 1    #新增console模块 
[root@localhost busybox]# mknod dev/null c 1 3       #新增null模块

[root@localhost busybox]# tree dev
dev
|-- console
`-- nullmdev -s 

[root@localhost busybox]# mkdir tmp

[root@localhost busybox]# ls

bin  dev  etc  init  lib  mnt  proc  sbin  sysroot  tmp usr 

[root@localhost busybox]# find . |cpio -H newc --quiet -o |gzip -9 > /mnt/boot/initrd.gz

[root@localhost busybox]# ls -lh /mnt/boot/initrd.gz 

-rw-r--r-- 1 root root 1.2M 12-15 08:55 /mnt/boot/initrd.gz

[root@localhost busybox]# cp /boot/vmlinuz-2.6.18-164.el5 /mnt/boot/vmlinuz #复制内核

[root@localhost busybox]# ls -lh /mnt/boot

总计 3.1M

-rw-r--r-- 1 root root 1.2M 12-15 08:55 initrd.gz

drwx------ 2 root root  12K 12-15 08:53 lost+found

-rw-r--r-- 1 root root 1.9M 12-15 08:57 vmlinuz

[root@localhost busybox]# grub-install --root-directory=/mnt /dev/hda #安装grub

Probing devices to guess BIOS drives. This may take a long time.

Installation finished. No error reported.

This is the contents of the device map /mnt/boot/grub/device.map.

Check if this is correct or not. If any of the lines is incorrect,

fix it and re-run the script `grub-install'.


(fd0)/dev/fd0

(hd0)/dev/hda   #此系统创建所需的引导程序

(hd1)/dev/sda

[root@localhost busybox]# ls /mnt/boot

grub  initrd.gz  lost+found  vmlinuz

[root@localhost busybox]# vi /mnt/boot/grub/grub.conf 
default=0
timeout=5
title John MiniLinux(2.6.18)
    root(hd0,0)
    kernel /vmlinuz ro root=/dev/hda2
    initrd /initrd.gz

[root@localhost busybox]# ls /mnt/boot

grub  initrd.gz  lost+found  vmlinuz


  二、系统初始化rc.sysinit: 初始其它硬件的驱动程序

[root@localhost busybox]# cd 

[root@localhost ~]# cd busybox-1.20.2  切换到busybox的解压缩目录

[root@localhost busybox-1.20.2]# ls

applets  busybox_unstripped.map  e2fsprogs  libbb  Makefile.help scripts  applets_sh busybox_unstripped.out  editors    libpwdgrp  miscutils  selinux

arch   Config.in   examples   LICENSE    modutils    shell

archival  configs   findutils  loginutils   networking   sysklogd

AUTHORS   console-tools  include    mailutils  printutils     testsuite

busybox  coreutils   init   Makefile   procps   TODO  util-linux

busybox.links   debianutils     _install   Makefile.custom  README         TODO_unicode  busybox_unstripped  docs   INSTALL    Makefile.flags   runit       

[root@localhost busybox-1.20.2]# cp _install/* /mnt/sysroot/ -a

[root@localhost busybox-1.20.2]# cd /mnt/sysroot/

[root@localhost sysroot]# ls

bin  linuxrc  lost+found  sbin  usr

[root@localhost sysroot]# rm linuxrc 

rm:是否删除 符号链接 “linuxrc”? y

[root@localhost sysroot]# ls

bin  lost+found  sbin  usr

[root@localhost sysroot]# mkdir proc sys dev tmp var/{log,lock,run} lib/modules etc/rc.d/init.d root boot mnt media -pv   #创建所需目录

[root@localhost sysroot]# ls

bin  boot  dev  etc  lib  lost+found  media  mnt  proc  root  sbin  sys  tmp usr  var

[root@localhost sysroot]# ls sbin/

acpid   fbsplash     hdparm     klogd     mkdosfs     nameif       setconsole syslogd  adjtimex    fdisk  hwclock    loadkmap  mke2fs      pivot_root   slattach    tunctl  arp   findfs       ifconfig   logread   mkfs.ext2   poweroff start-stop-daemon  udhcpc  blkid    freeramdisk  ifdown     losetup   mkfs.minix raidautorun  sulogin   vconfig   blockdev    fsck  ifenslave  lsmod    mkfs.vfat reboot   swapoff  watchdog  bootchartd  fsck.minix   ifup   makedevs  mkswap     rmmod   swapon zcip  depmod      getty   init     man       modinfo     route switch_root  devmem  halt   insmod     mdev     modprobe    runlevel   sysctl

[root@localhost sysroot]# vim etc/inittab

::sysinit:/etc/rc.d/rc.sysinit

console::respawn:-/bin/sh      

::ctrlaltdel:/sbin/reboot

::shutdown:/bin/umount -a -r

[root@test busybox2]# vim etc/fstab

sysfs      /sys     sysfs    defaults       0 0

proc       /proc    proc    defaults       0 0

/dev/hda1   /boot    ext3    defaults       0 0

/dev/hda2   /       ext3    defaults       1 1

[root@localhost sysroot]# sync  把内容同步至硬盘中以防丢失

[root@localhost sysroot]# mknod dev/console c 5 1  #新增console模块 

[root@localhost sysroot]# mknod dev/null c 1 3    #新增null模块 

[root@localhost sysroot]# ll dev/
total 0
crw-r--r-- 1 root root 5, 1 Nov 29 15:42 console
crw-r--r-- 1 root root 1, 3 Nov 29 15:42 null

[root@localhost sysroot]# vim etc/rc.d/rc.sysinit
#!/bin/sh      

#


echo -e "\tWelcome to  \033[34mJohn Mini \033[0m Liniux"
echo -e "Remounting the root filesystem .................[  \033[32mOK\033[0m  ]"
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -o  remount,rw  / 

echo -e "Creating the files of device ...................[  \033[32mOK\033[0m  ]"
mdev -s 

echo -e "Mounting the filesystem .......................[  \033[32mOK\033[0m  ]"
mount -a
swapon -a
[root@localhost sysroot]# chmod +x etc/rc.d/rc.sysinit

[root@localhost sysroot]# sync   #同步至硬盘中

[root@localhost sysroot]# sync

[root@localhost sysroot]# sync

至此一个精简版的linux系统就完成了

-----------------------------------------------------------------------------------


下面是通过脚本添加命令及模块用来完善系统功能的:

[root@localhost busybox]# ls /mnt/sysroot/
bin  boot  dev  etc  lib  lost+found  media  mnt  root  sbin  sys  tmp  usr  var
[root@localhost sysroot]# du -sh
4.0M    

[root@localhost boot]# du -sh
2.9M    

[root@localhost ~]# ./cpbin.sh  #cpbin脚本在文章最下面有附带脚本
Your command: bash

wKiom1hTfd6hy7IfAAMzY2OREkc378.jpg-wh_50


执行过cpbin脚本复制过bash命令后需更改配置文档/mnt/sysroot/etc/inittab,把/bin/sh改为/bin/bash

或者将/mnt/sysroot/bin目录下面的的sh先删除掉,再把/mnt/sysroot/bin/bash链接为当前目录下的sh

两种方法都可以,但是建议用第一种修改配置文档的方式,不建议删除链接后再重新链接。

[root@localhost bin]# rm sh  先删除/mnt/sysroot/bin目录下的软链接sh

rm:是否删除 符号链接 “sh”? y

[root@localhost bin]# ln -sv bash sh  重新建立一个sh软链接指向当前目录的bash

创建指向“bash”的符号链接“sh”

[root@localhost bin]# ll

总计 3172

-rwxr-xr-x 1 root root  801512 12-16 09:08 bash

-rwxr-xr-x 1 root root 2025496 12-15 13:43 busyboxx

lrwxrwxrwx 1 root root       4 12-16 13:56 sh -> bash


-----------------------------------------------------------------------------------


增加网络功能

[root@localhost ~]# modinfo mii    查看网卡模块需要的lib文档

filename:       /lib/modules/2.6.18-164.el5/kernel/drivers/net/mii.ko

license:        GPL

description:    MII hardware support library

author:         Jeff Garzik <jgarzik@pobox.com>

srcversion:     16DCEDEE4B5629C222C352D

depends:        

vermagic:       2.6.18-164.el5 SMP mod_unload gcc-4.1

[root@localhost ~]# modinfo pcnet32  查看网卡模块需要的lib文档

filename:       /lib/modules/2.6.18-164.el5/kernel/drivers/net/pcnet32.ko

license:        GPL

description:    Driver for PCnet32 and PCnetPCI based ethercards

author:         Thomas Bogendoerfer

[root@localhost ~]# cp  /lib/modules/2.6.18-164.el5/kernel/drivers/net/mii.ko /mnt/sysroot/lib/modules/

[root@localhost ~]# cp /lib/modules/2.6.18-164.el5/kernel/drivers/net/pcnet32.ko /mnt/sysroot/lib/modules/

[root@localhost ~]# ls /mnt/sysroot/lib

modules

[root@localhost ~]# ls /mnt/sysroot/lib/modules/

mii.ko  pcnet32.ko

#[root@test sysroot]# modinfo e1000    #新增启动网络模块,请确认网卡型号

#[root@test sysroot]#cp /lib/modules/2.6.18-164.el5PAE/kernel/drivers/net/e1000/e1000.ko lib/modules

[root@test sysroot]# vim /mnt/sysroot/etc/rc.d/init.d/network  (参考:Linux系统裁剪笔记之二insmod)

[root@test sysroot]# vim etc/rc.d/rc.sysinit  

...  #新增以下内容

echo "Initalizing network device ..."
/sbin/insmod /lib/modules/e1000.ko
[ $? -eq 0 ] && success "Initializing network device" || failure "Initializing network device"   
ifconfig lo 127.0.0.1/8
[ $? -eq 0 ] && success "Activating loopb

ack network" || failure "Activating loopback network"

ifconfig eth0 10.119.131.208/22 up
[ $? -eq 0 ] && success "Activating eth0 network" || failure "Activating eth0 network"



4、在系统登录时提供banner信息

# vi /mnt/sysroot/etc/issue

Welcome to Jack Linux XXXXX62 ...
Kernel \r

wKiom1g_x8PR5nNUAAFmc6Vki2M536.png-wh_50


5、在系统登录时显示用户信息

[root@test sysconfig]# cat /mnt/sysroot/etc/sysconfig/network 
HOSTNAME=Jack.test.com

[root@test etc]# vim rc.d/rc.sysinit 

... #新增以下内容

echo "Set the hostname..."
[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network
[ -z $HOSTNAME -o "HOSTNAME" == '(none)' ] && HOSTNAME=localhost
/bin/hostname $HOSTNAME
[ $? -eq 0 ] && success "Set the hostname" || failure "Set the hostname"

wKiom1g_zDmAiwlfAAF2zV1UfGs577.png-wh_50


6、在系统登录时提供日志功能

[root@test sysroot]# vim etc/rc.d/rc.sysinit 

... #新增以下内容

echo "Strat syslog..."
syslogd
[ $? -eq 0 ] && success "Start syslog" || failure "Start syslog"
klogd
[ $? -eq 0 ] && success "Start klog" || failure "Start klog"

[root@test sysroot]# cat etc/syslog.conf 
*.info;auth.none        /var/log/messages
auth.*                /var/log/secure

[root@test sysroot]# ll var/log    #新建以下两个文件,且改为600权限 
total 48
-rw------- 1 root root 47913 Dec  5 15:46 messages
-rw------- 1 root root     0 Dec  5 15:50 secure

wKioL1hFIaSBpMstAAITzxIZ1_8165.png-wh_50

7、在系统登录时提供SSH功能

[root@test sysroot]# cat etc/shells 
/bin/sh
/bin/bash
/bin/hush
/bin/ash
[root@test sysroot]# vim etc/fstab 
[root@test sysroot]# cat etc/fstab 
sysfs       /sys   sysfs   defaults        0 0
proc        /proc  proc    defaults        0 0
devpts        /dev/pts devpts mode=620       0 0
/dev/hda1   /boot  ext3    defaults        0 0
/dev/hda2   /      ext3    defaults        1 1
[root@test sysroot]# ls
bin  boot  dev  etc  lib  lost+found  media  mnt  proc  root  sbin  sys  tmp  usr  var
[root@test sysroot]# mkdir dev/pts


[root@test sysroot]# cat etc/shells 
/bin/sh
/bin/bash
/bin/hush
/bin/ash
[root@test sysroot]# cat etc/fstab 
sysfs       /sys   sysfs   defaults        0 0
proc        /proc  proc    defaults        0 0
devpts        /dev/pts devpts mode=620       0 0
/dev/hda1   /boot  ext3    defaults        0 0
/dev/hda2   /      ext3    defaults        1 1

[root@test sysroot]# mkdir etc/dropbear
[root@test sysroot]# dropbearkey -t rsa -f /mnt/sysroot/etc/dropbear/dropbear_rsa_host_key -s 2048
Will output 2048 bit rsa secret key to '/mnt/sysroot/etc/dropbear/dropbear_rsa_host_key'
Generating key, this may take a while...
Public key portion is:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAnGoBr5U1wMb0bsNmBC5PsX+e0tTc9j+UY/T/9cG2SrBbUcLZ/+vlHFmmrxMr3N/hJZBEFHwDoOxffnx6v7GhVFXlcWhNajq7DuSdLqCmknKAd/BzYU/61iQKTqGECLW7cg0sF30NE65cquumEfctUVbIhZ4m33B6NqBBIRUz/3pujbsmOoIAXnpIyVMKpQOHcDEER9dLC2JzBJ5iM2ZE7eaSW/Ddw3SYB3UrbE43A1xHZbXPpVSbr9GQS3r6DvgwFesUd1PPCLl8+5vq2EkCwWd3wiJkzhcI0TALyNaZnftPr3qQ2Bq3XEPnYMeA85ZRe65JRvSAoGqLy7XcPvSrLLLRw== root@test
Fingerprint: md5 3c:04:cf:fd:8c:89:a5:a8:3e:a8:90:bc:22:56:91:1e

[root@test sysroot]# dropbearkey -t dss -f /mnt/sysroot/etc/dropbear/dropbear_dss_host_key
Will output 1024 bit dss secret key to '/mnt/sysroot/etc/dropbear/dropbear_dss_host_key'
Generating key, this may take a while...
Public key portion is:
ssh-dss AAAAB3NzaC1kc3MAAACBANqb58a9g4TENxtXPl4hjM2Gkgp+GHcDn8DYQRJQDdQLr0GwwoA3xJWrB1geAFUem9vDcpQ5/oa0tEr300IyXUhTaldGj9C5yqQXPsCoJZOLBr5FZCpbBJ9D7xljAVLMXJhL4yB4hDbR+U1keP9eHe2PuuamD7VN8obAfG0MP25XAAAAFQD/eG5E3tlk6wG7NXmGqKNrFTgbMwAAAIEAjBh2Omy+VwqMSp7K0X2LkzN95VYPK7HpWIzeVPscdKzU4USr+Z2axmYlztzk+BaFkdhh3GO8FitUNjLsjIoOoPtPBZ1u0mc3tetnoTtjjJTaRrXstnaW4tR0844PGYo8+CCMnN6tmVUTxefNuo68FhRVp14kjcS73nENm7prqtEAAACBAMfr2mi48f36s+Dl0g+o0TefmV5GpP7W4wEV3yXzN/2loJS8Sp1yL4JbdMykpJQeOc5vl/GWRveV1tuudW2wI30O22Srpb6KZaV8BnMRthJtfUo8IEOctu4dqZTazoIP02tHiUTps1nBxH1VF2ZI0Veun0oP9/FWEEgVbHsoLl5r root@test
Fingerprint: md5 bf:81:55:ea:64:b3:8d:80:b7:42:3a:2c:f0:0d:68:bc

[root@test sysroot]# ls etc/dropbear/
dropbear_dss_host_key  dropbear_rsa_host_key


[root@test sysroot]# mkdir usr/lib

[root@test sysroot]# cp -d /lib/libnss_files* /mnt/sysroot/lib/

[root@test sysroot]# cp -d /usr/lib/libnss3.so /usr/lib/libnss_files.so /mnt/sysroot/usr/lib/

[root@test sysroot]# cp /etc/nsswitch.conf ./etc/

[root@test sysroot]# ll usr/lib/ 
total 1242
-rwxr-xr-x 1 root root 1188804 Dec  6 10:23 libnss3.so
lrwxrwxrwx 1 root root      27 Dec  6 10:23 libnss_files.so -> ../../lib/libnss_files.so.2
-rwxr-xr-x 1 root root   75284 Dec  6 10:39 libz.so.1
[root@test sysroot]# ll lib/ 
total 1848
-rwxr-xr-x 1 root root  125736 Nov 30 15:11 ld-linux.so.2
-rwxr-xr-x 1 root root   45288 Dec  6 10:39 libcrypt.so.1
-rwxr-xr-x 1 root root 1611564 Nov 30 15:11 libc.so.6
-rwxr-xr-x 1 root root   16428 Nov 30 15:11 libdl.so.2
-rwxr-xr-x 1 root root   46680 Dec  6 10:22 libnss_files-2.5.so
lrwxrwxrwx 1 root root      19 Dec  6 10:22 libnss_files.so.2 -> libnss_files-2.5.so
-rwxr-xr-x 1 root root   13276 Nov 30 15:11 libtermcap.so.2
-rwxr-xr-x 1 root root   15164 Dec  6 10:39 libutil.so.1
drwxr-xr-x 2 root root    1024 Nov 30 16:29 modules

[root@test sysroot]# cat etc/nsswitch.conf
passwd:     files
shadow:     files
group:      files
hosts:      files dns

[root@test sysroot]# sync   

以下为启动监听出错时解决办法:

wKioL1hGQJKD2ef6AAGkBhmam38578.png-wh_50

[root@test sysroot]# vi /etc/rc.d/rc.sysinit 

... #新增以下内容

echo "Activating SSH Service ..."                                                           
/sbin/dropbear                                                                              
[ $? -eq 0 ] && success "Activated SSH service" || failure "Activated SSH service"  

[root@fox ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address  Foreign Address State PID/Program name   
tcp    0  0 0.0.0.0:22  0.0.0.0:*     LISTEN      522/dropbear
[root@test sysroot]# /bin/dbclient -l root 110.119.131.204 #进入该主机后远程连接其它PC


【附带cpbin命令复制脚本】


附1:自动复制相关库文件 

* FILE=/usr/local/src

TEST=${FILE#*/}:$TEST结果为usr/local/src, 以/为关键字,删除从左边数第一个/及关键字/左边的所有字符串。 

DEST=${FILE##*/}:$DEST结果为src,以/为关键字,删除最后一个关键字/及关键字/左边的所有字符串。


DOTEST=${FILE%/*}:$DOTEST结果为/usr/local,以/为关键字,删除从右边数第一个/及关键字/右边的所有字符串。

DODEST=${FILE%%/*}:$DODEST结果为空,以/为关键字,删除从右边数最后一个/及关键字/右边的所有字符串。


[root@localhost ~]# vim cpbin.sh

#!/bin/bash

# bincopy.sh

DEST=/mnt/sysroot

libcp() {     #该函数接收一个库文件路径

 LIBPATH=${1%/*} #红色部分将/lib/libc.so.6切割成/lib/   %:表示从右边数删除第一个/及其右边的字符串。

[ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH #路径是否存在

 [ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "copy lib $1 finished."#库文件是否存在 

}

 

bincp() {      #该函数接收一个命令对应的地址,如:/bin/ls 

 CMDPATH=${1%/*} #红色部分将/bin/ls切割成/bin/ 

  [ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH

  [ ! -e $DEST${1} ] && cp $1 $DEST$CMDPATH

 

  for LIB in  `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`; do

    libcp $LIB   # /.*lib绿色为:/开始,中间任意字符,接着lib 

  done        # \(64\)\{0,1\}红色为:64出现0或1次 
}            # [^[:space:]]\{1,\}绿色为:空格开关且至少出现一次


read -p "Your command: " CMD

until [ $CMD == 'q' ]; do

 ! which $CMD &> /dev/null && echo "Wrong command" && read -p "Input again:" CMD && continue

 COMMAND=`which $CMD |grep -v "^alias" |grep -o "[^[:space:]]\{1,\}"`#以alias开头的行不显示

 bincp $COMMAND

 echo"copy $COMMAND finished."

 read -p"Continue: " CMD

done