2016年1月19日課程做業

一、描述centos6系統開機啓動流程node

CentOS6啓動系統時大體通過如下4個大模塊linux

wKiom1asvyqSvNi6AAEVSN6euIs677.png

BIOS加電自檢git

POST,PowerOnSelfTest,系統會對每個硬件設備進行自檢,待經過以後,會去檢測有操做系統引導啓動的設備,進而完成系統的順利啓動,這點跟咱們的Winodws是同樣的。正則表達式

MBR引導shell

MBR(Master Boot Record)通常指的是磁盤0磁道1扇區,共512個字節,前446個字節是BootLoder,後 4*16 的 64 個字節是存放分區信息的,最後 2 個字節是校驗信息,通常是 55AA。vim

GRUB(GRand Unified Bootloader)centos

GRUB程序的組成部分:數組

[root@jimjimlv ~]# ls -lh /boot/grub/ 
total 275K 
-rw-r--r--. 1 root root   63 Jan 16 22:24 device.map 
-rw-r--r--. 1 root root  14K Jan 16 22:24 e2fs_stage1_5 
-rw-r--r--. 1 root root  13K Jan 16 22:24 fat_stage1_5 
-rw-r--r--. 1 root root  12K Jan 16 22:24 ffs_stage1_5 
-rw-------. 1 root root 1.3K Jan 17 19:19 grub.conf
-rw-r--r--. 1 root root  12K Jan 16 22:24 iso9660_stage1_5 
-rw-r--r--. 1 root root  13K Jan 16 22:24 jfs_stage1_5 
lrwxrwxrwx. 1 root root   11 Jan 16 22:24 menu.lst -> ./grub.conf 
-rw-r--r--. 1 root root  12K Jan 16 22:24 minix_stage1_5 
-rw-r--r--. 1 root root  15K Jan 16 22:24 reiserfs_stage1_5 
-rw-r--r--. 1 root root 1.4K Nov 15  2010 splash.xpm.gz 
-rw-r--r--. 1 root root  512 Jan 16 22:24 stage1 
-rw-r--r--. 1 root root 124K Jan 16 22:24 stage2
-rw-r--r--. 1 root root  12K Jan 16 22:24 ufs2_stage1_5 
-rw-r--r--. 1 root root  12K Jan 16 22:24 vstafs_stage1_5 
-rw-r--r--. 1 root root  14K Jan 16 22:24 xfs_stage1_5
Stage1:MBR加載程序
Stage1.5:MBR以後的扇區,讓Stage1中的Bootloader可以識別Stage2所在的分區上的文件系統;
Stage2:磁盤分區(/boot/grub/)
配置文件:/boot/grub/grub.confg<-----/etc/grub.conf
[root@jimjimlv ~]# 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 have a /boot partition.  This means that 
#          all kernel and initrd paths are relative to /boot/, eg. 
#          root (hd0,0) 
#          kernel /vmlinuz-version ro 
root=/dev/mapper/vg_jimjimlv-lv_root 
#          initrd 
/initrd-[generic-]version.img 
#boot=/dev/sda
default=0   #默認啓動的內核Title,0表示是第一個 
timeout=5   #默認等待時間 
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz 
password --md5 nico1987   
#指定菜單的背景圖片的路徑。 
hiddenmenu  #隱藏菜單 
title CentOS 6 
(2.6.32-573.el6.x86_64)   # 標題名,用戶可自定義 
    root (hd0,0)          # 指定 grub 的根位置
    kernel /vmlinuz-2.6.32-573.el6.x86_64 ro 
root=/dev/mapper/vg_jimjimlv-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD 
SYSFONT=latarcyrheb-sun16 rd_LVM_LV=vg_jimjimlv/lv_swap crashkernel=auto 
rd_LVM_LV=vg_jimjimlv/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet 
    initrd /initramfs-2.6.32-573.el6.x86_64.img 
title CentOS 6 (My 
Linux) 
    root (hd0,0) 
    kernel /vmlinuz-2.6.32-573.el6.x86_64 ro 
root=/dev/mapper/vg_jimjimlv-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD 
SYSFONT=latarcyrheb-sun16 rd_LVM_LV=vg_jimjimlv/lv_swap crashkernel=auto 
rd_LVM_LV=vg_jimjimlv/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet 
    initrd /initramfs-2.6.32-573.el6.x86_64.img 
        password --md5 
