编译内核+BusyBox定制一个Linux提供ssh和web服务【转】

转自:http://chenpipi.blog.51cto.com/8563610/1390874

 

本次试验大致规划和步骤:完全定制一个linux系统;能让其远程登录和提供web服务。

1、添加一块空闲磁盘

2、下载编译内核

3、并为空闲磁盘安装grub

前提准备:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@soul ~]# fdisk /dev/sdb    分区
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4e977ad9
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           7       56196   83  Linux
/dev/sdb2               8          73      530145   83  Linux
/dev/sdb3              74         107      273105   82  Linux swap / Solaris
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
#格式化操作
[root@soul ~]# mke2fs -t ext4 /dev/sdb1
[root@soul ~]# mke2fs -t ext4 /dev/sdb2
[root@soul ~]# mkswap /dev/sdb3
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
[root@soul ~]# mkdir -pv /mnt/{boot,sysroot}
[root@soul ~]# mount /dev/sdb1 /mnt/boot/
[root@soul ~]# mount /dev/sdb2 /mnt/sysroot/
[root@soul ~]# grub-install --root-directory=/mnt /dev/sdb    安装grub
[root@soul ~]# ls /mnt/boot/
grub  lost+found
[root@soul ~]# 上述信息显示安装成功

 

其中需要一个脚本来移植命令和所依赖的库文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#脚本可能不完善;不过可用
#!/bin/bash
aimDir=/mnt/sysroot
cmdInput() {
    if which $cmd &> /dev/null;then
        cmdPath=`which --skip-alias $cmd`
    else
        echo "No such command."
        return 5
    fi
}
cpCmd() {
    cmdDir=`dirname $cmdPath`
    -d ${aimDir}${cmdDir} ] || mkdir -p ${aimDir}${cmdDir}
    -f $cmdPath ] && cp $cmdPath ${aimDir}${cmdDir}
}
cpLib() {
    for libPath in `ldd $cmdPath | grep -"/[^[:space:]]\{1,\}"`;do
        libDir=`dirname $libPath`
        -d ${aimDir}${libDir} ] || mkdir -p ${aimDir}${libDir}
        -f $libPath ] && cp $libPath ${aimDir}${libDir}
    done
}
echo "You can input [q|Q] quit."
while true;do
  read -"Enter a command: " cmd
  if [[ "$cmd" =~ \(|q|Q|\) ]];then
    echo "You choose quit."
    exit 0
  fi
    cmdInput
    [ $? -eq 5 ] && continue
    cpCmd
    cpLib
    [ $? -eq 0 ] && echo -"\033[36mCopy successful.\033[0m"
done

 

 

一、编译内核

下载地址:https://www.kernel.org/

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@soul ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  linux-3.13.8.tar.xz
[root@soul ~]# 这里下载的是目前最新的稳定版
[root@soul ~]# tar xf linux-3.13.8.tar.xz -C /usr/src/
[root@soul ~]# ln -sv /usr/src/linux-3.13.8/ /usr/src/linux    创建链接
`/usr/src/linux' -> `/usr/src/linux-3.13.8/'
[root@soul ~]# cd /usr/src/linux
[root@soul linux]#
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
#编译
[root@soul linux]# make allnoconfig    清除所有选择;然后重新选择定制
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/kconfig/conf.o
  SHIPPED scripts/kconfig/zconf.tab.c
  SHIPPED scripts/kconfig/zconf.lex.c
  SHIPPED scripts/kconfig/zconf.hash.c
  HOSTCC  scripts/kconfig/zconf.tab.o
  HOSTLD  scripts/kconfig/conf
scripts/kconfig/conf --allnoconfig Kconfig
#
# configuration written to .config
#
[root@soul linux]# make menuconfig
#下面的选择没办法列出来;给个大概
1、选择CPU类型
2、支持动态模块装载
3、PCI总线支持
4、硬盘驱动
5、文件系统
6、可执行文件格式
7、I/O驱动;USB驱动
8、devtmpfs支持
9、选择网络支持以及网卡驱动
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
#结束后备份下配置文件;然后编译成bzImage格式
[root@soul linux]# cp .config /root/config-3.13.8-x86_64
[root@soul linux]# make bzImage
[root@soul linux]# cp arch/x86/boot/bzImage /mnt/boot/

 

 

 

二、安装BusyBox

