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.www58058.com/48647
第八題和第九題都有提前判讀用戶是否存在,而且用兩種方式很贊,學習的過程有的時候就是不斷舉一反三,打破常規,拓展知識體系去嘗試各種方式,加油。
@luoweiro:多謝老師的鼓勵!我之前有點陷入了低潮,在lamp哪里,東西一下來的太多,吸收不過來。??吹嚼蠋煹脑挘視^續努力的