馬哥教育網絡班20期+第10周課程練習

 1. 請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情)

1. 加電自檢:POST
    通電后主板CMOS中的BIOS(基本輸入輸出系統)將進行硬件自檢并簡單設置,根據硬盤等
    相關設備情況進入下一步
    BIOS:Basic Input and Output System基本輸入輸出系統
2. 引導加載次序:BOOT Sequence
    按次序查找各引導設備,第一個有引導程序的設備即為本次啟動用到設備
3. 引導加載器:bootloader
    用來引導系統加載的程序
    ·windows: ntloader
    ·Linux:
        LILO:LInux Loader(早期)
        GRUB: GRand Uniform Bootloader(統一引導加載器)
            GRUB 0.X: GRUB Legacy(GRUB傳統版)
            GRUB 1.x: GRUB2(CentOS 7)
功能:
    提供一個菜單,允許用戶選擇要啟動系統或不同的內核版本;把用戶選定的內核裝載到內存
    中的特定空間中,解壓、展開,并把系統控制權移交給內核。
MBR:
  446:bootloader
  64:fat
  2:55AA
GRUB:
4. GRUB引導加載器
    GRUB 是bootloader階段的引導程序,CentOS主要通過GRUB(GRand Unified 
    Bootloader)引導。
(1)GRUB引導步驟:grub legacy       
stage1: mbr(磁盤分區表)         
stage1_5: mbr之后的扇區,讓stage1中的bootloader能識別stage2所在的分區上的文件系統;         
stage2:磁盤分區(/boot/grub/)
    配置文件:/boot/grub/grub.conf ? /etc/grub.conf
stage2及內核等通常放置于一個基本磁盤分區;
(2)GRUB的功用
1.提供菜單、并提供交互式接口  
  e: 編輯模式,用于編輯菜單;  
  c: 命令模式,交互式接口;  
2.加載用戶選擇的內核或操作系統  
  允許傳遞參數給內核  
  可隱藏此菜單  
3.為菜單提供了保護機制  
  為編輯菜單進行認證  
  為啟用內核或操作系統進行認證
(3)GRUB常用命令
·#help: 獲取幫助列表
·#help KEYWORD: 詳細幫助信息
·#find (hd#,#)/PATH/TO/SOMEFILE:查找磁盤#上的#分區的某文件
·#root (hd#,#)把第#磁盤的第#號分區設定為grub的根設備
·#kernel /PATH/TO/KERNEL_FILE: 設定本次啟動時用到的內核文件;額外還可以添加許多內核
支持使用的cmdline參數;例如:init=/path/to/init, selinux=0 表示禁用內核
·#initrd /PATH/TO/INITRAMFS_FILE: 設定為選定的內核提供額外文件的ramdisk;
·#boot: 引導啟動選定的內核;

手動在grub命令行接口啟動系統:
    # grub > root (hd#,#)  ( hd#:磁盤編號   #:分區編號 )
    # grub > kernel /vmlinuz-VERSION-RELEASE ro root=/dev/DEVICE
    # grub > initrd /initramfs-VERSION-RELEASE.img
    # grub > boot
(4)配置文件(grub保護機制)
[root@localhost ~]# cat /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You do not have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /, eg.
#          root (hd0,0)
#          kernel /boot/vmlinuz-version ro root=/dev/sda1
#          initrd /boot/initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title centos (2.6.32-71.el6.i686)
     root (hd0,0)
     kernel /boot/vmlinuz-2.6.32-71.el6.i686 ro 
     root=UUID=e6d7fd68-b2ef-424a-b4f4-f1145d953389 rd_NO_LUKS rd_NO_LVM
            rd_NO_MD rd_NO_DM.UTF-8 KEYBOARDTYPE=pc KEYTABLE=us
            crashkernel=auto rhgb quiet
     initrd /boot/initramfs-2.6.32-71.el6.i686.img