BusyBox 是一个遵循GPL协议、以自由软件形式发布的应用程序。Busybox在单一的可执行文件中提供了精简的Unix工具集(例如shell、init、getty、login...),可运行于多款POSIX环境的操作系统,例如Linux(包括Android)、Hurd、FreeBSD等等。

 

1、安装;官方下载地址:http://www.busybox.net/

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#因为稍后需要编译busybox为静态二进制程序;所以需要实现安装glibc-static和libmcrypt-devel;
#glibc-static在安装光盘的第二张光盘上;可以挂在安装;也可以到网上下载
[root@soul busybox-1.22.1]# mount /dev/cdrom /media/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@soul busybox-1.22.1]# yum -y install /media/Packages/glibc-static-2.12-1.132.el6.x86_64.rpm
[root@soul ~]# ls
anaconda-ks.cfg         config-3.13.8-x86_64  install.log.syslog
busybox-1.22.1.tar.bz2  install.log           linux-3.13.8.tar.xz
[root@soul ~]#
[root@soul ~]# tar xf busybox-1.22.1.tar.bz2
[root@soul ~]# cd busybox-1.22.1    #安装可以查看INSTALL文件说明
[root@soul busybox-1.22.1]# make menuconfig
Busybox Settings  --->
    Build Options  --->
        [*] Build BusyBox as a static binary (no shared libs) #选中这项
#其他选项都不需要改动了
                                                                                                      
[root@soul busybox-1.22.1]# make
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LANG = "en"
#根据提示设置下
[root@soul busybox-1.22.1]# export LANGUAGE=en_US.UTF-8
[root@soul busybox-1.22.1]# export LANG=en_US.UTF-8
[root@soul busybox-1.22.1]# export LC_ALL=en_US.UTF-8
[root@soul busybox-1.22.1]# make    再次make通过
[root@soul busybox-1.22.1]# make install
--------------------------------------------------
You will probably need to make your busybox binary
setuid root to ensure all configured applets will
work properly.
--------------------------------------------------
[root@soul busybox-1.22.1]#
[root@soul busybox-1.22.1]# ls _install/
bin  linuxrc  sbin  usr
[root@soul busybox-1.22.1]# cp -a _install/* /mnt/sysroot/
[root@soul busybox-1.22.1]# cd /mnt/sysroot/
[root@soul sysroot]# ls
bin  linuxrc  lost+found  sbin  usr
[root@soul sysroot]# mkdir -pv etc/rc.d var/log root proc sys srv boot mnt tmp home dev lib lib64

 

2、提供一个grub.conf文件

 

1
2
3
4
5
6
7
8
9
[root@soul sysroot]# vim /mnt/boot/grub/grub.conf
default=0
timeout=5
title Mini Linux (3.13.8-soul)
        root (hd0,0)
        kernel /bzImage ro root=/dev/sda2 init=/sbin/init
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
#完成后sync同步下;挂起宿主机;然后创建一个新的虚拟机使用之前添加的磁盘
#即可测试启动

wKioL1M749SzOSbTAABbxGWYUss105.jpg

wKiom1M75Ayi7fkTAAGSbFypIr8830.jpg

测试基本启动以正常。但是提示没有脚本文件。

wKioL1M76uzDUo-lAADvr_de5Lo380.jpg

wKiom1M76ySD12Q6AADsTSEU74I721.jpg

测试可以配置IP地址;也能ping网关。

 

3、提供rc脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@soul sysroot]# vim etc/fstab
/dev/sda1       /boot   ext4    defaults        0 0
proc            /proc   proc    defaults        0 0
sysfs           /sys    sysfs   defaults        0 0
/dev/sda2       /       ext4    defaults        0 0
/dev/sda3       swap    swap    defaults        0 0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
[root@soul sysroot]# mkdir etc/init.d
[root@soul sysroot]# vim etc/rc.d/rc.sysinit
#!/bin/sh
#
echo -"\tWelcome to \033[36mMini Linux\033[0m Soul"
mount -a
mdev -s
ifconfig lo 172.0.0.1
ifconfig eth0 172.16.40.2
[root@soul sysroot]# chmod +x etc/rc.d/rc.sysinit
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
[root@soul sysroot]# vim etc/inittab
::sysinit:/etc/rc.d/rc.sysinit
console::respawn:-/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount --r
[root@soul sysroot]#sync
#测试启动

wKioL1M78cbAbUkrAAGfldiU4OU526.jpg