nico1987

initramfs 是以 gzip 壓縮的 cpio 格式的文件。內核啓動時將他做爲一個臨時的根文件系統,grub 的 stage2 將initrd加載到內存裏,讓後將其中的內容釋放到內容中,瀏覽器

加載內核ruby

啓動init進程:進程是系統啓動的第一個進程。其餘進程都是有此進程控制fork出來的,也就是說,init是其餘進程的「祖先」,PID=1。

init啓動後所作的事情:

一、讀取/etc/inittab來讀取默認級別 假設:讀取到的默認級別是 5

功能:它是爲了系統的運行或維護等應用目的而設定的:

0-6:7個級別
0 : 關機
1 : 單用戶模式式(root,無需登陸),single,維護模式
2 : 多用戶模式,會啓動網絡功能,但不會啓動NFS:維護模式
3 : 多用戶模式,正常模式,文本界面
4 : 預留級別,功能跟級別3同樣,經過不使用
5 : 多用戶模式,正常模式,圖形界面
6 : 重啓
默認級別
3,5
切換級別
init # (#表明對應的級別號)
查看級別
[root@jimjimlv ~]# runlevel
N 5
[root@jimjimlv ~]# who -r
run-level 5  2016-01-15 17:07

二、執行初始化系統腳本 /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)清理操做

三、而後執行 /etc/rc.d/rc 腳本

此腳本運行的時候此腳本的時候,會讀取/etc/rc.d/rc5.d/下的全部腳本,來控制系統啓動的時候啓動或者關閉那些服務

[root@jimjimlv ~]# ll /etc/rc.d/rc5.d/ 
total 0 
lrwxrwxrwx. 1 root root 19 Jan 16 22:21 K10saslauthd -> ../init.d/saslauthd 
lrwxrwxrwx. 1 root root 20 Jan 16 22:22 K87multipathd -> ../init.d/multipathd 
lrwxrwxrwx. 1 root root 21 Jan 16 22:21 K87restorecond -> ../init.d/restorecond 
lrwxrwxrwx. 1 root root 20 Jan 16 22:21 K89netconsole-> ../init.d/netconsole 
lrwxrwxrwx. 1 root root 15 Jan 16 22:21 K89rdisc -> ../init.d/rdisc 
lrwxrwxrwx. 1 root root 22 Jan 16 22:22 S02lvm2-monitor -> ../init.d/lvm2-monitor 
lrwxrwxrwx. 1 root root 16 Jan 16 22:22 S07iscsid -> ../init.d/iscsid 
lrwxrwxrwx. 1 root root 19 Jan 16 22:21 S08ip6tables -> ../init.d/ip6tables 
lrwxrwxrwx. 1 root root 18 Jan 16 22:21 S08iptables -> ../init.d/iptables 
lrwxrwxrwx. 1 root root 17 Jan 16 22:21 S10network -> ../init.d/network 
lrwxrwxrwx. 1 root root 16 Jan 16 22:22 S11auditd -> ../init.d/auditd 
lrwxrwxrwx. 1 root root 17 Jan 16 22:21 S12rsyslog -> ../init.d/rsyslog 
lrwxrwxrwx. 1 root root 15 Jan 16 22:22 S13iscsi -> ../init.d/iscsi 
lrwxrwxrwx. 1 root root 19 Jan 16 22:22 S15mdmonitor -> ../init.d/mdmonitor 
lrwxrwxrwx. 1 root root 26 Jan 16 22:22 S25blk-availability -> ../init.d/blk-availability
lrwxrwxrwx. 1 root root 15 Jan 16 22:21 S25netfs -> ../init.d/netfs 
lrwxrwxrwx. 1 root root 19 Jan 16 22:21 S26udev-post -> ../init.d/udev-post 
lrwxrwxrwx. 1 root root 14 Jan 16 22:22 S55sshd -> ../init.d/sshd 
lrwxrwxrwx. 1 root root 17 Jan 16 22:21 S80postfix -> ../init.d/postfix 
lrwxrwxrwx. 1 root root 15 Jan 16 22:21 S90crond -> ../init.d/crond 
lrwxrwxrwx. 1 root root 11 Jan 16 22:21 S99local -> ../rc.local

