1、描述centos6系统开机启动流程

CentOS6启动系统时大致经过以下4个大模块

2016年1月19日课程作业_display

BIOS加电自检

POST,PowerOnSelfTest,系统会对每一个硬件设备进行自检,待通过之后,会去检测有操作系统引导启动的设备,进而完成系统的顺利启动,这点跟我们的Winodws是一样的。

MBR引导

MBR(Master Boot Record)一般指的是磁盘0磁道1扇区,共512个字节,前446个字节是BootLoder,后 4*16 的 64 个字节是存放分区信息的,最后 2 个字节是校验信息,一般是 55AA。

GRUB(GRand Unified Bootloader)

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加载到内存里,让后将其中的内容释放到内容中,

加载内核

启动init进程:进程是系统启动的第一个进程。其他进程都是有此进程控制fork出来的,也就是说,init是其他进程的“祖先”,PID=1。

init启动后所做的事情:

1、读取/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

2、执行初始化系统脚本 /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)清理操作

3、然后执行 /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. # 默认级别 启动次序 不启动次序

4、执行/etc/rc.d/rc.local脚本,此脚本是启动过程中最后启动的一个脚本

5、系统登录界面

2、描述/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)加载额外设备的驱动程序

3、总结文本处理工具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文件第一个域的字符号大于2、小于10的所有用户

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

4、写一个脚本,生成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[@]}

5、在每周二的凌晨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]#

6、写一个脚本:如果某路径不存在,则将其创建为目录;否则显示其存在,并显示内容类型

程序运程结果:目录不存在,创建目录
[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]#

7、写一个脚本,打印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]#

8、描述dns的工作流程,以及完整搭建一套dns主从服务器,相关域名和ip可自行设定

DNS,Domain Name Service,域名解析服务,平时我们直接在浏览器上输入某个网址就能直接访问该网站的内容,而实现这一过程就是使用DNS服务器将该网址(也称为域名)解析为IP地址,从而实现数据通信。

DNS使用的监听端口为:upd/53, tcp/53,在Linux系统当中,它主要是应用程序(软件)bind(Berkeley Internet Name Domain)所提供的服务。

DNS的树状结构

2016年1月19日课程作业_created_02

域名所代表的意义

名称代表意义
com公司、企业
org组织、机构
edu教育单位
gov政府
net网络
mil军事

DNS的解析方式

正向解析:FQDN –>>IP

反射解析:IP –>>FQDN

FQDN、Fully Qualified DOmain Name)全称域名

DNS的查询方式(工作流程)

它主要分为两种查询,一种为递归查询、它一种为迭代查询,它们各自的查询流程如下:

递归查询

2016年1月19日课程作业_display_03

迭代查询

2016年1月19日课程作业_created_04


实例演示:主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服务器配置完成