首页 > 代码库 > Linux01-BASH脚本编程之系统裁剪51

Linux01-BASH脚本编程之系统裁剪51

上接(BASH脚本编程之拷贝系统库文件50)


重新编辑grub为静默模式:

    1.vim /mnt/boot/grub/grub.conf

default=0

timeout=3

title Nick Linux(2.6.18)

kernel /vmlinuz ro root/dev/hda2 quiet

initrd /initrd.gz


如何实现终端提示信息:

    2.复制宿主机文件:

        cp /etc/issue /mnt/sysroot/etc/ 

    3.重新编辑issue信息:

        vim /mnt/sysroot/etc/issue

Nick release 5.9 (Final)

Kernel \r on an \m


http://www.mageedu.com


如何挂载/etc/fstab中定义的其他文件系统:

  • rc.sysinit:挂载/etc/fstab中定义的其它文件系统

    a)怎么判断设备是否挂载:

        awk ‘{print $1}‘ /proc/mounts | grep "/dev/hda2"

    b)swap设备不能使用mount直接挂载:

        grep -v "\<swap\>" /etc/fstab | awk ‘{print $1}‘

        grep -E -v "\<swap|proc|sysfs\>" /etc/fstab | awk ‘{print $1}‘

        grep -E -v "\<swap|proc|sysfs\>" /etc/fstab | awk ‘{print $1}‘ | while read LINE;do awk ‘{print $1}‘ /proc/mounts | grep "^$LINE";done


    4.cd /mnt/sysroot/

    5.vim etc/rc.d/rc.sysinit修改添加:

#!/bin/bash

#

. /etc/rc.d/init.d/functions


echo -e "\tWelcome to\033[34mNick Team\033[0m Linux."


echo "Remount rootfs ..."

mount -n -o remount,rw /

[ $? -eq 0 ] && success "Remount rootfs" || failure "Remount rootfs"


mount -a

[ $? -eq 0 ] && success "Mount others filesystem" || failure "Mount others filesystem"


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"


echo "Initialization network device ..."

/sbin/insmod    /lib/modules/mii.ko

/sbin/insmod    /lib/modules/pcnet32.ko

[ $? -eq 0 ] && success "Initialization network device" || failure "Initialization network device"


ifconfig lo 127.0.0.1/8

[ $? -eq 0 ] && success "Activating loopback network device" || failure "Activating loopback network device"


如何设定内核参数/etc/sysctl.conf:

    6.运行binary.sh移植sysctl命令;

    7.创建/etc/sysctl.conf配置文件:

        cd /mnt/sysroot/

        vim /etc/sysctl.conf

net.ipv4.ip_forward = 1

    8.开机自动生效:

        vim /etc/rc.d/rc.sysinit最后行添加:

sysctl -p &> /dev/null

[ &? -eq 0 ] && success "Set kernel parameter" || failure "Set kernel parameter"

    9.启动新主机,测试:

        cat /proc/sys/net/ipv4/ip_forward


如何实现用户运行:

  • PAM:Pluggable Authentication Module(可插入式配置文件);

  • RHEL5 通过/etc/pam.d/*来实现用户的登录、认证等等;

  • 绕过PAM,RHEL的登录显示通过/bin/login打印显示:

  • login程序如何实现用户登录和认证:

    a)nsswitch(作为login的中间层):Network Service Switch

    b)框架:/etc/passwd,/etc/shadow,/etc/group,/etc/gshadow

        库:libnss_file.so,libnss_nis.so,libnsss_ldap.so ...

        配置文件:/etc/nsswitch.conf

        库文件:/lib/libnss*;/usr/lib/libnss*


    10.复制用户验证库文件到/lib:

        cp -d /lib/libnss_files* /mnt/sysroot/lib/  --->-d选项,保留文件链接

        ls -l /mnt/sysroot/lib/

    11.复制用户验证库文件到/usr/lib:

        cp -d /usr/lib/libnss_files.so /mnt/sysroot/usr/lib/

        cp -d /usr/lib/libnss3.so /usr/lib/libnssckbi.so /usr/lib/libnssutil3.so /mnt/sysroot/usr/lib/

        ls -l /mnt/sysroot/usr/lib/

    12、编辑配置文件:

        cp /etc/nsswitch.conf /mnt/sysroot/etc/

        vim /mnt/sysroot/etc/nsswitch.conf

passwd:files

shadow:files

group:files

hosts:filesdns

    13、创建用户:

        添加hadoop用户

        grep -E "^(root|hadoop)\>" /etc/passwd > /mnt/sysroot/etc/passwd

        grep -E "^(root|hadoop)\>" /etc/shadow > /mnt/sysroot/etc/shadow

        grep -E "^(root|hadoop)\>" /etc/group > /mnt/sysroot/etc/group

    14、移植mingetty,passwd,useradd,userdel,usermod,groupadd命令;

    15、修改inittab文件:

        cd /mnt/sysroot/

        vim etc/inittab修改:

1:2345:respawn:/sbin/mingetty tty1

2:2345:respawn:/sbin/mingetty tty2

    16.下载login到/mnt/sysroot/bin

    17.添加执行权限:chmod +x /mnt/sysroot/bin/login 

    18.启动新主机,init 3 看是否提示用户登录;


创建用户配置文件:

    19.创建用户命令提示符配置文件:

        cd /mnt/sysroot/

        vim root/.bash_profile

PS1=‘[\u@\h \W]\$‘

export PS1


单用户模式:

    20.修改root目录权限:

        cd /mnt/sysroot/

        chmod -R og=--- root/

    21.定义inittab文件的1级别:

        vim etc/inittab添加一行:

l1:1:wait:/etc/rc.d/rc 1

    22.创建1级别服务链接:

        cd /mnt/sysroot/etc/rc.d 

        mkdir rc1.d 

        cd rc1.d 

        ln -sv ../init.d/network K90network

        ln -sv ../init.d/tserver K33tserver

    23.创建单用户singer文件:

        vim etc/rc.d/init.d/single

#!/bin/bash

#

case $1 in

start)

 ;;

*)

        echo "Usage:Single start"

        ;;

esac


exec /sbin/init S

添加执行权限:chmod +x etc/rc.d/init.d/single

    24、创建单用户服务链接:

        cd rc1.d 

        ln -sv ../init.d/single S98single

    25、开始测试新主机的单用户模式:

        init 6 

        e

        选择kernel /vmlinuz ro root/dev/hda2 quiet输入e

        输入1--> 回车

        b键开始引导.

本文出自 “Nick Liu的博文” 博客,请务必保留此出处http://zkhylt.blog.51cto.com/3638719/1430050