備註:

  1. # 以上以S開頭的表明開機啓動、K表明開機不啓動

  2. 每一個文件的命名格式,例如:S01sysstat S 開機啓動 01 啓動次序 sysstat 啓動腳本名 

  3. # 啓動的定義是在每一個腳本中定義的:例如 

  4. # # chkconfig: - 85 15 

  5. # 默認級別 啓動次序 不啓動次序

四、執行/etc/rc.d/rc.local腳本,此腳本是啓動過程當中最後啓動的一個腳本

五、系統登陸界面

二、描述/etc/rc.d/sysinit腳本功能;

(1)設置主機名
(2)設置歡迎信息
(3)激活udev和selinux
(4)掛載/etc/fstab文件中定義的文件系統
(5)檢測根文件系統,並以讀寫方式從新掛載根文件系統
(6)設置系統時鐘
(7)激活swap設備
(8)根據/etc/sysctl.conf文件設置內核參數
(9)激活lvm及software raid設備
(10)加載額外設備的驅動程序

三、總結文本處理工具sed及awk的用法;(必須附帶示例)

Sed,Stream EDitor,行編輯器

語法格式:sed [option]... '編輯命令' inputfile..

經常使用選項與參數
                -n:不輸出模式中的內容至屏幕;                 
                -e: 多點編輯; 
                -f /PATH/TO/SCRIPT_FILE: 從指定文件中讀取編輯腳本;           
                -r: 支持使用擴展正則表達式;                 
                -i: 原處編輯;

經常使用編輯命令

a: 新增內容,能夠接字串,並出如今新一一行
d: 刪除,一般後面不接任何東西    
p: 顯示模式空間中的內容,一般與參數sed –n 一塊兒運行    
a \text:在行後面追加文本;支持使用\n實現多行追加;    
i \text:在行前面插入文本;支持使用\n實現多行插入;    
c \text:替換行爲單行或多行文本;    
w /path/to/somefile: 保存模式空間匹配到的行至指定文件中;    
r /path/from/somefile:讀取指定文件的文本流至模式空間中匹配到的行的行後;    
=: 爲模式空間中的行打印行號;    
!: 取反條件;    
s///:支持使用其它分隔符,s@@@,s###;    
替換標記:         
g: 行內全局替換; 
p: 顯示替換成功的行;         
w:/PATH/TO/SOMEFILE:將替換成功的結果保存至指定文件中;

例1:刪除文件/etc/fstab裏面第3~6行的內容

[root@Jimjimlv ~]# cat -n /etc/passwd | sed '3,6d' 
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt

例2:將/etc/fstab文件的第3~6行替換爲」Line 3~6 replaced「

[root@Jimjimlv ~] cat n /etc/passwd | sed '3,6c Line from 2 to 6 being replaced' 
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
Line from 2 to 6 being replaced 
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt

例3:將文件sed.txt開頭的單詞」THIS「替換爲」this「

[root@Jimjimlv sed]# sed 's/^THIS/this/g' sed.txt 
this IS THE TESTING FOR SED 
this IS THE TESTING FOR SED 
this IS THE TESTING FOR SED

例4:刪除/boot/grub/grub.conf文件中全部以空白開頭的行行首的空白字符

[root@Jimjimlv sed]# sed 
's@^[[:space:]]\+@@' /etc/grub.conf 
# grub.conf generated by anaconda 
# 
# Note that you do not have to rerun grub after making changes to this file 
# NOTICE:  You have a /boot partition.  This means that 
#          all 
kernel and initrd paths are relative to /boot/, eg. 
#          root (hd0,0) 
#          kernel /vmlinuz-version ro root=/dev/mapper/vg_jimjimlv-lv_root 
#          initrd /initrd-[generic-]version.img 
#boot=/dev/sda 
default=0 
timeout=5 
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz 
hiddenmenu 
title CentOS 6 (2.6.32-573.el6.x86_64) 
root (hd0,0) 
kernel /vmlinuz-2.6.32-573.el6.x86_64 ro 
root=/dev/mapper/vg_jimjimlv-lv_root rd_NO_LUKS LANG=en_US.UTF-8 
rd_NO_MD 
SYSFONT=latarcyrheb-sun16 
rd_LVM_LV=vg_jimjimlv/lv_swap crashkernel=auto 
rd_LVM_LV=vg_jimjimlv/lv_root KEYBOARDTYPE=pc 
KEYTABLE=us rd_NO_DM rhgb quiet 
initrd 
/initramfs-2.6.32-573.el6.x86_64.img

Awk命令

