定制linux内核+Busybox+dropbear实现远程登录推荐

简单介绍下各阶段工作流程:

POST:开机后,加载BIOS信息(里面包含各硬件的相关信息)

BIOS(boot sequence):选择(设备)启动项,然后读取MBR信息

Boot Loader:初始化硬件、建立内存空间映射,读取grub配置文件

Initrd:加载内核(硬件检测及初始化、挂载根文件系统)0→启动第一个进程init→该程序读出/etc/inittab、/etc/rc.d/rc.sysinit、/etc/rc.d/rc.local文件

Shell:启动/bin/login程序,进入登录界面

进入实验部分:

一、环境搭建

1、虚拟机(server1)上添加一个硬盘

2、在系统中给该磁盘进行分区

[root@localhost~]#fdisk/dev/sdb

3、格式化

[root@localhost~]#mke2fs-text4/dev/sdb1[root@localhost~]#mke2fs-text4/dev/sdb2[root@localhost~]#mkswap/dev/sdb3

4、挂载

[root@localhost~]#mkdir/mnt/{sysroot,boot}#创建挂载目录[root@localhost~]#mount/dev/sdb1/mnt/boot/[root@localhost~]#mount/dev/sdb2/mnt/sysroot/

5、安装grub

[root@localhost~]#grub-install--root-directory=/mnt/dev/sdb

6、创建linux各目录

[root@localhost~]#cd/mnt/sysroot/[root@localhost~]#mkdir-pvetc/rc.dvar/logrootprocsyssrvbootmnttmphomedevliblib64

二、编译内核

[root@localhost~]#tarxflinux-3.13.6.tar.xz-C/usr/src/#解压[root@localhost~]#cd/usr/src/[root@localhostsrc]#ln-slinux-3.13.6linux#创建软链接[root@localhostsrc]#cdlinux[root@localhostlinux]#yumgroupinstall"DevelopmentTools"-y#安装开发包组[root@localhostlinux]#makeallnoconfig#重置配置选项[root@localhostlinux]#makemenuconfig

如果make menuconfig时报错:

提示:缺少ncurses-devel库文件

[root@localhostlinux]#yum-yinstallncurses-devel[root@localhostlinux]#makemenuconfig#再次进行

[*]64bitkernel#64位支持[*]gerernalsetup()localversion-appendtokernelrelease#版本号[*]Enableloadablemodualsupport#允许模块加载- ProgressortypeandfeaturesProcessorFamily(Core2/newerXeon)#自行选择处理器类型[*]Symmetricmulti-processingsupport#支持多核- BusOptions(PCIetc.)[*]PCIsupport#支持PCI总线- Filesystem[*]TheExtended4(ext)filesystem#支持ext4文件系统- Executablefileformats/Emulations#可执行文件系统[*]KernelsupportforELFbinaries#支持ELF二进制程序[*]Kernelsupportforscriptsstartingwith#!#支持脚本[*]Networkingsupport- Networkingoptions[*]Unixdomainsockets[*]UNIX:socketmonitoringinterface[*]TCP/IPnetworking[*]IP:multicasting#ip多播协议[*]IP:advancedrouter#高级路由协议[*]IP:kernellevelautoconfiguration#内核级别配置[*]IP:DHCPsupport#DHCP服务[*]IP:BOOTPsupport[*]IP:RARPSupport#RARP协议[*]IP:TCPsyncookiesupport#tcp同步状态- DeviceDrivers- GernalDriverOptions[*]Maintainadevtmpfsfilesystemtomountat/dev#使用devtmpfs机制挂载设备文件[*]Automountdevtmpfsat/dev,afterthekernelmountedtherootfs#自动挂载- SCSIdevicesupport[*]SCSIdeveicesupport[*]SCSIdisksupport[*]FusionMPTdevicesupport#支持虚拟磁盘[*]FusionMPTScsiHostdriversforSPI#虚拟磁盘[*]FusionMPTmiscdevice(ioctl)driver#磁盘初始化[*]Networkdevicesupport[*]Networkcoredriversupport#网络核心驱动[*]Ethernetdriversupport#以太网卡驱动[*]Inteldevices(NEW)[*]Intel(R)PRO/1000GigabitEthernetsupport[*]Intel(R)PRO/1000PCI-ExpressGigabitEthernetsupport- InputDevicesupport[*]Mouseinterface[*]Keyboards#键盘[*]Mice#ps/2[*]USBsupport[*]SupportforHost-sideUSB[*]xHCIHCD(USB2.0)support[*]EHCIHCD(USB3.0)support[*]OHCIHCD(USB1.1)support

[root@localhostlinux]#makebzImage-j3#只编译内核,并且使用3个线程[root@localhostlinux]#cparch/x86_64/boot/bzImage/mnt/boot/#拷贝内核

三、安装busybox

安装busybox需要依赖glibc-static

安装glibc-static 包在DVD2中,如何没有DVD2比如我..,自己搭建网络yum源安装