wKiom1M78f7wBwrNAAEquagbxS8957.jpg

测试启动正常。

 

三、实现密码登陆;且可以ssh远程连接

1、提供虚拟终端;更改之前的inittab文件

1
2
3
4
5
6
7
8
9
10
11
[root@soul sysroot]# vim etc/inittab
::sysinit:/etc/rc.d/rc.sysinit
::respawn:/sbin/getty 19200 tty1
::respawn:/sbin/getty 19200 tty2
::respawn:/sbin/getty 19200 tty3
::respawn:/sbin/getty 19200 tty4
::respawn:/sbin/getty 19200 tty5
::respawn:/sbin/getty 19200 tty6
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount --r
[root@soul sysroot]# sync

 

2、提供密码文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#这里以原有的机器上的密码文件追加到新的系统文件里
[root@soul sysroot]# head -1 /etc/passwd > /mnt/sysroot/etc/passwd
[root@soul sysroot]# grep soul /etc/passwd >> /mnt/sysroot/etc/passwd
[root@soul sysroot]# vim /mnt/sysroot/etc/passwd
#更改默认shell
root:x:0:0:root:/root:/bin/sh
soul:x:500:500::/home/soul:/bin/sh
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
[root@soul sysroot]# head -1 /etc/group > /mnt/sysroot/etc/group
[root@soul sysroot]# grep soul /etc/group >> /mnt/sysroot/etc/group
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
[root@soul sysroot]# head -1 /etc/shadow > /mnt/sysroot/etc/shadow
[root@soul sysroot]# grep soul /etc/shadow >> /mnt/sysroot/etc/shadow
[root@soul sysroot]# chmod 400 /mnt/sysroot/etc/shadow
#sync后直接测试;一般tty1登陆是有问题的。

wKiom1M7-Vzg3pKMAAC5XYSpBC4301.jpg

wKioL1M7-UDy9V_RAACdaMVdNDU390.jpg

wKiom1M7-XfS4RUCAACc6iODnME901.jpg

测试登陆成功。

 

3、提供主机名等信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@soul sysroot]# mkdir etc/sysconfig
[root@soul sysroot]# vim etc/sysconfig/network
HOSTNAME=Soul.com
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
[root@soul etc]# vim profile        设置环境变量
export PS1='[\u@\h \W]\$'
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
[root@soul sysroot]# vim etc/rc.d/rc.sysinit
#!/bin/sh
#
echo -"\tWelcome to \033[36mMini Linux\033[0m Soul"
-/etc/sysconfig/network ] && . /etc/sysconfig/network
-"$HOSTNAME" -"$HOSTNAME" == "(none)" ] && HOSTNAME=localhost
/bin/hostname $HOSTNAME
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
-/etc/profile ] && . /etc/profile
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
mdev -s
mount -a
ifconfig lo 172.0.0.1
ifconfig eth0 172.16.40.2
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
[root@soul sysroot]# vim etc/issue
Welcome to Mini Linux Soul
kernel \r

 

4、编译安装dropbear提供ssh服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@soul ~]# tar xf dropbear-2013.58.tar.bz2
[root@soul ~]# cd dropbear-2013.58
[root@soul dropbear-2013.58]# ./configure
[root@soul dropbear-2013.58]# make PROGRAMS="dropbear dbclient dropbearkey dropbearconvert scp"
[root@soul dropbear-2013.58]# make PROGRAMS="dropbear dbclient dropbearkey dropbearconvert scp" install
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
[root@soul dropbear-2013.58]# mkdir /etc/dropbear
[root@soul dropbear-2013.58]# dropbearkey -t rsa -s 1024 -f /etc/dropbear/dropbear_rsa_host_key
[root@soul dropbear-2013.58]# dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
[root@soul dropbear-2013.58]# dropbear -p 2222
[root@soul dropbear-2013.58]# ss -tunl | grep 2222
tcp    LISTEN     0      20                    :::2222                 :::*
tcp    LISTEN     0      20                     *:2222                  *:*
[root@soul dropbear-2013.58]# 测试是否可以登陆
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
#用脚本来复制命令和依赖的库文件到Mini系统上
[root@soul ~]# sh cp.sh
You can input [q|Q] quit.
Enter a command: dropbear
Copy successful.
Enter a command: dropbearkey
Copy successful.
Enter a command: scp
Copy successful.
Enter a command: bash
Copy successful.
Enter a command: q
You choose quit.
[root@soul ~]#
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
#认证库
[root@soul ~]# cp -d /lib64/libnss_files* /mnt/sysroot/lib64/
[root@soul ~]# cp -d /usr/lib64/libnss3.so /mnt/sysroot/usr/lib64/
[root@soul ~]# cp -d /usr/lib64/libnss_files.so /mnt/sysroot/usr/lib64/
[root@soul ~]# cp /etc/nsswitch.conf /mnt/sysroot/etc/
[root@soul ~]# vim /mnt/sysroot/etc/shells
#安全shell
/bin/sh
/bin/hush
/sbin/nologin
/bin/bash
/bin/ash
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
#在Mini系统生成key文件
[root@soul ~]# mkdir /mnt/sysroot/etc/dropbear
[root@soul ~]# dropbearkey -t dss -f /mnt/sysroot/etc/dropbear/dropbear_dss_host_key
[root@soul ~]# dropbearkey -t rsa -s 1024 -f /mnt/sysroot/etc/dropbear/dropbear_rsa_host_key
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
[root@soul ~]# mkdir /mnt/sysroot/var/run    存放pid文件

 

