Linux系統-小倒騰之Linux DIY定製裁剪(定製Linux+SSH/Nginx)o_o(三)

       此文緊接上篇博文《Linux系統-小倒騰之Linux DIY定製裁剪(New kernel+Busybox)o_o(二)》,前面咱們已經將定製版Linux系統已經初步有了一個Linux系統擁有的基本功能,那如今須要作的就是從宿主機上將遠程服務軟件dropbear以及nginx移植至這臺定製版Linux中,而且實現它們本身各自的功能。具體實現步驟請看下面:html


       在移植以前須要在宿主機上進行安裝測試,待工做正常,才採起移植操做nginx


====================編譯安裝dropbear、測試及移植====================shell

1.解壓安裝包vim

[root@station10 ~]# tar -xf  dropbear-2013.58.tar.bz2 -C /usr/src/
[root@station10 ~]# cd /usr/src/dropbear-2013.58/
[root@station10 dropbear-2013.58]#

2.編譯操做
安全

[root@station10 dropbear-2013.58]# ./configure
[root@station10 dropbear-2013.58]# make PROGRAMS="dropbear dbclient dropbearkey dropbearconvert scp"
[root@station10 dropbear-2013.58]# make PROGRAMS="dropbear dbclient dropbearkey dropbearconvert scp" install

3.建立dropbear密鑰存放目錄
bash

[root@station10 dropbear-2013.58]# mkdir /etc/dropbear/

4.生成密鑰文件
框架

[root@station10 dropbear-2013.58]# dropbearkey -t rsa -s 1024 -f /etc/dropbear/dropbear_rsa_host_key
Will output 1024 bit rsa secret key to '/etc/dropbear/dropbear_rsa_host_key'
Generating key, this may take a while...
Public key portion is:
.........
.........
[root@station10 dropbear-2013.58]# dropbearkey -t dss  -f /etc/dropbear/dropbear_dss_host_key
Will output 1024 bit dss secret key to '/etc/dropbear/dropbear_dss_host_key'
Generating key, this may take a while...
Public key portion is:
.........
.........
[root@station10 dropbear-2013.58]# ls /etc/dropbear/
dropbear_dss_host_key  dropbear_rsa_host_key
[root@station10 dropbear-2013.58]#

5.啓動dropbear測試
ssh

由於我實驗中用的是sshd的22port,爲了不他們衝突我將使用22022portide

[root@station10 dropbear-2013.58]# dropbear -p 22022
#端口監聽中
[root@station10 dropbear-2013.58]# ss -tnl | grep 22022
LISTEN     0      20                       :::22022                   :::*
LISTEN     0      20                        *:22022                    *:*
[root@station10 dropbear-2013.58]#

wKioL1M-M5SwAzUhAAHuFrevfNM328.jpg

ok!如今dropbear工做起來了,開始移植吧memcached

6.移植dropbear所需程序文件

(1)執行那個bincp腳本:

[root@station10 ~]# sh bincp.sh
A command: dropbear
Copy /usr/local/sbin/dropbear to /mnt/sysroot finished.
Copy /lib64/libcrypt.so.1 to /mnt/sysroot finished.
Copy /lib64/libutil.so.1 to /mnt/sysroot finished.
Copy /lib64/libz.so.1 to /mnt/sysroot finished.
Copy /lib64/libfreebl3.so to /mnt/sysroot finished.
A command: dropbearkey
Copy /usr/local/bin/dropbearkey to /mnt/sysroot finished.
A command: dbclient
Copy /usr/local/bin/dbclient to /mnt/sysroot finished.
A command: scp
Copy /usr/local/bin/scp to /mnt/sysroot finished.
A command: q
[root@station10 ~]# ls /mnt/sysroot/usr/local/bin/
dbclient  dropbearkey  scp
[root@station10 ~]# ls /mnt/sysroot/usr/local/sbin/
dropbear
[root@station10 ~]#

(2)移植名稱解析服務文件

咱們知道在遠程鏈接時dropbear它須要對用戶身份驗證,也就是名稱解析,這就意味着nssswitch這個框架文件須要移植過去

#移植依賴的庫文件
[root@station10 ~]# cp -d /lib64/libnss_files* /mnt/sysroot/lib64/
[root@station10 ~]# cp -d /usr/lib64/libnss3.so /mnt/sysroot/usr/lib64/
[root@station10 ~]# cp -d /usr/lib64/libnss_files.so /mnt/sysroot/usr/lib64/
#移植配置文件
[root@station10 ~]# cp /etc/nsswitch.conf /mnt/sysroot/etc/

(3)移植安全shell文件

dropbear驗證用戶身份時是須要驗證該用戶是不是使用的安全shell登陸,因此我將宿主機裏面/etc/shells文件移植至定製版Linux中

[root@station10 ~]# cp /etc/shells /mnt/sysroot/etc/
#移植bash,執行cpbin腳本
[root@station10 ~]# sh bincp.sh
A command: bash
Copy /bin/bash to /mnt/sysroot finished.
A command: q
[root@station10 ~]#