root@localhost~]#wgethttp://mirrors.163.com/.help/CentOS6-Base-163.repo这是网易yum源的配置文件[root@localhost~]#yumcleanall/#生效刚刚加载的yum仓库[root@localhost~]#yum-yinstallglibc-static[root@localhost~]#tarxfbusybox-1.22.1.tar.bz2[root@localhost~]#cdbusybox-1.22.1[root@localhostbusybox-1.22.1]#makemenuconfig
- BusyboxSettings- BuildOptions[*]BuildBusyBoxasastaticbinary(nosharedlibs)

[root@localhostbusybox-1.22.1]#make makeinstall[root@localhostbusybox-1.22.1]#cp-a_install/*/mnt/sysroot/

提供grub.conf文件:

[root@localhost~]#vim/mnt/boot/grub/grub.confdefault=0timeout=5titleLinux(3.13.6)root(hd0,0)kernel/bzImageroroot=/dev/sda2init=/sbin/init[root@localhost~]#sync#把内存缓冲区的数据立即写入磁盘中

测试:

添加新的虚拟机(server2) 注意选择磁盘时要选择之前创建的磁盘

添加完成后,把server1挂起或关机,然后server2开机

启动正常,但提示没有初始化文件

四、提供初始化文件(etc/fstab etc/inittabetc/rc.d/rc.sysinit)

[root@localhostsysroot]#vimetc/fstab/dev/sdb1/bootext4defaults00/dev/sdb2/ext4defaults00/dev/sdb3swapswapdefaults00proc/procprocdefaults00sysfs/syssysfsdefaults00

[root@localhostsysroot]#vimetc/inittab::sysinit:/etc/rc.d/rc.sysinit::respawn:/sbin/getty19200tty1::respawn:/sbin/getty19200tty2::respawn:/sbin/getty19200tty3::respawn:/sbin/getty19200tty4::respawn:/sbin/getty19200tty5::respawn:/sbin/getty19200tty6::ctrlaltdel:/sbin/reboot::shutdown:/bin/umount-a-r

[root@localhostsysroot]#vimetc/rc.d/rc.sysinit#!/bin/shecho-e"\tWelcometo\033[36mLinux\033[0m"[-r/etc/sysconfig/network] ./etc/sysconfig/network[-z"$HOSTNAME"-o"$HOSTNAME"=="(none)"] HOSTNAME=localhost/bin/hostname$HOSTNAMEmount-a#基于/etc/fstab文件挂载设备mdev-s#挂载内核所需设备文件mount-oremount-rw/#把根挂载成可读写ifconfiglo127.0.0.1#配置网卡信息ifconfigeth0192.168.199.222#配置网卡信息exportPS1="[\u@\h\w]$"#导出PS1路径exportPATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"#修改环境变量[root@localhostsysroot]#chmod+xetc/rc.d/rc.sysinit

提供账号和密码文件:

[root@localhostsysroot]#head-1/etc/passwd etc/passwd[root@localhostsysroot]#vimetc/passwd[root@localhostsysroot]#head-1/etc/group etc/group[root@localhostsysroot]#head-1/etc/shadow etc/shadow[root@localhostsysroot]#chmod400etc/shadow

提供认证库文件:

[root@localhost~]#cp-d/lib64/libnss_files*/mnt/sysroot/lib64/[root@localhost~]#cp-d/usr/lib64/libnss3.so/mnt/sysroot/usr/lib64/[root@localhost~]#mkdir/mnt/sysroot/usr/lib64[root@localhost~]#cp-d/usr/lib64/libnss3.so/mnt/sysroot/usr/lib64/[root@localhost~]#cp-d/usr/lib64/libnss_files.so/mnt/sysroot/usr/lib64/[root@localhost~]#cp/etc/nsswitch.conf/mnt/sysroot/etc/[root@localhost~]#cp/etc/shells/mnt/sysroot/etc/

提供主机名:

[root@localhostsysroot]#mkdiretc/sysconfig[root@localhostsysroot]#vimetc/sysconfig/networkHOSTNAME=biao.com[root@localhostsysroot]#sync

测试:

本地登陆成功!

五、提供ssh服务

[root@localhost~]#tarxfdropbear-2016.73.tar.bz2[root@localhost~]#cddropbear-2016.73[root@localhostdropbear-2016.73]#./configure

预编译时如果报如下错误:

#yum-yinstallzlib-devel#安装缺少的库#./configure#再次预编译[root@localhostdropbear-2016.73]#makePROGRAMS="dropbeardbclientdropbearkeyscp"[root@localhostdropbear-2016.73]#makePROGRAMS="dropbeardbclientdropbearkeydropbearconvertscp"install

命令移植脚本:

#!/bin/bashaimDir=/mnt/sysrootcmdInput(){ifwhich$cmd /dev/null;thencmdPath=`which--skip-alias$cmd`elseecho"Nosuchcommand."return5ficpCmd(){cmdDir=`dirname$cmdPath`[-d${aimDir}${cmdDir}]||mkdir-p${aimDir}${cmdDir}[-f$cmdPath] cp$cmdPath${aimDir}${cmdDir}cpLib(){forlibPathin`ldd$cmdPath|grep-o"/[^[:space:]]\{1,\}"`;dolibDir=`dirname$libPath`[-d${aimDir}${libDir}]||mkdir-p${aimDir}${libDir}[-f$libPath] cp$libPath${aimDir}${libDir}doneecho"Youcaninput[q|Q]quit."whiletrue;doread-p"Enteracommand:"cmdif[["$cmd"=~\(|q|Q|\)]];thenecho"Youchoosequit."exit0ficmdInput[$?-eq5] continuecpCmdcpLib[$?-eq0] echo-e"\033[36mCopysuccessful.\033[0m"Done