常用配置項
    default=#: 設定默認啟動的菜單項;落單項(title)編號從0開始;
    timeout=#:指定菜單項等待選項選擇的時長;
    splashimage=(hd#,#) /PATH/TO/XPM_PIC_FILE:指明菜單背景圖片文件路徑;
    hiddenmenu:隱藏菜單;
    password [--md5] STRING: 菜單編輯認證;
    title TITLE:定義菜單項“標題”, 可出現多次;
    root (hd#,#):grub查找stage2及kernel文件所在設備分區;為grub的“根”;
    kernel /PATH/TO/VMLINUZ_FILE [PARAMETERS]:啟動的內核
    initrd /PATH/TO/INITRAMFS_FILE: 內核匹配的ramfs文件;
    password [--md5] STRING: 啟動選定的內核或操作系統時進行認證;
5. 進入Kernel
    在GRUB中選擇對應的kernel后,kernel會對自身進行初始化
    自身初始化:
1.探測可識別到的所有硬件設備;
2.加載硬件驅動程序;(有可能會借助于ramdisk加載驅動)
    ramdisk:內核中的特性之一:使用緩沖和緩存來實現對磁盤上的文件訪問
      CentOS 5:initrd, 工具程序:mkinitrd
      CentOS 6:initramfs, 工具程序:mkinitrd,dracut
3.以只讀方式掛載根文件系統;
4.運行用戶空間的第一個應用程序:/sbin/init
init程序的類型
SysV: init, CentOS 5
   配置文件:/etc/inittab
Upstart: init, CentOS 6
   配置文件:/etc/inittab, /etc/init/*.conf
Systemd:systemd, CentOS 7
   配置文件:/usr/lib/systemd/system, /etc/systemd/system
系統初始化小結:
    POST --> BootSequence (BIOS) --> Bootloader(MBR) --> kernel(ramdisk) --> 
    rootfs(只讀) --> init
6. 運行init
    CentOS 6的init位置為/sbin/init,共分7個級別
6.1運行級別:為了系統的運行或維護等應用目的而設定
  0:關機
  1:單用戶模式(root, 無須登錄), single, 維護模式;
  2: 多用戶模式,會啟動網絡功能,但不會啟動NFS;維護模式;
  3:多用戶模式,正常模式;文本界面;
  4:預留級別;可同3級別;
  5:多用戶模式,正常模式;圖形界面;
  6:重啟
默認級別:3,5
切換級別:init ?
如:# init 0 表示關機
查看級別:
    # runlevel
    # who -r
[root@localhost ~]# runlevel
N 5
[root@localhost ~]# who -r
運行級別 5 2016-08-10 11:09
6.2 配置文件:/etc/inittab,
CenOS 6的upstart程序配置文件還包括/etc/init/*.conf
    配置文件格式為:id:runlevel:action:process
每行定義一種action以及與之對應的process(進程)
id:指入口標識符,它是一個字符串,對于getty或mingetty等其他login程序項,
要求id與tty的編號相同,否則getty程序將不能正常工作。
runlevel:init的系統運行級別
action:是用來定義后面process的運行方式,常用的有下面幾種
wait: 切換至此級別運行一次;
respawn:此process終止,就重新啟動之;
initdefault:設定默認運行級別;process省略;
sysinit:設定系統初始化方式,此處一般為指定/etc/rc.d/rc.sysinit
process:為具體的執行程序
查看本機配置文件顯示為
    id:5:initdefault:表示默認運行級別5,即多用戶模式的圖形界面
[root@localhost ~]# cat /etc/inittab | tail -10
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
#
id:5:initdefault:
如果設定系統初始化方式為:10:0:wait:/etc/rc.d/rc 0 表示讀取/etc/rc.d/rc3.d/下的配置文件
rc 0 相當于讀取/etc/rc.d/rc0.d/  
打開此目錄下的文件名為:K或S+2個數字+文件名
[root@localhost ~]# ls /etc/rc.d/rc0.d
K01certmonger    K72autofs         K87irqbalance
K01smartd      K73ypbind         K87restorecond
K02avahi-daemon    K74acpid          K87rpcbind
K02oddjobd     K74haldaemon       K88auditd
K05atd       K74nscd           K88nslcd
K10cups       K74ntpd           K88rsyslog
K10psacct      K75netfs          K89portreserve
K10saslauthd     K75ntpdate         K89rdisc
K15httpd      K75udev-post       K90network
K16abrtd      K80kdump          K92ip6tables
K25sshd       K80sssd          K92iptables
K30postfix     K83bluetooth       K95cgconfig
K43vmware-tools-thinprint  K83nfslock   K95firstboot
K50dnsmasq     K83rpcgssd         K99cpuspeed
K50netconsole    K83rpcidmapd        K99lvm2-monitor
K50snmpd      K84NetworkManager      K99microcode_ctl
K50snmptrapd    K84wpa_supplicant      K99sysstat
K60crond      K85mdmonitor         K99vmware-tools
K60nfs       K85messagebus         S00killall
K69rpcsvcgssd   K86cgred            S01halt
說明:
K開頭的文件,K##*:##運行次序;數字越小,越先運行;數字越小的服務,通常為依賴到別的服務;
S開頭的文件,S##*:##運行次序;數字越小,越先運行;數字越小的服務,通常為被依賴到的服務;
7.系統初始化腳本:/etc/rc.d/rc.sysinit,會在系統啟動時運行一次
主要作用
1.設置主機名;
2.設置歡迎信息;
3.激活udev和selinux;
4.掛載/etc/fstab文件中定義的文件系統;
5.檢測根文件系統,并以讀寫方式重新掛載根文件系統;
6.設置系統時鐘;
7.激活swap設備;
8.根據/etc/sysctl.conf文件設置內核參數;
9.激活lvm及software raid設備;
10.加載額外設備的驅動程序;
11.清理操作;
8.啟動系統服務
centos6下的系統服務腳本都放在/etc/rc.d/init.d或者/etc/init.d中,因為建立了鏈接,
所以內容是一樣的
[root@localhost ~]# ll /etc/init.d
lrwxrwxrwx. 1 root root 11 7月   4 17:17 /etc/init.d -> rc.d/init.d
[root@localhost ~]# ls !$
ls /etc/init.d
abrtd         haldaemon       nfs          rsyslog
acpid         halt            nfslock      sandbox
atd           httpd           nscd         saslauthd
auditd        ip6tables       nslcd        single
autofs        iptables        ntpd         smartd
avahi-daemon  irqbalance      ntpdate      snmpd
bluetooth     kdump           oddjobd      snmptrapd
certmonger    killall         portreserve  sshd
cgconfig      lvm2-monitor    postfix      sssd
cgred         mdmonitor       psacct       sysstat
cpuspeed      messagebus      rdisc        udev-post
crond         microcode_ctl   restorecond  vmware-tools
cups          netconsole      rpcbind      vmware-tools-thinprint
dnsmasq       netfs           rpcgssd      wpa_supplicant
firstboot     network         rpcidmapd    ypbind
functions     NetworkManager  rpcsvcgssd
chkconfig命令:
(1)查看服務在所有級別的啟動或關閉設定情形:
chkconfig [--list] [name]
[root@localhost ~]# chkconfig --list
NetworkManager 0:關閉      1:關閉      2:啟用      3:啟用      4:啟用      5:啟用      6:關閉
abrtd              0:關閉      1:關閉      2:關閉      3:啟用      4:關閉      5:啟用      6:關閉
acpid              0:關閉      1:關閉      2:啟用      3:啟用      4:啟用      5:啟用      6:關閉
... ...
(2)添加:
SysV的服務腳本放置于/etc/rc.d/init.d (/etc/init.d)
# chkconfig --add name
(3)刪除:
# chkconfig --del name
(4)修改指定的鏈接類型
# chkconfig [--level levels] name <on|off|reset>
  --level LLLL: 指定要設置的級別;省略時表示2345;
注意:正常級別下,最后啟動一個服務S99local沒有鏈接至/etc/rc.d/init.d一個服務腳本,
而是指向了/etc/rc.d/rc.local腳本;因此,不便或不需寫為服務腳本放置于/etc/rc.d/init.d/
目錄,且又想開機時自動運行的命令,可直接放置于/etc/rc.d/rc.local文件中;
                                                        mingetty會調用login程序9. 設置登陸終端
根據終端配置打印登陸提示符
初始化階段小結為:
/sbin/init --> (/etc/inittab) --> 設置默認運行級別 --> 運行系統初始腳本、
完成系統初始化 --> 關閉對應下需要關閉的服務,啟動需要啟動服務 --> 設置登錄終端
總結
POST --> Boot Sequence(BIOS) --> Boot Loader (MBR) --> Kernel(ramdisk) -->
rootfs --> switchroot --> /sbin/init -->(/etc/inittab, /etc/init/*.conf) -->
設定默認運行級別 --> 系統初始化腳本 --> 關閉或啟動對應級別下的服務 --> 啟動終端

2、為運行于虛擬機上的CentOS 6添加一塊新硬件,提供兩個主分區;

(1) 為硬盤新建兩個主分區;并為其安裝grub;

(2) 為硬盤的第一個主分區提供內核和ramdisk文件; 為第二個分區提供rootfs;

(3) 為rootfs提供bash、ls、cat程序及所依賴的庫文件;

(4) 為grub提供配置文件;

(5) 將新的硬盤設置為第一啟動項并能夠正常啟動目標主機;

(1)為硬盤新建兩個主分區;/dev/sdb1; /dev/sdb2
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x14cc3f0b.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (655-2610, default 655):
Using default value 655
Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): +5G
 
Command (m for help): p
 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x14cc3f0b
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux
/dev/sdb2             655        1308     5253255   83  Linux
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partx -a /dev/sdb  # 通知內核重新讀取硬盤分區
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
[root@localhost ~]# mkfs -t ext4 /dev/sdb1 # 設置文件格式為ext4
[root@localhost ~]# mkfs -t ext4 /dev/sdb2
(2)為硬盤的第一個主分區提供內核和ramdisk文件;為第二個分區提供rootfs;
[root@localhost ~]# mkdir /mnt/boot /mnt/sysroot
[root@localhost ~]# mount /dev/sdb1 /mnt/boot/
[root@localhost ~]# mount /dev/sdb2 /mnt/sysroot/
[root@localhost ~]# ls /mnt/boot
lost+found
[root@localhost ~]# ls /mnt/sysroot
lost+found
[root@localhost ~]# cp /boot/vmlinuz-2.6.32-71.el6.i686 /mnt/boot/vmlinuz
[root@localhost ~]# cp /boot/initramfs-2.6.32-71.el6.i686.img /mnt/boot/initramfs.img
[root@localhost ~]# ls /mnt/boot
initramfs.img  lost+found  vmlinuz
# 為其安裝grub
[root@localhost ~]# grub-install --root-directory=/mnt /dev/sdb
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.
 
(fd0)   /dev/fd0
(hd0)   /dev/sda
(hd1)   /dev/sdb
[root@localhost sysroot]# mkdir -pv etc bin sbin lib lib64 dev proc sys tmp var usr home root mnt media
[root@localhost sysroot]# ls
bin  etc   lib    lost+found  mnt   root  sys  usr
dev  home  lib64  media       proc  sbin  tmp  var
(3)為rootfs提供bash、ls、cat程序及所依賴的庫文件;
[root@localhost ~]# mkdir /mnt/sysroot/{bin,lib64}/
[root@localhost sysroot]# cp /bin/bash /bin/ls /bin/cat /mnt/sysroot/bin/
[root@localhost sysroot]# cp `ldd /bin/bash | sed '1d'| grep -Eo "/.*[0-9] "` /mnt/sysroot/lib64/
[root@localhost sysroot]# cp `ldd /bin/ls | sed '1d'| grep -Eo "/.*[0-9] "` /mnt/sysroot/lib64/
[root@localhost sysroot]# cp `ldd /bin/cat | sed '1d'| grep -Eo "/.*[0-9] "` /mnt/sysroot/lib64/
(4)為grub提供配置文件;
[root@localhost sysroot]# vim /mnt/boot/grub/grub.conf
[root@localhost sysroot]# cat /mnt/boot/grub/grub.conf
default=0
timeout=5
title CentOS (Express)
root (hd0,0)
kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs.img
(5) 將新的硬盤設置為第一啟動項并能夠正常啟動目標主機;
關閉用來設置的虛擬機,新建虛擬機,將原來添加的虛擬磁盤加載到新虛擬機,可正常啟動bash,并能執行ls和cat命令
bash-4.1 # ls /bin/
bash cat ls

3、制作一個kickstart文件以及一個引導鏡像。描述其過程。

一、kickstart文件的格式:
(1)命令段:指明各種安裝前配置,如鍵盤類型等;
(2)程序包段:指明要安裝的程序包組或程序包,不安裝的程序包等;
    %packages(開始)
    @group_name(包組)
    package(要安裝的程序包)
    -package(不安裝的程序包)
    %end(結束)
(3)腳本段:
    %pre: 安裝前腳本
        運行環境:運行于安裝介質上的微型Linux環境
    %post: 安裝后腳本
        運行環境:安裝完成的系統;
(4)命令段中的命令:
# 必備命令
authconfig: 認證方式配置
    authconfig --useshadow  --passalgo=sha512(密碼加密格式)
bootloader:bootloader的安裝位置及相關配置
    bootloader --location=mbr --driveorder=sda 
    --append="crashkernel=auto crashkernel=auto rhgb rhgb quiet quiet"
keyboard: 設定鍵盤類型
lang: 語言類型
part: 創建分區
rootpw: 指明root的密碼
timezone: 時區
# 可選命令
install OR upgrade(安裝或升級)
text: 文本安裝界面
network
firewall
selinux
halt(關機)
poweroff(關機)
reboot(重啟)
repo(安裝過程中額外用到的)
user:安裝完成后為系統創建新用戶
url: 指明安裝源
二、創建kickstart文件的方式:
(1)直接手動編輯;
  依據某模板修改;
(2)可使用創建工具:system-config-kickstart (CentOS 6)
  依據某模板修改并生成新配置;
三、檢查ks文件的語法錯誤:ksvalidator
  # ksvalidator /PATH/TO/KICKSTART_FILE
四、創建引導光盤:
  # mkisofs -R -J -T -v &ndash;no-emul-boot &ndash;boot-load-size 4 &ndash;
  boot-info-table -V &ldquo;CentOS 6.8 x86_64 boot&rdquo;-b isolinux/isolinux.bin 
   -c isolinux/boot.cat -o /root/boot.iso myiso/

1、anaconda的配置方式:
    (1) 交互式配置方式;
    (2) 通過讀取事先給定的配置文件自動完成配置;
    a)按特定語法給出的配置選項;
       kickstart文件;
      b)安裝引導選項:
        text: 文本安裝方式
        method: 手動指定使用的安裝方法
      c)與網絡相關的引導選項:
        ip=IPADDR
        netmask=MASK
        gateway=GW
        dns=DNS_SERVER_IP
        ifname=NAME:MAC_ADDR
      d)與遠程訪問功能相關的引導選項:
        vnc
        vncpassword='PASSWORD'
      e)指明kickstart文件的位置
        ks=
        DVD drive: ks=cdrom:/PATH/TO/KICKSTART_FILE
        Hard drive: ks=hd:/device/drectory/KICKSTART_FILE
        HTTP server: ks=http://host:port/path/to/KICKSTART_FILE
        FTP server: ks=ftp://host:port/path/to/KICKSTART_FILE
        HTTPS server: ks=https://host:port/path/to/KICKSTART_FILE
      f)啟動緊急救援模式:
        rescue
2、 kickstart文件的格式:
      a)命令段:指明各種安裝前配置,如鍵盤類型等;
      b)程序包段:指明要安裝的程序包組或程序包,不安裝的程序包等;
            %packages
            @group_name
            package
            -package
            %end
      c)腳本段:
            %pre: 安裝前腳本
    d)運行環境:運行于安裝介質上的微型Linux環境
           %post: 安裝后腳本
       運行環境:安裝完成的系統;
    
   命令段中的命令:
       必備命令
            authconfig: 認證方式配置
            authconfig --useshadow  --passalgo=sha512
            bootloader:bootloader的安裝位置及相關配置
            bootloader --location=mbr --driveorder=sda 
                        --append="crashkernel=auto 
                        crashkernel=auto rhgb rhgb quiet quiet"
            keyboard: 設定鍵盤類型
            lang: 語言類型
            part: 創建分區
            rootpw: 指明root的密碼
            timezone: 時區
        可選命令
            install OR upgrade
            text: 文本安裝界面
            network
            firewall
            selinux
            halt
            poweroff
            reboot
            repo
            user:安裝完成后為系統創建新用戶
            url: 指明安裝源
3、創建kickstart文件的方式:
   (1) 直接手動編輯;
       依據某模板修改;
   (2) 可使用創建工具:system-config-kickstart (CentOS 6)
       依據某模板修改并生成新配置;
4、檢查ks文件的語法錯誤:ksvalidator
   # ksvalidator /PATH/TO/KICKSTART_FILE
5、創建引導光盤:
   # mkisofs -R -J -T -v &ndash;no-emul-boot &ndash;boot-load-size 4 &ndash;
   boot-info-table -V &ldquo;CentOS 6.8 x86_64 boot&rdquo; 
   -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/boot.iso myiso/
[root@localhost ~]# yum install system-config-kickstart
[root@localhost ~]# system-config-kickstart # 進入圖形界面

4、寫一個腳本

(1) 能接受四個參數:start, stop, restart, statusstart: 輸出“starting 腳本名 finished.”

(2) 其它任意參數,均報錯退出;

#!/bin/bash
# chkconfig: - 88 12
# description:test service script
prog=$(basename $0)
lockfile=/var/lock/subsys/$prog
start(){
    if [ -e $lockfile ];then
    echo "$prog is aleady running."
    return 0
    else
    touch $lockfile
    [ $? -eq 0 ] && echo "starting $prog finished."
    fi
}
stop(){
    if [ -e $lockfile ];then
    rm -f $lockfile && echo "stop $prog ok."
    else
    echo "$prog is stopped yet."
    fi
}
status(){
    if [ -e $lockfile ];then
    echo "$prog is running."
    else
    echo "$prog is stopped."
    fi
}
usage(){
    echo "Usage:$prog {start|stop|restart|status}"
}
if [ $# -lt 1 ] || [ $1 != "start" -a $1 != "stop" -a $1 != "restart" -a $1 != "status"  ];then
    usage
    exit 1
fi
case $1 in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    stop
    start
    ;;
status)
    status
    ;;
*)
    usage
esac

5、寫一個腳本,判斷給定的用戶是否登錄了當前系統;

(1) 如果登錄了,則顯示用戶登錄,腳本終止;

(2) 每3秒鐘,查看一次用戶是否登錄;

方法一:(while循環)
#/bin/bash
read -p "Enter a user name:" username
while true;do
    if who | grep "^$username" &> /dev/null;then
        break
    fi
    sleep 3
done
echo "$username logged on!"
方法二:(until循環)
#/bin/bash
read -p "Enter a user name:" username
until who | grep "^$username" &> /dev/null;do
    sleep 3
done
echo "$username logged on!"

6、寫一個腳本,顯示用戶選定要查看的信息;

cpu) display cpu info

mem) display memory info

disk) display disk info

quit) quit

非此四項選擇,則提示錯誤,并要求用戶重新選擇,只到其給出正確的選擇為止;

#/bin/bash
cat << EOF
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
========================
EOF
read -p "Enter a option:" option
while [ "$option" ! = "cpu" -a "$option" ! = "mem" -a "$option" ! = "disk" -a "$option" ! = "disk" -a "$option" ! = "quit" ];do
    read -p "Wrong option,please enter again:" option
done
case "$option" in
cpu)
    lscpu
    ;;
mem)
    cat /proc/meminfo
    ;;
disk)
    fdisk -l
    ;;
*)
    echo "Quit..."
    exit 0
    ;;
esac

7、寫一個腳本

(1) 用函數實現返回一個用戶的UID和SHELL;用戶名通過參數傳遞而來;

(2) 提示用戶輸入一個用戶名或輸入“quit”退出;當輸入的是用戶名,則調用函數顯示用戶信息;當用戶輸入quit,則退出腳本;

進一步地:顯示鍵入的用戶相關信息后,再次提醒輸出用戶名或quit:

#!/bin/bash
#
function showinfo {
if id $name &> /dev/null;then
    echo "UID : `id -u $name`"
    echo "SHELL:`grep -E '^('$name')\>' /etc/passwd | cut -d: -f7`"
    read -p "Enter a username or quit:" name
else
    read -p  "wrong argument,plsease enter a username or quit:" name
fi
}
read -p "Enter a username or quit:" name
while [ "$name" != "quit" ];do
    showinfo $name
done

8、寫一個腳本,完成如下功能(使用函數)

(1) 提示用戶輸入一個可執行命令的名字;獲取此命令依賴的所有庫文件;

(2) 復制命令文件至/mnt/sysroot目錄下的對應的rootfs的路徑上,例如,如果復制的文件原路徑是/usr/bin/useradd,則復制到/mnt/sysroot/usr/bin/目錄中;

(3) 復制此命令依賴的各庫文件至/mnt/sysroot目錄下的對應的rootfs的路徑上;規則同上面命令相關的要求;(ldd /bin/ls | grep -o "/[^[:space:]]\{1,\}"可篩選出命令對應的庫文件) 

#!/bin/bash
read -p "Enter a exeable commond:" cmd
path() {
     whereis $cmd | cut -d' ' -f2
}
mkdir /mnt/sysroot/{bin,lib}
comfilecp(){
    cp $(path $1) /mnt/sysroot$(path $1)
    echo "cp command file $(path $1) to /mnt/sysroot sucess."
}
libfilecp(){
    libdir=`ldd $(path $1)| sed '1d'| grep -Eo "/.*[0-9] "`
    for i in $libdir;do
        cp $i /mnt/sysroot$i
        echo "cp lib file $line to /mnt/sysroot sucess."
    done
}
comfilecp
libfilecp

原創文章,作者:二極管,如若轉載,請注明出處:http://www.www58058.com/34335

(0)
二極管二極管
上一篇 2016-08-15
下一篇 2016-08-15

相關推薦

  • 8月5日課堂及課后作業

    課堂作業 1.找出ifconfig命令結果中的IP地址 [root@localhost ~]# ifconfig |head -2|grep "inet" |tr " " ":"|cut -d:&nb…

    2016-08-08
  • 2016/08/03:用戶管理與權限

    名稱解析:User IDentifier UID 組名:Group IDentifier GID                                     &nbsp…

    學員作品 2016-08-05
  • ?Linux中的用戶與組

    一、用戶與UID 1.用戶(User)分為管理員與普通用戶 2.對于Linux系統而言,通過UID來界定某用戶是屬于哪個用戶,而不是用戶名 3.查看某用戶的UID可以通過id命令實現,Centos6.8與7.2系統上的UID部分起始數值有所不同 Centos7.2 [root@localhost home]# id root u…

    學員作品 2016-08-10
  • 【福利貼-招聘】- 維護工程師

    地點:黃浦–人民廣場  職位:技術支持/維護 要求:招1人| 大專| 1-2年 薪資范圍: 8-12k 職位職能:技術支持/維護工程師 崗位職責: 1.熟悉Linux及其遠程維護; 2.熟練掌握Linux網絡安裝、日常管理、安全、備份、恢復、故障處理、日志分析等技能; 3.熟悉Web、mysql優化管理、apache等常見的問題及處理 …

    Linux職位 2015-05-14
  • 馬哥教育網絡班20期-第十周課程作業

    請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情) 1)POST:加電自檢 2)BOOT Sequence:按次序查找各引導設備,第一個有grub的設備即為本次啟動用到設備 3)加載kernel,先自身初始化,探測可識別到的所有硬件設備,加載硬件驅動程序,以只讀方式掛載根文件系統,運行用戶空間的第一個應用程序:/sbin/init 4)i…

    學員作品 2016-09-08
  • 91-ansible

    一. Ansible      Configuration、Command and Control

    2016-11-18

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-08-17 14:22

    寫的很好,排版也很棒,加油

欧美性久久久久