(4)爲定製版Linux生成dropbearkey文件

[root@station10 ~]# cd /mnt/sysroot/etc/
[root@station10 etc]# mkdir dropbear
[root@station10 etc]# dropbearkey -t rsa -s 2048 -f dropbear/dropbear_rsa_host_key
Will output 2048 bit rsa secret key to 'dropbear/dropbear_rsa_host_key'
Generating key, this may take a while...
Public key portion is:
.........
.........
[root@station10 etc]# dropbearkey -t dss -f dropbear/dropbear_dss_host_key
Will output 1024 bit dss secret key to 'dropbear/dropbear_dss_host_key'
Generating key, this may take a while...
Public key portion is:
.........
.........
[root@station10 etc]# ls dropbear/
dropbear_dss_host_key  dropbear_rsa_host_key
[root@station10 etc]#

(5)pid文件:

在dropbear啓動後它會在/var/run目錄中生成一個pid文件,須要保證有這個目錄

(前面我已經有建立過了)

(6)僞終端

在遠程登陸是都是使用僞終端,那僞文件系統又從何而來呢?Target上有沒有;其實他是掛載而來的,因此須要讓其支持僞文件系統

[root@station10 etc]# pwd
/mnt/sysroot/etc
[root@station10 etc]# vim fstab
/dev/sda1       /boot   ext4    defaults 0 0
/dev/sda2       /       ext4    defaults 0 0
proc            /proc   proc    defaults 0 0
sysfs           /sys    sysfs   defaults 0 0
/dev/sda3       swap    swap    defaults 0 0
devpts          /dev/pts devpts defaults 0 0 #加入此行
#/dev/pts這個目錄無需建立,
drwxr-xr-x 2 root root 0 Apr  1 19:31 /dev/pts/
[root@station10 sysroot]# sync

7.爲其提供開關機機自動啓動/關閉腳本