5、挂载pts

1
2
3
4
5
6
7
8
9
10
11
[root@soul sysroot]# vim etc/rc.d/rc.sysinit
mdev -s    #这个下面添加一行
mkdir /dev/pts
mount -a
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
[root@soul sysroot]# vim etc/fstab
/dev/sda1       /boot           ext4            defaults        0 0
proc            /proc           proc            defaults        0 0
sysfs           /sys            sysfs           defaults        0 0
#下面加一行
devpts          /dev/pts        devpts          defaults        0 0

 

6、提供dropbear的启动脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
[root@soul sysroot]# mkdir etc/rc.d/init.d
[root@soul sysroot]# vim etc/rc.d/init.d/dropbear
#!/bin/bash
#
# description: dropbear ssh daemon
# chkconfig: 2345 66 33
#
dsskey=/etc/dropbear/dropbear_dss_host_key
rsakey=/etc/dropbear/dropbear_rsa_host_key
lockfile=/var/lock/subsys/dropbear
pidfile=/var/run/dropbear.pid
dropbear=/usr/local/sbin/dropbear
dropbearkey=/usr/local/bin/dropbearkey
-/etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions
-/etc/sysconfig/dropbear ] && . /etc/sysconfig/dropbear
keysize=1024
port=22
gendsskey() {
    -/etc/dropbear ] || mkdir /etc/dropbear
    echo -"Starting generate the dss key: "
    $dropbearkey -t dss -f $dsskey &> /dev/null
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        success
        echo
        return 0
    else
        failure
        echo
        return 1
    fi
}
genrsakey() {
    -/etc/dropbear ] || mkdir /etc/dropbear
    echo -"Starting generate the rsa key: "
    $dropbearkey -t rsa -s $keysize -f $rsakey &> /dev/null
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        success
        echo
        return 0
    else
        failure
        echo
        return 1
    fi
}
start() {
    -e $dsskey ] || gendsskey
    -e $rsakey ] || genrsakey
    if -e $lockfile ]; then
        echo -"dropbear daemon is already running: "
        success
        echo
        exit 0
    fi
    echo -"Starting dropbear: "
    daemon --pidfile="$pidfile" $dropbear -p $port -d $dsskey -r $rsakey
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ]; then
        touch $lockfile
        return 0
    else
        rm -f $lockfile $pidfile
        return 1
    fi
}
stop() {
    if [ ! -e $lockfile ]; then
        echo -"dropbear service is stopped: "
        success
        echo
        exit 1
    fi
    echo -"Stopping dropbear daemon: "
    killproc dropbear
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ]; then
        rm -f $lockfile $pidfile
        return 0
    else
        return 1
    fi
}
status() {
    if -e $lockfile ]; then
        echo "dropbear is running..."
    else
        echo "dropbear is stopped..."
    fi
}
usage() {
    echo "Usage: dropbear {start|stop|restart|status|gendsskey|genrsakey}"
}
case $1 in
    start)
        start ;;
    stop)
        stop ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    gendsskey)
        gendsskey
        ;;
    genrsakey)
        genrsakey
        ;;
    *)
        usage
        ;;
