N26 第七周作业

1、创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;

[root@localhost ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# mke2fs -b 2048 -m 2 -t ext4 -L MYDATA /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=MYDATA
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 5242880 blocks
104857 blocks (2.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=273678336
320 block groups
16384 blocks per group, 16384 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
   16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104,
   2048000, 3981312

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   

[root@localhost ~]# tune2fs -o acl /dev/sdb1
tune2fs 1.42.9 (28-Dec-2013)
[root@localhost ~]# tune2fs -l /dev/sdb1
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name:   MYDATA
Last mounted on:          <not available>
Filesystem UUID:          ddd0ab73-0f58-414c-ae46-d2b354d2bdde
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              655360
Block count:              5242880
Reserved block count:     104857
Free blocks:              5121266
Free inodes:              655349
First block:              0
Block size:               2048
Fragment size:            2048
Group descriptor size:    64
Reserved GDT blocks:      512
Blocks per group:         16384
Fragments per group:      16384
Inodes per group:         2048
Inode blocks per group:   256
Flex block group size:    16
Filesystem created:       Sat Feb  4 11:31:58 2017
Last mount time:          n/a
Last write time:          Sat Feb  4 11:32:29 2017
Mount count:              0
Maximum mount count:      -1
Last checked:             Sat Feb  4 11:31:58 2017
Check interval:           0 (<none>)
Lifetime writes:          65 MB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:           256
Required extra isize:     28
Desired extra isize:      28
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      a05dbfa7-5483-4885-9295-2418b7bd76ea
Journal backup:           inode blocks

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

[root@localhost ~]# mount -o noexec,noatime /dev/sdb1 /data/mydata
[root@localhost ~]# stat /data/mydata/aaa.txt
  File: ‘/data/mydata/aaa.txt’
  Size: 15           Blocks: 4          IO Block: 2048   regular file
Device: 811h/2065d   Inode: 16          Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:unlabeled_t:s0
Access: 2017-02-04 10:45:12.167018328 +0600
Modify: 2017-02-04 10:45:12.167018328 +0600
Change: 2017-02-04 10:45:12.197018328 +0600
 Birth: –
# 读取文件后Access时间戳不会改变
[root@localhost ~]# cat /data/mydata/aaa.txt
I study Linux.
[root@localhost ~]# stat /data/mydata/aaa.txt
  File: ‘/data/mydata/aaa.txt’
  Size: 15           Blocks: 4          IO Block: 2048   regular file
Device: 811h/2065d   Inode: 16          Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:unlabeled_t:s0
Access: 2017-02-04 10:45:12.167018328 +0600
Modify: 2017-02-04 10:45:12.167018328 +0600
Change: 2017-02-04 10:45:12.197018328 +0600
 Birth: –
# 即使设置挂载选项noatime,修改文件或touch文件仍然可以改变时间戳
[root@localhost ~]# touch /data/mydata/aaa.txt
[root@localhost ~]# stat /data/mydata/aaa.txt
  File: ‘/data/mydata/aaa.txt’
  Size: 15           Blocks: 4          IO Block: 2048   regular file
Device: 811h/2065d   Inode: 16          Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:unlabeled_t:s0
Access: 2017-02-04 10:48:16.080017861 +0600
Modify: 2017-02-04 10:48:16.080017861 +0600
Change: 2017-02-04 10:48:16.080017861 +0600
 Birth: –

# 运行二进制程序会提示“权限拒绝”
[root@localhost ~]# /data/mydata/bash
-bash: /mnt/bash: Permission denied

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

[root@localhost ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 1
Invalid partition type `1'

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (20973568-41943039, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +1G     
Partition 2 of type Linux and of size 1 GiB is set

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=00550b28-79e0-4a67-9a8f-020adf3bd811
[root@localhost ~]# swapon /dev/sdb2
[root@localhost ~]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   120G  0 disk
├─sda1            8:1    0   500M  0 part /boot
└─sda2            8:2    0 119.5G  0 part
  ├─centos-root 253:0    0    50G  0 lvm  /
  ├─centos-swap 253:1    0     2G  0 lvm  [SWAP]
  └─centos-home 253:2    0  67.5G  0 lvm  /home
sdb               8:16   0    20G  0 disk
├─sdb1            8:17   0    10G  0 part
└─sdb2            8:18   0     1G  0 part [SWAP]
sr0              11:0    1   7.2G  0 rom 

3、写一个脚本
   (1)、获取并列出当前系统上的所有磁盘设备;
   (2)、显示每个磁盘设备上每个分区相关的空间使用信息;

#!/bin/bash

fdisk -l | grep "Disk /dev/sd." | cut -d: -f1
echo
df

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

RAID分为以下几个级别
RAID0 条带卷
   最少磁盘数量:2
   读写性能:读写性能均有提升
   容错能力:没有容错能力,一旦有一块磁盘损坏,将丢失全部数据
   磁盘空间:最小磁盘容量 * 磁盘数量
RAID1
   最少磁盘数量:2 一般不会多于2块
   读写性能:读数据性能提升,写数据性能略有下降
   容错能力:有容错能力,允许一块磁盘损坏
   磁盘空间:最小磁盘的磁盘容量
RAID4
   最少磁盘数量:3
   读写性能:读写性能均有提升
   容错能力:有容错能力,允许一块磁盘损坏,但因为使用单独磁盘做校验盘,所以校验盘读写压力大,也增加了校验盘损坏的风险
   磁盘空间:最小磁盘容量 * ( 磁盘数量-1 )
RAID5
   最少磁盘数量:3
   读写性能:正常工作时读写性能均有提升,如果有一块磁盘损坏,则读写性能均会下降
   容错能力:有容错能力,允许一块磁盘损坏
   磁盘空间:最小磁盘容量 * ( 磁盘数量-1 )
RAID6
   最少磁盘数量:4 两块磁盘做校验盘
   读写性能:读写性能均有提升
   容错能力:最多允许坏两块盘,但不允许两块数据盘同时损坏
   磁盘空间:最小磁盘容量 * ( 磁盘数量-2 )
以下为组合类型
RAID10
   实质是先把磁盘两两组成RAID1,然后再把RAID1组成RAID0,进一步提升了容错能力,最多允许每一组RAID1中有一块磁盘损坏
RAID01
   实质是先把一批磁盘分为两组,然后每组磁盘组成RAID0,然后再把两组RAID0组成RAID1,也能进一步提升容错能力,但这种组合写数据能力较RAID10偏低,因为这种组合需要数据两级切片
RAID50
   先组RAID5,再组RAID0

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

[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 0x2b8c50c6.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1305, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305):
Using default value 1305

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x9aef7b46.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

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):
Using default value 2610

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# fdisk /dev/sdd
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x5ca49bc1.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1958, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1958, default 1958):
Using default value 1958

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# lsblk
NAME                        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0                          11:0    1   4.2G  0 rom  
sda                           8:0    0   120G  0 disk
├─sda1                        8:1    0   500M  0 part /boot
└─sda2                        8:2    0 119.5G  0 part
  ├─VolGroup-lv_root (dm-0) 253:0    0    50G  0 lvm  /
  ├─VolGroup-lv_swap (dm-1) 253:1    0     2G  0 lvm  [SWAP]
  └─VolGroup-lv_home (dm-2) 253:2    0  67.6G  0 lvm  /home
sdb                           8:16   0    10G  0 disk
└─sdb1                        8:17   0    10G  0 part
sdc                           8:32   0    20G  0 disk
└─sdc1                        8:33   0    20G  0 part
sdd                           8:48   0    15G  0 disk
└─sdd1                        8:49   0    15G  0 part
# 三块硬盘大小不同
[root@localhost ~]# mdadm -C /dev/md0 -l 1 -c 128 –raid-devices=2 /dev/sd[bc]1 –spare-devices=1 /dev/sdd1
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    –metadata=0.90
mdadm: largest drive (/dev/sdc1) exceeds size (10474176K) by more than 1%
# 因为用做raid的磁盘大小差异太大,上面一行给出提示,下面一行询问是否继续创建
Continue creating array? (y/n) y  
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
# 刚刚创建完成,执行挂载命令,提示必须先指定“文件系统类型”
# 看来没有指定文件系统的设备是无法挂载的
[root@localhost ~]# mount /dev/md0 /mnt
mount: you must specify the filesystem type
# -D 选项查看刚刚创建在raid在详细信息
[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Sat Feb  4 11:29:15 2017
     Raid Level : raid1
     Array Size : 10474176 (9.99 GiB 10.73 GB)
  Used Dev Size : 10474176 (9.99 GiB 10.73 GB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Sat Feb  4 11:30:08 2017
          State : clean
 Active Devices : 2
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 1

           Name : localhost.localdomain:0  (local to host localhost.localdomain)
           UUID : a944c4e6:68ee9c12:baf9ff54:cec61ecf
         Events : 17

    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       1       8       33        1      active sync   /dev/sdc1

       2       8       49        –      spare   /dev/sdd1

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

[root@localhost ~]# mdadm -C /dev/md1 -l5 -c256 –raid-devices=3 /dev/sd[efg]

mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
[root@localhost ~]# mdadm –detail /dev/md1
/dev/md1:
        Version : 1.2
  Creation Time : Mon Feb  6 23:23:54 2017
     Raid Level : raid5
     Array Size : 4190208 (4.00 GiB 4.29 GB)
  Used Dev Size : 2095104 (2046.00 MiB 2145.39 MB)
   Raid Devices : 3
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Mon Feb  6 23:24:06 2017
          State : clean
 Active Devices : 3
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 0

         Layout : left-symmetric
     Chunk Size : 256K

           Name : localhost.localdomain:1  (local to host localhost.localdomain)
           UUID : 177e560f:12fbca97:110aa862:ae27995c
         Events : 18

    Number   Major   Minor   RaidDevice State
       0       8       64        0      active sync   /dev/sde
       1       8       80        1      active sync   /dev/sdf
       3       8       96        2      active sync   /dev/sdg

[root@localhost ~]# mkfs.ext4 /dev/md1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=64 blocks, Stripe width=128 blocks
262144 inodes, 1047552 blocks
52377 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1073741824
32 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
  32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# blkid /dev/md1
/dev/md1: UUID="4dce67bd-a912-458c-9ab3-125de499f60e" TYPE="ext4"

[root@localhost ~]# mkdir /backup
[root@localhost ~]# vim /etc/fstab
# 添加一行,创建raid时使用/dev/md1但自动挂载时不一定是这个设备名称,所以挂载时使用了UUID
UUID=4dce67bd-a912-458c-9ab3-125de499f60e   /backup     ext4  defaults,acl,noatime  0 0
# 重启系统使用mount命令检查是否自动挂载
[root@localhost ~]# mount
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/VolGroup-lv_home on /home type ext4 (rw)
/dev/md126 on /backup type ext4 (rw,noatime,acl)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

7、写一个脚本
   (1) 接受一个以上文件路径作为参数;
   (2) 显示每个文件拥有的行数;
   (3) 总结说明本次共为几个文件统计了其行数;

#!/bin/bash

# filename rptnof.sh
# report number of each ASCII text file, and give statistic info.

if [ $# -eq 0 ]; then
        echo "At least one path argument" && exit 1
fi

declare -i m=0 n=0;
for i in $@; do
        if [[  $(file -b $i) = "ASCII text" ]]; then
                echo $(wc -l $i)
                let m++;
        else
                let n++
        fi
done

echo -e "\nTotal $# files, $m text files and report number of lines, $n files not text file "

# 添加属主执行权限
[root@localhost scripts]# chmod u+x rptnof.sh

[root@localhost scripts]# ./rptnof.sh /etc/fstab /etc/passwd /bin/bash
17 /etc/fstab
29 /etc/passwd

Total 3 files, 2 text files and report number of lines, 1 files not text file

8、写一个脚本
   (1) 传递两个以上字符串当作用户名;
   (2) 创建这些用户;且密码同用户名;
   (3) 总结说明共创建了几个用户;

#!/bin/bash

# add a batch of users

if [ $# -eq 0 ]; then
        echo "Usage: $(basename $0) username [,username]…"
fi

#!/bin/bash
# add a batch of users

if [ $# -eq 0 ]; then
        echo "Usage: $(basename $0) username [,username]…"
        exit 1
fi

declare -i num=0;
declare -a u;
for i in $@; do
        id $i &> /dev/null
        if [ $? -eq 0 ]; then
                echo "user $i exists"
        else
                (useradd $i && echo $i | passwd –stdin $i) &> /dev/null
                let num++;
                u[${#u[@]}]=$i
        fi
done

echo "Total $num users added: ${u[@]}"

[root@localhost scripts]# bash add-users.sh zhsan lisi wangwu zhaoliu
user zhsan exists
user lisi exists
Total 2 users added: wangwu zhaoliu

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

#!/bin/bash

declare -i sum=0;
for i in {1..20};do
        username="visitor$i"
        useradd "$username" &> /dev/null && sum+=$(id -u $username )
done

echo The sum of userid is $sum.

[root@localhost scripts]# bash sumuid.sh
The sum of userid is 10490.

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

#!/bin/bash

echo "The number of lines of start with # is $(grep "^#" /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab | wc -l)"
echo "The number of lines of blank is $(grep "[[:space:]]*" /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab | wc -l) "

[root@localhost scripts]# bash speclinecount.sh
The number of lines of start with # is 91
The number of lines of blank is 1484

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

#!/bin/bash

declare -a auid;
grep "/bin/bash$" /etc/passwd | cut -d: -f1,3
auid=($(grep "/bin/bash$" /etc/passwd | cut -d: -f1,3 | cut -d: -f2))
declare -i sum;
for i in ${auid[@]}; do
        sum+=$i;
done
echo "The sum of uid of above users is $sum."

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

#!/bin/bash

aUser=($(cat /etc/passwd | cut -d: -f1))
declare -i num=0;
for i in ${aUser[@]};do
        id $i | grep -E "groups=[^,]+,.+"
        if [ $? -eq 0 ]; then
                let num++;
        fi
done

echo "Total $num user(s) have supplementary group "

[root@localhost scripts]# bash specuser.sh
uid=1(bin) gid=1(bin) groups=1(bin),2(daemon),3(sys)
uid=2(daemon) gid=2(daemon) groups=2(daemon),1(bin),4(adm),7(lp)
uid=3(adm) gid=4(adm) groups=4(adm),3(sys)
uid=89(postfix) gid=89(postfix) groups=89(postfix),12(mail)
Total 4 user(s) have supplementary group

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

# 修改分区类型为Linux LVM
[root@localhost scripts]# fdisk /dev/sdc

2017-02-07 21 34 11.gif

# 查看分区
[root@localhost scripts]# fdisk -l /dev/sdc

2017-02-07 21 37 06.gif

# 创建物理卷
[root@localhost scripts]# pvcreate /dev/sdc[1-3]
# 创建卷组
[root@localhost scripts]# vgcreate -s 8M MyVolGrp /dev/sdc[1-3]
# 查看卷组
[root@localhost scripts]# vgdisplay MyVolGrp

2017-02-07 21 38 59.gif

# 创建逻辑卷
[root@localhost scripts]# lvcreate -L 5G -n mylv1 MyVolGrp
[root@localhost scripts]# lvs

2017-02-07 21 42 00.gif

# 格式化
[root@localhost scripts]# mkfs.ext4 /dev/mapper/MyVolGrp-mylv1

2017-02-07 21 44 09.gif

# 创建目录用于挂载
[root@localhost scripts]# mkdir /users
# 编辑配置文件实现开机自动挂载
[root@localhost scripts]# vim + /etc/fstab
## /dev/mapper/MyVolGrp-mylv1      /users  ext4  acl       0 0
# 检查开机自动挂载
[root@localhost ~]# mount

2017-02-07 21 44 59.gif

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

[root@localhost ~]# useradd -d /users/magedu magedu
[root@localhost ~]# tail -1 /etc/passwd
[root@localhost ~]# useradd -d /users/magedu magedu
[root@localhost ~]# tail -1 /etc/passwd
[magedu@localhost ~]$ ll .

2017-02-07 21 27 06.gif

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

# 扩展逻辑卷
[root@localhost ~]# lvextend -L 9G /dev/MyVolGrp/mylv1 
[root@localhost ~]# lvs
# 检查磁盘空间发现并没有扩展,还是5G大小     
[root@localhost ~]# df
# 扩展文件系统
[root@localhost ~]# resize2fs /dev/mapper/MyVolGrp-mylv1 
# 现在磁盘空间已经扩展
[root@localhost ~]# df -h /dev/mapper/MyVolGrp-mylv1 
# 检查空间扩展后文件的可用性
[root@localhost ~]# ll /users/magedu
[root@localhost ~]# tail -1 /users/magedu/passwd

   2017-02-07 21 19 36.gif

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

[root@localhost ~]# umount /dev/mapper/MyVolGrp-mylv1 
[root@localhost ~]# e2fsck -f /dev/mapper/MyVolGrp-mylv1
[root@localhost ~]# resize2fs /dev/MyVolGrp/mylv1 7G
[root@localhost ~]# lvreduce -L 7G /dev/MyVolGrp/mylv1 
[root@localhost ~]# mount -a
[root@localhost ~]# tail -1 /users/magedu/passwd

2017-02-07 21 14 15.jpg

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

[root@localhost tmp]# lvcreate -L 10M -p r -s -n mylv1_snap /dev/MyVolGrp/mylv1 
[root@localhost tmp]# mount /dev/MyVolGrp/mylv1_snap /mnt
mount: block device /dev/mapper/MyVolGrp-mylv1_snap is write-protected, mounting read-only
[root@localhost tmp]# tar cJf /backup/test.tar.xz /mnt/magedu
tar: Removing leading `/' from member names
[root@localhost tmp]# ll /backup/test.tar.xz 
-rw-r--r--. 1 root root 1208 Feb  7 08:56 /backup/test.tar.xz
[root@localhost tmp]# tar tfv /backup/test.tar.xz

2017-02-07 21 06 52.jpg

原创文章,作者:和风细雨,如若转载,请注明出处:http://www.178linux.com/67391

(0)
上一篇 2017-02-07 20:48
下一篇 2017-02-07 23:38

相关推荐

  • N25第四周总结

    linux bassic Week 4 Blogging 1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 ~]# cp -r /etc/skel/   /home/tuser1/ ~]# chmod&n…

    Linux干货 2016-12-26
  • Ansible的常用模块

    command模块: 目的:在指定节点上运行hostname命令 命令:ansible 192.168.1.16 -m command  ‘hostname’copy模块:目的:把主控端/root目录下的a.sh文件拷贝到到指定节点上 命令:ansible 192.168.1.16 -m copy -a ‘src=/roo…

    Linux干货 2017-10-31
  • nginx初步

    一、知识整理 1、查看进程使用的cpu;ni查看nice值 [root@localhost html]# ps axo pid,comm,psr,ni  43769 nginx          &n…

    Linux干货 2016-10-31
  • 22期第十四周课堂练习

    系统的INPUT和OUTPUT默认策略为DROP; [root@localhost ~]# iptables -P INPUT DROP [root@localhost ~]# iptables -P OUTPUT DROP 1、限制本地主机的web服务器在周…

    Linux干货 2017-03-15
  • bash 的过程式和循环式的编写

           bash脚本的编写,对于Linux这个多用户,多任务的系统,有很大的帮助,脚本可以帮助我们定时执行任务,过滤文本,检查系统性能等等。下面我们来了解一下过程式和循环式脚本的编写。 使用read 命令来接受输入 使用read 来把输入值分配给一个或多个shell 变量:  &nb…

    Linux干货 2016-08-24
  • 自制linux系统

    本文主要通过裁剪现有Linux系统,打造一个属于自己的Linux小系统,让其能够装载网卡驱动,并配置IP地址,实现网络功能。 自制linux系统 步骤概述: 1、新建一个硬盘2、在该新硬盘上新建两个分区,一个当boot分区,一个当/分区3、格式化并且挂载两个分区4、安装grub至目标磁盘5、为grub提供配置文件6、复制内核文件和initrd文件7、创建目标…

    Linux干货 2016-09-13

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-02 20:06

    非常不建议用截图来显示命令。