awk是一個強大的文本分析工具,相對於grep的查找,sed的編輯,awk在其對數據分析並生成報告時,顯得尤其強大。簡單來講awk就是把文件逐行的讀入,以空格爲默認分隔符將每行切片,切開的部分再進行各類分析處理

awk有3個不一樣版本: awk、nawk和gawk,未做特別說明,通常指gawk,gawk 是 AWK 的 GNU 版本。

awk 實際上是指向gawk

[root@jimjimlv ~]# which awk 
/usr/bin/awk 
[root@jimjimlv ~]# ll /usr/bin/awk 
lrwxrwxrwx. 1 root root 4 Jan  2 03:15 /usr/bin/awk –> gawk
awk的語法格式 : 基本用法:gawk [option] ‘program’ FILE…..
Program: PATTERN{Action statements},語句之間用分號分隔

awk 的經常使用選項:

-F: 指明輸入時用到的字段分隔符
-v: var=value: 自定義變量

awk 支持兩種打印輸出的函數print 和printf

print 命令: 輸出格式爲" print item1, ,item2,…….」
printf命令: 輸出格式爲" printf FORMAT,item1,item2,…….」
FORMAT是必須給出
不會自動換行,須要顯示給出換行控制符,\n
FORMAT中須要分別爲後面的每一個item指定一個格式化符號

例子1: 以":" 爲分隔符顯示/etc/passwd打印輸出第1, 3個域

方法一:  利用自定義變量
[root@jimjimlv bin]# awk -v FS=":" '{print $1,$3}' /etc/passwd 
root 0 
bin 1 
daemon 2
這次後面省略……
方法二:  直接使用選項"-F"
[root@jimjimlv bin]# cat /etc/passwd | awk -F ':' '{print $1,$3}' 
root 0 
bin 1 
daemon 2
這次後面省略……

例2:顯示/etc/passwd的帳戶和帳戶對應的shell,帳戶與shell之間以tab鍵分割,而且在全部行添加列名name,shell

[root@jimjimlv ~]# awk -F ':' 'BEGIN {print "Name,    Shell"} {print $1,"\t"$7}' /etc/passwd 
Name, Shell 
root  /bin/bash 
bin   /sbin/nologin 
daemon  /sbin/nologin
這次後面省略……

 awk 的內置變量:

FS:input filed seperator, 默認爲空白字符
OFS: output field seperator, 默認爲空白字符:
RS:input record seperator, 輸入時的換行符
ORS:output record seperator, 輸出時的換行符 
NF: number of field, 字段數量
NR: number of record, 行數
FNR: 各文件分別計數:行數
FILENAME:當前文件名
ARGC:命令行參數的個數
ARGV:數組,保存的是命令行所給定的各參數

 支持的格式符:

  %c:顯示字符的ASCII碼
  %d, %i:顯示十進制整數
  %e,%E:科學計數法數值顯示
  %f:顯示爲浮點數
  %g,%G:以科學計數法或浮點形式顯示數值
  %s,顯示字符串
  %u:無符號整數
  %%:顯示%自身

例3:統計文件/etc/fstab裏面每一行的字段數量

[root@jimjimlv ~]# cat /etc/fstab
# # /etc/fstab 
# Created by anaconda on Sat Jan  2 11:12:56 2016 
# 
# Accessible filesystems, by reference, are maintained under 
'/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) 
for more info 
# UUID=e3539129-48be-4a5a-82bf-d14ee2cd28c6 /          xfs     defaults        0 0 
# UUID=cad6332b-bb5f-4db1-a75b-0b2d5bf0c26b /boot      xfs     defaults        0 0 
# UUID=06dea0f4-c235-4726-9de2-975ab4dd7bc2 /swap      swap    defaults        0 0 
[root@jimjimlv ~]# awk '{print NF}' /etc/fstab 
0 
1 
2 
10 
1 
9 
12 
1 
6 
6 
6

例4:以左對齊輸出/etc/passwd中的用戶名和UUID,並加備註,如「Username: root, UUID: 0」

[root@jimjimlv ~]# awk -F: '{printf "Username: %-15s,UID: %d\n",$1,$3}' /etc/passwd 
Username: root           ,UID: 0 
Username: bin            ,UID: 1 
Username: daemon         ,UID: 2 
Username: adm            ,UID: 3 
Username: lp             ,UID: 4
這次後面省略……
awk 操做符
支持算術操做符、賦值操做符、模式匹配符、函數調用、條件表達式等