esac                                                                                                                                                                                                                                                            
[root@soul sysroot]# chmod +x etc/rc.d/init.d/dropbear
[root@soul sysroot]# cp /etc/rc.d/init.d/functions etc/rc.d/init.d/
#做启动脚本链接文件
[root@soul ~]# cd /mnt/sysroot/etc/rc.d/
[root@soul rc.d]# ln -sv init.d/dropbear dropbear.s
[root@soul rc.d]# ln -sv init.d/dropbear dropbear.k
`dropbear.k' -> `init.d/dropbear'
[root@soul rc.d]# ll
total 8
lrwxrwxrwx. 1 root root   15 Apr  2 22:09 dropbear.k -> init.d/dropbear
lrwxrwxrwx. 1 root root   15 Apr  2 22:09 dropbear.s -> init.d/dropbear
#查看是否连接成功
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
[root@soul etc]# vim rc.d/rc.sysinit
mkdir /dev/pts
#下面加一行
/etc/rc.d/*.s start

 

7、提供关机服务脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@soul etc]# vim rc.d/rc.sysdown
#!/bin/sh
#
sync
sleep 5
/etc/rc.d/*.k stop
/bin/umount --r
poweroff
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
[root@soul etc]# chmod +x rc.d/rc.sysdown
[root@soul etc]# vim inittab
#更改下面这行为执行脚本
::shutdown:/etc/rc.d/rc.sysdown
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
#测试启动远程连接

wKioL1M8H8ihYb9JAAD-zhJkZsI958.jpg

测试tty1也可以正常登陆;只是需要等待一会在登陆即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Xshell:\> ssh 172.16.40.2
Connecting to 172.16.40.2:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
[root@Soul ~]#ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:38:36:2B
          inet addr:172.16.40.2  Bcast:172.16.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:67 errors:0 dropped:0 overruns:0 frame:0
          TX packets:28 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6415 (6.2 KiB)  TX bytes:3538 (3.4 KiB)
          Interrupt:19 Base address:0x2000
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
#测试开机可以自动启动dropbear;并可以远程登陆

 

 

四、安装nginx;提供web服务

1、安装;下载地址http://nginx.org/

1
2
3
4
5
6
7
8
[root@soul ~]# cd nginx-1.4.2
[root@soul nginx-1.4.2]# ./configure --prefix=/usr/local --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --user=nginx --group=nginx --without-pcre --without-http_rewrite_module --without-http_geo_module --without-http_uwsgi_module --without-http_fastcgi_module  --without-http_scgi_module --without-http_memcached_module
[root@soul nginx-1.4.2]# make && make install
[root@soul nginx-1.4.2]# useradd nginx
[root@soul nginx-1.4.2]# nginx
[root@soul nginx-1.4.2]# ss -tunl | grep 80
tcp    LISTEN     0      128                    *:80                    *:*
[root@soul nginx-1.4.2]# 在浏览器测试下

 

2、移植nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@soul ~]# sh cp.sh
You can input [q|Q] quit.
Enter a command: nginx
Copy successful.
Enter a command: q
You choose quit.
[root@soul ~]#
[root@soul ~]# cp /etc/nginx/ /mnt/sysroot/etc/ -r
[root@soul ~]# grep "^nginx" /etc/passwd >> /mnt/sysroot/etc/passwd
[root@soul ~]# grep "^nginx" /etc/group >> /mnt/sysroot/etc/group
[root@soul ~]# grep "^nginx" /etc/shadow >> /mnt/sysroot/etc/shadow
[root@soul ~]# mkdir /mnt/sysroot/usr/local/html
[root@soul ~]# vim /mnt/sysroot/usr/local/html/index.html
<h1>Welcome to Nginx</h1>
[root@soul ~]#

 

3、提供服务脚本

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#由于nginx的脚本如调用functions函数;可能会导致依赖其他redhat系统独有的函数;会导致开机无法自启动;也会影响其他程序导致无法启动;所以需要自行写个脚本。
[root@soul ~]# vi /mnt/sysroot/etc/rc.d/nginx
#!/bin/sh
#
# Startup script for the Nginx
# chkconfig: - 88 63
# description: Nginx is a free,open-source,high-performance HTTP Server and reverse proxy.
# program:/usr/local/sbin/nginx
# config:/etc/nginx/nginx.conf
# pidfile:/usr/local/logs/nginx.pid
                                                                                                                                                                                                                                                                                                                  
# Synopsis:
#        nginx [--help] [--version] {start|stop|restart|reload|status}
                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                  
# Define variable
nginx=/usr/local/sbin/nginx
pidfile=/usr/local/logs/nginx.pid
PROGRAM=`basename $0`
nginx_conf=/etc/nginx/nginx.conf
alog=/var/log/nginx/access.log
elog=/var/log/nginx/error.log
VERSION=1.4.2
                  
# Functions
usage(){
    echo "Usage: $PROGRAM [--help] [--version] {start|stop|restart|reload|status}"
}
                                                                                                                                                                                                                                                                                                                  
version(){
    echo "Version:$VERSION"
}
                                                                                                                                                                                                                                                                                                                  
start(){
if -e $pidfile ]
   then
    echo "Nginx already running..."
   else
    -f $alog ] || touch $alog
    -f $elog ] || touch $elog
    echo -"Starting Nginx:\t\t\t\t\t\t\t\c"
    $nginx -c $nginx_conf
    touch $pidfile
    echo -"[ \c"
    echo -"\033[0;32mOK\033[0m\c"
    echo -" ]\c"
    echo -"\r"
fi
}
                                                                                                                                                                                                                                                                                                                  
stop(){
if -e $pidfile ]
   then
    echo -"Stopping Nginx:\t\t\t\t\t\t\t\c"
    /usr/bin/killall $PROGRAM &> /dev/null
    rm -f $pidfile
    echo -"[ \c"
    echo -"\033[0;32mOK\033[0m\c"
    echo -" ]\c"
    echo -"\r"
   else
    echo "Nginx already stopped..."
fi
}
                                                                                                                                                                                                                                                                                                                  
reload(){
if -e $pidfile ]
   then
    echo -"Reloading Nginx:\t\t\t\t\t\t\c"
    kill -HUP `pidof $PROGRAM`
    echo -"[ \c"
    echo -"\033[0;32mOK\033[0m\c"
    echo -" ]\c"
    echo -"\r"
   else
    echo "Nginx is not running..."
fi
}
                                                                                                                                                                                                                                                                                                                  
status(){
    if -e $pidfile ];then
        echo  "Nginx is running..."
       else
        echo  "Nginx is stopped..."
    fi
}
                                                                                                                                                                                                                                                                                                                  
case $1 in
        start)
            start
            ;;
        stop)
            stop
            ;;
        restart)
            stop
        sleep2
            start
            ;;
        reload)
            reload
            ;;
        status)
            status
            ;;
        --help)
            usage
            ;;
        --version)
            version
            ;;
        *)
            usage
esac
[root@soul ~]# chmod +x /mnt/sysroot/etc/rc.d/init.d/nginx
[root@soul ~]# cd /mnt/sysroot/etc/rc.d/
[root@soul rc.d]# ls
dropbear.k  dropbear.s  init.d  rc.sysdown  rc.sysinit
[root@soul rc.d]# ln -sv init.d/nginx nginx.s
`nginx.s' -> `init.d/nginx'
[root@soul rc.d]# ln -sv init.d/nginx nginx.k
`nginx.k' -> `init.d/nginx'

4、开机测试

这里可能由于没有启动的先后顺序;如按照之前的启动方式会导致都无法开机自动启动;这里更改了开机启动脚本:

 

1
2
3
4
5
6
7
8
[root@soul sysroot]# vim etc/rc.d/rc.sysinit
#下面是分开启动的;否则会启动失败
mdev -s
mkdir /dev/pts
mount -a
mount --o remount,rw /
/etc/rc.d/dropbear.s start
/etc/rc.d/nginx.s start

wKiom1M_X6rwLxBlAAJ5T5-oU5A813.jpg

wKioL1M_X47x1qTOAAEcvqMhP0c134.jpg

wKiom1M_X8OhDOh3AADGsUN0KzY102.jpg

1
2
3
4
5
6
7
Xshell:\> ssh 172.16.40.2
Connecting to 172.16.40.2:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
[root@Soul ~]#su - soul
[soul@Soul ~]$
#测试ssh登陆和切换账户都是没有问题的。

 

到此;本实验结束;且需要的功能都以实现;关于程序的开机启动和关机关闭的问题;这个问题也琢磨了很长时间;个人感觉应该是次顺问题;所以如需实现;可能需要更改启动/关闭脚本文件。

posted @ 2016-08-25 16:35  liangwode  阅读(4870)  评论(0编辑  收藏  举报