移植所需的命令:

[root@localhost~]#bashcp.shYoucaninput[q|Q]quit.Enteracommand:dropbearCopysuccessful.Enteracommand:dropbearkeyCopysuccessful.Enteracommand:qYouchoosequit.

生成密钥:

[root@localhost~]#mkdir/mnt/sysroot/etc/dropbear[root@localhost~]#dropbearkey-trsa-f/mnt/sysroot/etc/dropbear/dropbear_rsa_host_key-s2048[root@localhost~]#dropbearkey-tdss-f/mnt/sysroot/etc/dropbear/dropbear_dss_host_key

创建pid文件存放目录:

[root@localhost~]#mkdir/mnt/sysroot/var/run

挂载pts:

[root@localhostsysroot]#mkdirdev/pts[root@localhostsysroot]#vimetc/fstab........#上面省略devpts/dev/ptsdevptsdefaults00

提供服务脚本:

[root@localhostsysroot]#mkdiretc/rc.d/init.d[root@localhostsysroot]#vimetc/rc.d/init.d/dropbear#!/bin/bash#description:dropbearsshdaemon#chkconfig:23456633dsskey=/etc/dropbear/dropbear_dss_host_keyrsakey=/etc/dropbear/dropbear_rsa_host_keylockfile=/var/lock/subsys/dropbearpidfile=/var/run/dropbear.piddropbear=/usr/local/sbin/dropbeardropbearkey=/usr/local/bin/dropbearkey[-r/etc/rc.d/init.d/functions] ./etc/rc.d/init.d/functions[-r/etc/sysconfig/dropbear] ./etc/sysconfig/dropbearkeysize=1024port=22gendsskey(){[-d/etc/dropbear]||mkdir/etc/dropbearecho-n"Startinggeneratethedsskey:"$dropbearkey-tdss-f$dsskey /dev/nullRETVAL=$?if[$RETVAL-eq0];thensuccessechoreturn0elsefailureechoreturn1figenrsakey(){[-d/etc/dropbear]||mkdir/etc/dropbearecho-n"Startinggeneratethersakey:"$dropbearkey-trsa-s$keysize-f$rsakey /dev/nullRETVAL=$?if[$RETVAL-eq0];thensuccessechoreturn0elsefailureechoreturn1fistart(){[-e$dsskey]||gendsskey[-e$rsakey]||genrsakeyif[-e$lockfile];thenecho-n"dropbeardaemonisalreadyrunning:"successechoexit0fiecho-n"Startingdropbear:"daemon--pidfile="$pidfile"$dropbear-p$port-d$dsskey-r$rsakeyRETVAL=$?echoif[$RETVAL-eq0];thentouch$lockfilereturn0elserm-f$lockfile$pidfilereturn1fistop(){if[!-e$lockfile];thenecho-n"dropbearserviceisstopped:"successechoexit1fiecho-n"Stoppingdropbeardaemon:"killprocdropbearRETVAL=$?echoif[$RETVAL-eq0];thenrm-f$lockfile$pidfilereturn0elsereturn1fistatus(){if[-e$lockfile];thenecho"dropbearisrunning..."elseecho"dropbearisstopped..."fiusage(){echo"Usage:dropbear{start|stop|restart|status|gendsskey|genrsakey}"case$1instart)start;;stop)stop;;restart)stopstart;;status)status;;gendsskey)gendsskey;;genrsakey)genrsakey;;*)usage;;[root@localhostsysroot]#chmod+xetc/rc.d/init.d/dropbear[root@localhostsysroot]#cp/etc/rc.d/init.d/functionsetc/rc.d/init.d/[root@localhostsysroot]#cdetc/rc.d/[root@localhostrc.d]#ln-sinit.d/dropbeardropbear.start[root@localhostrc.d]#ln-sinit.d/dropbeardropbear.stop[root@localhostrc.d]#echo"/etc/rc.d/*.startstart" rc.sysinit

关机脚本

[root@localhostrc.d]#vimrc.sysdown#!/bin/shsync#把内存缓冲区的数据立即写入磁盘中sleep3#给系统3秒的写入时间/etc/rc.d/*.stopstopumount-a-rPoweroff[root@localhostrc.d]vim/mnt/sysroot/etc/inittab::shutdown:/etc/rc.d/rc.sysdown#最后一行修改成这样

测试:

服务开机启动

ssh远程登录成功:

以上实验全部完成!!!!!!!!!

我只愿,在你的理想和希望里能为你增加一点鼓励,

定制linux内核+Busybox+dropbear实现远程登录推荐

相关文章:

你感兴趣的文章:

标签云: