N22-妙手-第七周课程练习

1、创建一个10G分区,并格式为ext4文件系统;

   (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;

[root@localhost ~]# mke2fs -t ext4 -m 2 -L MYDATA -b 2048 /dev/sda8
  
[root@localhost ~]# tune2fs -o acl /dev/sda8
tune2fs 1.42.9 (28-Dec-2013)
[root@localhost ~]# dumpe2fs /dev/sda8 | grep acl
dumpe2fs 1.42.9 (28-Dec-2013)
Default mount options:    user_xattr acl

   (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;

[root@localhost ~]# mount -o noexec,noatime /dev/sda8 /data/mydata
[root@localhost ~]# cat /etc/mtab | grep /dev/sda8
/dev/sda8 /data/mydata ext4 rw,seclabel,noexec,noatime,data=ordered 0 0

2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;

[root@localhost ~]# mkswap /dev/sda9
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=8d9e49df-5578-489d-8337-aa7d6faeecb5
[root@localhost ~]# swapon /dev/sda9
 
[root@localhost ~]# swapon -s | grep /dev/sda9
/dev/sda9                                 partition   1048572     0     -2

3、写一个脚本

   (1)、获取并列出当前系统上的所有磁盘设备;

   (2)、显示每个磁盘设备上每个分区相关的空间使用信息;

        脚本内容

#!/bin/bash
#
fdisk -l | grep -o "/dev/[[:alnum:]]\{3,\}"
df -lh

4、总结RAID的各个级别及其组合方式和性能的不同;

    见“N22-妙手-第七周博客作业第四题:RAID各级别的特性”

5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;

[root@localhost ~]# mdadm -C /dev/md3 --force -a yes -l 1 -n 1 -x 1 -c 128K  /dev/sde{2,3}

6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;

    (1) 创建RAID5设备

[root@localhost ~]# mdadm -C /dev/md1 -a yes -n 2 -x 1 -l 5 -c 256K /dev/sdc{2,3,5}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.

    

    (2) 格式化RAID5设备文件系统为ext4

[root@localhost ~]# mke2fs -t ext4 /dev/md1


    (3)设置自动挂载属性

[root@localhost ~]# cat /etc/fstab | grep md1
/dev/md1 /backup                                ext4    acl,noatime     0 0

7、写一个脚本

   (1) 接受一个以上文件路径作为参数;

   (2) 显示每个文件拥有的行数;

   (3) 总结说明本次共为几个文件统计了其行数;

    脚本内容

#!/bin/bash
#

declare -i existFileNumber=0
declare -i nonexistFileNumber=0

if [ $# -lt 1 ]; then
    echo "At lease 1 argument"
    exit 2
fi

for file in $@; do
    if [ -a $file ]; then
        fileLines=$(cat $file | wc -l)
        echo "The $file owns $fileLines lines"
        let existFileNumber+=1
    else
        echo "This $file does not exists"
        let nonexistFileNumber+=1
    fi
done

echo
echo "*******************************************"
echo "Summary information"
echo "How many lines of each file calculated: $existFileNumber"
echo "Wrong or nonexist file: $nonexistFileNumber"

    执行结果

[root@localhost week07]# bash sh7.sh /etc/fstab /etc/mtab 
The /etc/fstab owns 14 lines
The /etc/mtab owns 37 lines

*******************************************
Summary information
How many lines of each file calculated: 2
Wrong or nonexist file: 0

8、写一个脚本

   (1) 传递两个以上字符串当作用户名;

   (2) 创建这些用户;且密码同用户名;

   (3) 总结说明共创建了几个用户;

    

    脚本内容

if [ $# -lt 2 ]; then
    echo "At lease 2 usernames"    
    exit 2
fi

for username in $@; do
    if cat /etc/passwd | grep $username &> /dev/null; then
        echo "user $username already exits"
    else
        useradd $username
        echo $username | passwd --stdin $username &> /dev/null
        echo "$username added to current system"
        let createdUser+=1
        
    fi
done

echo "number of users created: $createdUser"


    执行结果

[root@localhost week07]# bash sh8.sh testuser5 testuser55 testuser555 testuser6
testuser5 added to current system
testuser55 added to current system
testuser555 added to current system
testuser6 added to current system
number of users created: 4


9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;

    脚本内容

#!/bin/bash
#

declare -i idsum=0

for i in {1..20}; do
        vistorname=visitor$i
        if id $vistorname >& /dev/null; then
                echo "user $vistorname is already exists"
        else
                useradd $vistorname
        fi
        userid=$(cat /etc/passwd | grep $vistorname | cut -d: -f3)
        idsum=$[$idsum+$userid]
done

echo "the sum of id for users vistor1 to vistor10: $idsum"

    执行结果

[root@localhost week07]# bash sh9.sh 
the sum of id for users vistor1 to vistor20: 22596

10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;

    脚本内容

#!/bin/bash 
# 
filepath_init=/etc/rc.d/rc.sysinit 
filepath_func=/etc/rc.d/init.d/functions 
filepath_fstab=/etc/fstab 
 
linesum_init=$(grep "^#" $filepath_init | wc -l) 
linesum_func=$(grep "^#" $filepath_func | wc -l) 
linesum_fstab=$(grep "^#" $filepath_fstab | wc -l) 
 
echo "# line number of file /etc/rc.d/rc.sysinit is: $linesum_init" 
echo "# line number of file /etc/rc.d/init.d/functions $linesum_func" 
echo "# line number of file /etc/fstab: $linesum_fstab" 
 
linespace_init=$(grep "^$" $filepath_init | wc -l)  
linespace_func=$(grep "^$" $filepath_func | wc -l)  
linespace_fstab=$(grep "^$" $filepath_fstab | wc -l) 
 
totalspace=$[$linespace_init+$linespace_func+$linespace_fstab] 
 
echo "total blink line sum is: $totalspace"

    执行结果

[root@localhost week07]# bash sh10.sh 
# line number of file /etc/rc.d/rc.sysinit is: 44
# line number of file /etc/rc.d/init.d/functions 43
# line number of file /etc/fstab: 7
total blink line sum is: 206

11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;

    脚本内容

#!/bin/bash
#
declare -i sumid=0
while read user_line; do    
    userbash=$(echo $user_line | cut -f7 -d:)
    userid=$(echo $user_line | cut -f3 -d:)
    username=$(echo $user_line | cut -f1 -d:)
    
    sumid=$[$sumid+$userid]
    if [ "$userbash" == "/bin/bash" ]; then
        echo "$username, $userid"
    fi
 
done < /etc/passwd

echo "userid sum is: $sumid"

    执行结果

abc10, 1064
userid sum is: 140656


12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;

    脚本内容

#!/bin/bash
#

declare -i sumOfGroupUsers=0
while read userline; do
    username=$(echo $userline | cut -f1 -d:)
    usergroup=$(echo $userline | cut -f4 -d:)
    if [ "$(id -G $username)" == "$usergroup" ]; then
        continue
    else
        echo "Owned addintional groups' user: $username"
        sumOfGroupUsers=$[$sumOfGroupUsers+1]

    fi
done < /etc/passwd

echo "Total number of owned additional groups' user from current system: $sumOfGroupUsers"

    执行结果

[root@localhost week07]# bash sh12.sh 
Owned addintional groups' user: postfix
Owned addintional groups' user: amandabackup
Owned addintional groups' user: qemu
Owned addintional groups' user: xiangbaomeng
Owned addintional groups' user: visitor1
Owned addintional groups' user: visitor2
Owned addintional groups' user: visitor3
Owned addintional groups' user: visitor15
Owned addintional groups' user: visitor16
Total number of owned additional groups' user from current system: 9

13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;

    (1) 以设备/dev/sdb 和 /dev/sdc创建分区

[root@localhost ~]# fdisk -l /dev/sdb
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    31459327    15728640   8e  Linux LVM
/dev/sdb2        31459328    39847935     4194304   8e  Linux LVM
 
[root@localhost ~]# fdisk -l /dev/sdc
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    10487807     5242880   8e  Linux LVM

 

   (2)创建PV

[root@localhost ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
[root@localhost ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created

   

    (3)创建卷组

[root@localhost ~]# vgcreate -s 8M vgtest /dev/sdb1 /dev/sdc1
  Volume group "vgtest" successfully created

 

    (4) 创建逻辑卷

[root@localhost ~]# lvcreate -L 5G -n mylv1 vgtest
  Logical volume "mylv1" created.

    (5)将创建的逻辑格式化为ext4

[root@localhost ~]# mke2fs -t ext4 /dev/vgtest/mylv1


    (6) 设置逻辑卷开机自动挂载

[root@localhost ~]# cat /etc/fstab | grep -E "/dev/vgtest/mylv1"
/dev/vgtest/mylv1 /users                        ext4    default,acl             0 0

(7)  挂载逻辑卷mylv1至挂载点/users

[root@localhost ~]# mount /dev/vgtest/mylv1 /users


14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;

    (1) 新建用户magedu

[root@localhost ~]# useradd -m -d /users/magedu magedu
 [root@localhost ~]# ls /users/magedu -al
total 20
drwx------. 3 magedu magedu 100 Sep 21 06:01 .
drwxr-xr-x. 4 root   root    30 Sep 21 06:01 ..
-rw-r--r--. 1 magedu magedu  18 Nov 20  2015 .bash_logout

   

    (2) 切换至magedu并复制多个文件至用户家目录

[root@localhost ~]# su - magedu
Attempting to create directory /users/magedu/perl5
[magedu@localhost ~]$ pwd
/users/magedu
[magedu@localhost ~]$ cp -r /etc/httpd /users/magedu

15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;

    (1) 将逻辑卷扩展至9G

[root@localhost ~]# lvextend -L 9G /dev/vgtest/mylv1
  Size of logical volume vgtest/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents).
  Logical volume mylv1 successfully resized.

  

    (2) 将逻辑卷文件系统扩展至9G

[root@localhost ~]# resize2fs /dev/vgtest/mylv1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vgtest/mylv1 is mounted on /users/test; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/vgtest/mylv1 is now 2359296 blocks long.

        (3) 查看扩展逻辑卷之后文件数据是否正常

[root@localhost ~]# df -lh | grep "users"
/dev/mapper/vgtest-mylv1  8.8G   23M  8.3G   1% /users/test
[root@localhost ~]# cat /users/magedu/httpd/conf/httpd.conf
#
# This is the main Apache HTTP server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.

16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;

    (1) 卸载逻辑卷

[root@localhost ~]# umount /dev/vgtest/mylv1

        (2) 检查文件系统

[root@localhost ~]# e2fsck -f /dev/vgtest/mylv1

/dev/vgtest/mylv1: 12/589824 files (0.0% non-contiguous), 75552/2359296 blocks


    (3) 缩减文件系统容量

[root@localhost ~]# resize2fs /dev/vgtest/mylv1 7G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vgtest/mylv1 to 1835008 (4k) blocks.
The filesystem on /dev/vgtest/mylv1 is now 1835008 blocks long.

        (4) 缩减逻辑卷容量

[root@localhost ~]# lvreduce -L 7G /dev/vgtest/mylv1
  Logical volume mylv1 successfully resized.

  (5) 重新挂载逻辑卷至挂载点/users

[root@localhost ~]# mount /dev/vgtest/mylv1 /users

17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;

[root@localhost ~]# lvcreate -L 7G -p r -s -n snap_mylv1 /dev/vgtest/mylv1
  Logical volume "snap_mylv1" created.

原创文章,作者:mxb93,如若转载,请注明出处:http://www.178linux.com/48647

(0)
上一篇 2016-10-09 09:06
下一篇 2016-10-09 09:06

相关推荐

  • Linux文件查找及压缩常用知识总结

    一、文件查找 1.locate命令: locate KEYWORD 常用选项:     -i 执行区分大小写的搜索     -n  N只列举前N个匹配项目 查询系统上预建的文件索引数据库在:/var/lib/mlocate/mlocate….

    Linux干货 2016-08-18
  • 马哥网络教育班第21期+第六周课程练习

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; [root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp/ [root@localho…

    Linux干货 2016-08-02
  • 一起学DHCP系列(二)三种途径

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jeffyyko.blog.51cto.com/28563/162176 《一起学DHCP》系列第二节。      和WINS服务类似,DHCP大体上也由两部分组成,DHC…

    Linux干货 2015-03-25
  • 马哥教育网络19期+第十二周课程练习

    1、请描述一次完整的http请求处理过程; a.向根域名服务器请求解析域名,然后根服务器返回相应的IP信息; b.用户的Web浏览器向服务器端的80端口通过三次握手建立TCP连接; c.建立完TCP连接后发送HTTP请求,请求的格式包括请求方法、URL和协议版本号,方法有GET、HEAD、POST、PUT、DELETE、OPTIONS、TRACE,如: &n…

    Linux干货 2016-08-22
  • 硬链接和软链接的区别

    硬链接和软链接在文件系统实现上的区别决定了两者之间功能上的不同,也就是说硬链接和软链在文件系统上两者实现的方式不同,让其各自具有不同的功能和特色,也决定了各自的优点和缺点。接下同文件系统上创建硬链接和软链接看各自的特点。 一文件系统上创建硬链接 下面命令是在文件系统上创建一个硬链接: ~]# ln test /testdir/t…

    Linux干货 2016-10-29
  • raid5创建

    创建分区并改类型 创建raid5   创建把文件系统 生成配置文件    

    2017-12-17

评论列表(2条)

  • luoweiro
    luoweiro 2016-10-12 00:01

    第八题和第九题都有提前判读用户是否存在,而且用两种方式很赞,学习的过程有的时候就是不断举一反三,打破常规,拓展知识体系去尝试各种方式,加油。

    • mxb93
      mxb93 2016-10-15 11:43

      @luoweiro多谢老师的鼓励!我之前有点陷入了低潮,在lamp哪里,东西一下来的太多,吸收不过来。。看到老师的话,我会继续努力的