例子5:顯示文件/etc/fstab當中ID號小於1000的用戶

[root@jimjimlv ~]# awk -F: '$3<1000{print $1,$3}' /etc/passwd 
root 0 
bin 1 
daemon 2
這次後面省略……

例子6:打印輸出/etc/fstab文件第一個域的字符號大於二、小於10的全部用戶

[root@jimjimlv ~]# awk -F: '(NR>=2&&NR<=10){print $1}' /etc/passwd 
bin 
daemon 
adm 
lp 
sync 
shutdown
這次後面省略……

四、寫一個腳本,生成10個隨機數,並按從小到大進行排序(要求至少使用2中方法)

方法一:使用for循環語句實現

[root@Jimjimlv tmp]# cat randsort.sh    
#!/bin/bash     
#     
#This script will created 10 digits, then display it by sort.     
#it created by Jim Lv on Saturday 26, 2016.     
for((i=1;i<=10;i++))     
    do     
        echo "$RANDOM"     
    done | sort -n     
[root@Jimjimlv tmp]# bash randsort.sh     
1365     
5047     
5288     
11037     
13373     
13463     
18773     
22710     
24842     
28670

方法二:利用FOR循環語句

[root@Jimjimlv tmp]# chmod +x sortname.sh    
[root@Jimjimlv tmp]# bash sortname.sh     
total=10    7259 7824 1899 574 19606 12398 7051 800 20237 569    
Begin to sort    
569 574 800 1899 7051 7259 7824 12398 19606 20237    
[root@Jimjimlv tmp]# cat sortname.sh     
#!/bin/bash    
#    
#this script created by Jim lv on Jan.27, 2016 10:35AM    
for((i=0;i<10;i++))      
   do       
     rand[$i]=$RANDOM      
   done
echo -e "total=${#rand[@]}\n${rand[@]}\nBegin to sort"
for((i=9;i>=1;i--))     
   do        
      for((j=0;j<i;j++))          
          do            
            if [ ${rand[$j]} -gt ${rand[$[$j+1]]} ] ;then            
               swapValue=${rand[$j]}            
               rand[$j]=${rand[$[$j+1]]}            
               rand[$[$j+1]]=$swapValue                
            fi          
          done     
    done    
 echo ${rand[@]}

五、在每週二的凌晨1點5分執行腳本/data/get_username.sh,並將腳本的輸出至/tmp/get_username.log日誌文件中;