#建立鎖文件目錄:
mkdir -p /mnt/sysroot/var/lock/subsys/
[root@station10 etc]# mkdir rc.d/init.d
#移植宿主機上面的/etc/rc.d/init.d/funcitons
[root@station10 ~]# cp /etc/rc.d/init.d/functions /mnt/sysroot/etc/rc.d/init.d/
#移植宿主機上面的/sbin/consoletype,它被functions所依賴
[root@station10 ~]# sh bincp.sh
A command: /sbin/consoletype
Copy /sbin/consoletype to /mnt/sysroot finished.
A command: q
[root@station10 ~]#
#建立dropbear
[root@station10 etc]# vim 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
[ -r /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions
[ -r /etc/sysconfig/dropbear ] && . /etc/sysconfig/dropbear
keysize=1024
port=22
gendsskey() {
        [ -d /etc/dropbear ] || mkdir /etc/dropbear
        echo -n "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() {
        [ -d /etc/dropbear ] || mkdir /etc/dropbear
        echo -n "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 -n "dropbear daemon is already running: "
                success
                echo
                exit 0
        fi
        echo -n "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 -n "dropbear service is stopped: "
                success
                echo
                exit 1
        fi
        echo -n "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@station10 etc]# chmod +x rc.d/init.d/dropbear
#建立自動啓動/關閉腳本相關文件
[root@station10 etc]# cd rc.d/
#因爲busybox沒有運行級別這個概念,我這裏就假設運行在級別3
[root@station10 rc.d]# mkdir rc3.d
[root@station10 rc.d]# cd rc3.d/
[root@station10 rc3.d]# ln -sv ../init.d/dropbear dropbear.start
`dropbear.start' -> `init.d/dropbear'
[root@station10 rc3.d]# ln -sv ../init.d/dropbear dropbear.stop
`dropbear.stop' -> `init.d
/dropbear'
[root@station10 rc.d]#
[root@station10 etc]# vim rc.d/rc.sysinit
/etc/rc3.d/*.start start   #加入此行系統在開機時它就會加載/etc/rc.d/下面以start結尾的全部服務腳本使之啓動
#關機時執行的
[root@station10 rc3.d]# vim ../rc.sysdown
#!/bin/bash
#
sync
sleep 2
sync
/etc/rc.d/rc3.d/*.stop stop
/bin/umount -a -r
[root@station10 rc3.d]# chmod +x rc.sysdown
#修改/etc/inittab文件,最後一行修改成:
[root@station10 rc.d]# vim ../inittab
::shutdown:/etc/rc.d/rc.sysdown
[root@station10 rc.d]# sync
#開機時的我在上面第7點已經寫在/etc/rc.d/rc.sysinit文件中了

看來差很少了,如今啓動試一下:

Picture-1(登陸界面)

wKiom1M_hPTwH9obAACAY2bj1Cw552.jpg

Picture-2(登陸後,網卡IP、命令提示符、環境變量沒有問題)

wKiom1M_hRvQyukbAAKvViO0EbI853.jpg

Picture-3(dropbear應用程序實現開機自動運行)

wKiom1M-gyXyMYW7AACGd0jT8-M943.jpg

Picture-4(嘗試遠程登陸,讓其監視狀態)

wKioL1M_hSHzjKg-AAJm9HvHLSY574.jpg

Picture-5(遠程登陸後,毫無疑問用的就是虛擬終端)

wKioL1M_hU2wkAxlAABzyk5i0B0508.jpg

Picture-6(定製版Linux端監視到遠程登陸成功的信息)

wKioL1M_hXvwmRkkAACjtmrRL1U624.jpg

ok!目前doprbear已經能夠實現開機自動啓動了,而且客戶端遠程鏈接也沒有問題了

好的!至此dropbear應用程序移植完成...


====================編譯安裝nginx、測試及移植====================

仍是先在宿主機上測試,測試經過在移植

1.解壓安裝包

[root@station10 ~]# tar -xf nginx-1.4.2.tar.gz -C /usr/src/
[root@station10 ~]# cd /usr/src/nginx-1.4.2/
[root@station10 nginx-1.4.2]#

2.編譯安裝操做

[root@station10 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/access.log --user=nginx --group=nginx --with-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@station10 nginx-1.4.2]# make && make install

3.建立用戶和組

[root@station10 nginx-1.4.2]# groupadd -r nginx
[root@station10 nginx-1.4.2]# useradd -r -g nginx -s /sbin/nologin nginx

4.啓動測試

[root@station10 ~]# nginx
[root@station10 ~]# ss -tnl | grep :80
LISTEN     0      128                       *:80                       *:*

wKioL1M-iIOi9OZhAAGtkFUuZKo251.jpg

ok!在個人宿主機上面沒有問題,下面是移植操做:

5.移植nginx

(1)執行bincp腳本移植nginx命令

[root@station10 ~]# sh bincp.sh
A command: nginx
Copy /usr/local/sbin/nginx to /mnt/sysroot finished.
Copy /lib64/libpcre.so.0 to /mnt/sysroot finished.
Copy /usr/lib64/libcrypto.so.10 to /mnt/sysroot finished.
A command: q
[root@station10 ~]#


(2)移植nginx配置文件

[root@station10 ~]# cp /etc/nginx/ /mnt/sysroot/etc/ -r
[root@station10 ~]# ls /mnt/sysroot/etc/nginx/
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf
[root@station10 ~]#

(3)移植運行nginx的用戶和組

[root@station10 ~]# openssl passwd  -1
Password:
Verifying - Password:
$1$9Xf6T0AX$SRIJSQJr5vim8Jb8z030k/
[root@station10 ~]#
[root@station10 ~]# tail -1 /etc/passwd >> /etc/passwd
[root@station10 ~]# tail -1 /etc/group >> /mnt/sysroot/etc/group
[root@station10 ~]# tail -1 /etc/shadow >> /mnt/sysroot/etc/shadow
#由於是系統用戶我就不給密碼了

(4)建立nginx的網頁存放目錄

[root@station10 ~]# mkdir /mnt/sysroot/usr/local/html
[root@station10 ~]# vim /mnt/sysroot/usr/local/html/index.html #→建立測試頁

(5)提供nginx服務腳本

[root@station10 ~]# vim /mnt/sysroot/etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
# Source function library.
. /etc/rc.d/init.d/functions
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
# Source networking configuration.
. /etc/sysconfig/network
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
nginx="/usr/local/sbin/nginx"
prog=$(basename $nginx)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
lockfile=/var/lock/subsys/nginx
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
force_reload() {
    restart
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
rh_status() {
    status $prog
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
rh_status_q() {
    rh_status >/dev/null 2>&1
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
[root@station10 ~]# chmod +x /mnt/sysroot/etc/rc.d/init.d/nginx
#建立開關機服務腳本
[root@station10 ~]# cd /mnt/sysroot/etc/rc.d/rc3.d
[root@station10 rc3.d]# ln -sv ../init.d/nginx nginx.start
`nginx.start' -> `init.d/nginx'
[root@station10 rc3.d]# ln -sv ../init.d/nginx nginx.stop
`nginx.stop' -> `init.d/nginx'
[root@station10 rc3.d]# sync

OK! 如今差很少了,我先啓動測試一下有沒有問題

Picture-1(開機後)

wKioL1M_oHuTi9wmAAELLtPVShc811.jpg

Picture-2(用win訪問,沒有問題)

wKiom1M_pMrgIiqoAAGtC3eixac471.jpg

Picture-3(再次遠程訪問),這裏先給個人那個普通用戶建立一個家目錄,而後用他來遠程登陸

wKiom1M_ogHzesUrAACT42CJR2M270.jpg

wKioL1M_pKKQwPHEAAGuNeVa_CE248.jpg

wKiom1M_pMrioOO-AADH6TU6ndg785.jpg


ok!自此兩個應用程序的移植已完成;

上篇博文中提到的bincp.sh腳本請點擊附件下載,各軟件包這裏我就不提供了,有興趣的朋友嘗試一下吧;

t_0003.gif小生能力有限,有問題請留言!

相關文章
相關標籤/搜索