[root@Jimjimlv tmp]# cat /data/get_username.sh    
#!/bin/bash     
#     
#print out the user root     
w
[root@Jimjimlv tmp]#
[root@Jimjimlv tmp]# cat /etc/crontab    
SHELL=/bin/bash     
PATH=/sbin:/bin:/usr/sbin:/usr/bin     
MAILTO=root     
HOME=/
# For details see man 4 crontabs
# Example of job definition:    
# .---------------- minute (0 - 59)     
# |  .------------- hour (0 - 23)     
# |  |  .---------- day of month (1 - 31)     
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...     
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR 
sun,mon,tue,wed,thu,fri,sat   
# |  |  |  |  |     
# *  *  *  *  * user-name command to be executed     
5  1  *  *  2 root  /data/get_username.sh >> /tmp/get_username.log
 [root@Jimjimlv tmp]# stat get_username.log     
  File: `get_username.log'     
  Size: 199           Blocks: 8          IO Block: 4096   regular file     
Device: fd00h/64768d    Inode: 914046      Links: 1     
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)     
Access: 2016-01-26 01:13:43.861999972 +0800     
Modify: 2016-01-26 01:05:01.516999989 +0800
Change: 2016-01-26 01:05:01.516999989 +0800     
[root@Jimjimlv tmp]# cat get_username.log     
01:05:01 up  7:05,  1 user,  load average: 0.00, 0.00, 0.00     
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT      
root     pts/0    10.100.100.165   17:57    8.00s  0.86s  0.79s bash
[root@Jimjimlv tmp]#

六、寫一個腳本:若是某路徑不存在,則將其建立爲目錄;不然顯示其存在,並顯示內容類型

程序運程結果:目錄不存在,建立目錄
[root@Jimjimlv tmp]# bash -n pathconf.sh    
[root@Jimjimlv tmp]# bash pathconf.sh     
Please enter a directory file name:    
/tmp/jimlv    
[root@Jimjimlv tmp]# ll /tmp/    
total 52    
-rw-r--r--. 1 root root  121 Jan 26 22:44 a    
-rw-r--r--. 1 root root  117 Jan 27 18:07 a.txt    
-rw-r--r--. 1 root root    6 Jan 26 22:44 b    
-rw-r--r--. 1 root root  199 Jan 26 01:05 get_username.log    
drwxr-xr-x. 2 root root 4096 Jan 27 18:08 jimlv    
drwxrwxr-t. 2 root root 4096 Jan 20 08:30 Jimlv    
-rw-r--r--. 1 root root  398 Jan 27 17:39 namepath.sh    
-rw-r--r--. 1 root root  735 Jan 27 18:07 pathconf.sh    
-rwxr-xr-x. 1 root root  392 Jan 27 17:27 path.sh    
-rw-r--r--. 1 root root    0 Jan 26 23:14 randsort2    
-rw-r--r--. 1 root root  222 Jan 26 23:19 randsort2.sh    
-rw-r--r--. 1 root root  192 Jan 26 22:59 randsort.sh    
drwxr-xr-x. 2 root root 4096 Jan 23 00:43 sed    
drwxr-xr-x. 2 root root 4096 Jan 22 00:30 test    
-rw-------. 1 root root    0 Jan 20 00:57 yum.log
程序運行結果:目錄存在,顯示目錄下全部文件的類型
[root@Jimjimlv tmp]# bash pathconf.sh     
Please enter a directory file name:    
/var    
/var is existed.     
cache  is an Other type file.    
db  is an Other type file.    
empty  is an Other type file.    
games  is an Other type file.    
lib  is an Other type file.    
local  is an Other type file.    
lock  is an Other type file.    
log  is an Other type file.    
mail  is an Other type file.    
nis  is an Other type file.    
opt  is an Other type file.    
preserve  is an Other type file.    
run  is an Other type file.    
spool  is an Other type file.    
tmp  is an Other type file.    
yp  is an Other type file.
[root@Jimjimlv tmp]# ll /var/    
total 60    
drwxr-xr-x.  4 root root 4096 Jan 20 00:58 cache    
drwxr-xr-x.  3 root root 4096 Jan 20 01:01 db    
drwxr-xr-x.  3 root root 4096 Jan 20 01:01 empty    
drwxr-xr-x.  2 root root 4096 Sep 23  2011 games    
drwxr-xr-x. 15 root root 4096 Jan 27 17:10 lib    
drwxr-xr-x.  2 root root 4096 Sep 23  2011 local    
drwxrwxr-x.  5 root lock 4096 Jan 20 01:01 lock    
drwxr-xr-x.  3 root root 4096 Jan 27 16:38 log    
lrwxrwxrwx.  1 root root   10 Jan 20 00:58 mail -> spool/mail    
drwxr-xr-x.  2 root root 4096 Sep 23  2011 nis    
drwxr-xr-x.  2 root root 4096 Sep 23  2011 opt    
drwxr-xr-x.  2 root root 4096 Sep 23  2011 preserve    
drwxr-xr-x. 11 root root 4096 Jan 27 16:38 run    
drwxr-xr-x.  8 root root 4096 Jan 20 00:59 spool    
drwxrwxrwt.  2 root root 4096 Jan 20 01:02 tmp    
drwxr-xr-x.  2 root root 4096 Sep 23  2011 yp
程序運行:輸入對象爲/etc
[root@Jimjimlv tmp]# bash pathconf.sh    
Please enter a directory file name:    
/etc/    
/etc/ is existed.     
adjtime  is an Other type file.    
aliases  is an Other type file.    
aliases.db  is an Other type file.    
alternatives  is an Other type file.    
anacrontab  is an Other type file.    
audisp  is an Other type file.    
audit  is an Other type file.    
bash_completion.d  is an Other type file.    
bashrc  is an Other type file.    
blkid  is an Other type file.    
centos-release  is an Other type file.    
chkconfig.d  is an Other type file.    
cron.d  is an Other type file.    
cron.daily  is an Other type file.    
cron.deny  is an Other type file.    
cron.hourly  is an Other type file.
後面省略…
代碼:
[root@Jimjimlv tmp]# cat pathconf.sh     
#!/bin/bash    
#    
#confirm the input path true or not, if ture, display the file type under path, or create the path.    
#created by Jim lv on 27 Jan, 2016 10:02AM    
function one {    
_pathname={1}    
`ls ${pathname} > a.txt`    
}

read -p "Please enter a directory file name:   
"  pathname    
if [ ! -d "$pathname" ]; then    
mkdir "$pathname"    
exit 2   
fi

if [ -d "$pathname" ]; then   
echo "$pathname is existed. "    
one ${pathname}    cat a.txt | while read line     
    do          
        if [ -f ${line} ];  then            
          echo "${line} is an Common file."          
        elif [ -d ${line} ]; then            
          echo "${line} is an Directory."          
        elif [ -L ${line} ];  then            
          echo "${line} is an Symbolic file."          
        else            
          echo "${line}  is an Other type file."          
        fi     
     done    
fi    
[root@Jimjimlv tmp]#

七、寫一個腳本,打印9X9乘法表;

程序運行結果:
[root@Jimjimlv tmp]# bash mathexp.sh    
1*1=1    
2*1=2    2*2=4    
3*1=3    3*2=6    3*3=9    
4*1=4    4*2=8    4*3=12    4*4=16    
5*1=5    5*2=10    5*3=15    5*4=20    5*5=25    
6*1=6    6*2=12    6*3=18    6*4=24    6*5=30    6*6=36    
7*1=7    7*2=14    7*3=21    7*4=28    7*5=35    7*6=42    7*7=49    
8*1=8    8*2=16    8*3=24    8*4=32    8*5=40    8*6=48    8*7=56    8*8=64    
9*1=9    9*2=18    9*3=27    9*4=36    9*5=45    9*6=54    9*7=63    9*8=72    9*9=81
代碼:
[root@Jimjimlv tmp]# cat mathexp.sh     
#!/bin/bash    
#    
#    
for((i=1;i<=9;i++))    
 do    
  Line=""    
  for((j=1;i<=9;j++))    
   do    
     Line=$Line"$i*$j="$[$i*$j]"\t"    
     [ $i -eq $j ] && echo -e $Line && break    
   done     
 done    
[root@Jimjimlv tmp]#

八、描述dns的工做流程,以及完整搭建一套dns主從服務器,相關域名和ip可自行設定

DNS,Domain Name Service,域名解析服務,平時咱們直接在瀏覽器上輸入某個網址就能直接訪問該網站的內容,而實現這一過程就是使用DNS服務器將該網址(也稱爲域名)解析爲IP地址,從而實現數據通訊。

DNS使用的監聽端口爲:upd/53, tcp/53,在Linux系統當中,它主要是應用程序(軟件)bind(Berkeley Internet Name Domain)所提供的服務。

DNS的樹狀結構

p_w_picpath

域名所表明的意義

名稱 表明意義
com 公司、企業
org 組織、機構
edu 教育單位
gov 政府
net 網絡
mil 軍事

DNS的解析方式

正向解析:FQDN –>>IP

反射解析:IP –>>FQDN

FQDN、Fully Qualified DOmain Name)全稱域名

DNS的查詢方式(工做流程)

它主要分爲兩種查詢,一種爲遞歸查詢、它一種爲迭代查詢,它們各自的查詢流程以下:

遞歸查詢

p_w_picpath

迭代查詢

p_w_picpath


實例演示:主DNS服務器IP:192.168.10.5 ;域名:soeasy.com

安裝Bind:
[root@localhost named]# yum -y install bind bind-chroot
[root@localhost named]# rpm -q bind
bind-9.9.4-29.el7_2.2.x86_64

BIND配置文件保存在兩個主要位置:

/etc/named.conf 這個是BIND服務主配置文件

/var/named/ 這個是用來存放DNS的zone文件

配置DNS服務配置「named.conf」文件

修改監聽的端口和IP地址、以及確認bind的工做目錄爲「/var/named」

[root@localhost ~]# cd /etc/
[root@localhost etc]# pwd
/etc
[root@localhost etc]# vim named.conf
/
// named.conf
//
// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
// server as a caching only nameserver (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.

//
options {        
            listen-on port 53 { 192.168.10.5; };        
            listen-on-v6 port 53 { ::1; };        
            directory       "/var/named";        
            dump-file       "/var/named/data/cache_dump.db";        
            statistics-file "/var/named/data/named_stats.txt";        
            memstatistics-file "/var/named/data/named_mem_stats.txt";        
            allow-query     { any; };

建立主域服務器

[root@localhost etc]# vim named.conf
// named.conf
//
// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
// server as a caching only nameserver (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//

options {        
              listen-on port 53 { 192.168.10.5; }; 
              listen-on-v6 port 53 { ::1; };        
              directory       "/var/named";        
              dump-file     "/var/named/data/cache_dump.db";        
              statistics-file "/var/named/data/named_stats.txt";        
              memstatistics-file "/var/named/data/named_mem_stats.txt";        
              allow-query     { any; };
zone "soeasy.com" {     #定義soeasy.com域      
      type master;      #定義主域服務器      
      file "soeasy.com.zone";  #定義zone文件,目錄爲/var/named};

手動建立「soeasy.com」域zone文件,咱們能夠利用bind的模板文件 named.localhost

[root@localhost etc]# cd /var/named
[root@localhost named]# ll
total 20
drwxrwxr--. 2 named named   22 Jan 30 04:42 data
drwxrwxr--. 2 named named   30 Jan 30 04:47 dynamic
-rw-r--r--. 1 root  named 2076 Jan 28  2013 named.ca
-rw-r--r--. 1 root  named  152 Dec 15  2009 named.empty
-rw-r--r--. 1 root  named  152 Jun 21  2007 named.localhost
-rw-r--r--. 1 root  named  168 Dec 15  2009 named.loopback
drwxrwxr--. 2 named named    6 Jan 27 04:55 slaves
[root@localhost named]# cat named.localhost 
$TTL 1D
@    IN SOA    @ rname.invalid. 
(
                    0     ; serial
                    1D    ; refresh
                    1H    ; retry
                    1W    ; expire
                    3H )    ; minimum
    NS    @
    A    
   127.0.0.1
    AAAA    ::1

複製模板文件爲「soeasy.com.zone」,選項「-p」能夠把屬性也一塊兒拷貝過去

[root@localhost named]# cp -p named.localhost soeasy.com.zone
編輯「soeasy.com.zone」文件,加入相關的地址解析
[root@localhost named]# vim soeasy.com.zone
$TTL 1D@     
IN SOA  @ rname.invalid. (
                    0     ; serial
                    1D    ; refresh
                    1H    ; retry
                    1W    ; expire
                    3H )    ; minimum                                               
 NS      @                
 A      192.168.10.5                
 AAAA    ::1                
 IN MX 10 mail.soeasy.com.www    
 IN A    192.168.10.6
 ~

爲」/var/named/*「下全部文件給予讀權限:

[root@localhost named]# chmod +r /var/named/*
[root@localhost named]# ll
total 20
drwxrwxr--. 2 named named   22 Jan 30 04:42 data
drwxrwxr--. 2 named named   30 Jan 30 04:47 dynamic
-rw-r--r--. 1 root  named 2076 Jan 28  2013 named.ca
-rw-r--r--. 1 root  named  152 Dec 15  2009 named.empty
-rw-r--r--. 1 root  named  152 Jun 21  2007 named.localhost
-rw-r--r--. 1 root  named  168 Dec 15  2009 named.loopback
drwxrwxr--. 2 named named    6 Jan 27 04:55 slaves
-rw-r--r--. 1 root  named  258 Jan 30 05:32 soeasy.com.zone

重啓named服務

[root@localhost named]# service named restart
Redirecting to /bin/systemctl restart  named.service
利用nslookup進行測試
[root@localhost named]# nslookup 
 Server:   ::1
 Address:  ::1#53

 Name:    
 Address: 192.168.10.6

利用host命令進行測試:

[root@localhost named]# host 
 www.soeasy.com has address 10.5.5.3
 [root@localhost named]# dig -t MX soeasy.com
; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.2 <<>> -t MX soeasy.com;; 
global options: +cmd;; 
Got answer:;; 
->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64374;; 
flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096;
; QUESTION SECTION:
;soeasy.com.            IN    MX
;; ANSWER SECTION:
soeasy.com.        86400    IN    MX    10 mail.soeasy.com.
;; AUTHORITY SECTION:
soeasy.com.        86400    IN    NS   soeasy.com.
;; ADDITIONAL SECTION:
soeasy.com.        86400    IN    A   192.168.10.5
soeasy.com.        86400    IN    AAAA    ::1

至此,DNS服務器配置完成

相關文章
相關標籤/搜索