一、建立一個10G分區,並格式爲ext4文件系統;node
[root@localhost ~]# fdisk /dev/sdb //管理分區sdb vim
命令(輸入 m 獲取幫助):n //輸入命令n建立一個新分區centos
Select (default p): p //設置分區類型爲主分區緩存
分區號 (1-4,默認 1):1 //設置分區號安全
起始 扇區 (2048-41943039,默認爲 2048): //設置起始扇區,由於這裏是sdb上第一個分區因此不輸入任何信息,從默認起始扇區開始bash
Last 扇區, +扇區 or +size{K,M,G} (2048-41943039,默認爲 41943039):+10G //設置分區大小服務器
分區 1 已設置爲 Linux 類型,大小設爲 10GiB網絡
命令(輸入 m 獲取幫助):w //輸入w保存並退出app
(1) 要求其block大小爲2048, 預留空間百分比爲2, 卷標爲MYDATA, 默認掛載屬性包含acl;
dom
[root@localhost ~]# mke2fs -t ext4 -b 2048 -m2 -L MYDATA /dev/sdb1 //設置文件系統類型、塊大小及預留空間百分比
mke2fs 1.42.9 (28-Dec-2013)
文件系統標籤=MYDATA
OS type: Linux
塊大小=2048(log=1)
分塊大小=2048(log=1)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 5242880 blocks
104857 blocks (2.00%) reserved for the super user
第一個數據塊=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: 完成
正在寫入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
(2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;
[root@localhost ~]# mount -oacl,noatime,noexec -L MYDATA /data/mydata/
[root@localhost ~]# mount | grep sdb1 //查看sdb1分區是否掛載
/dev/sdb1 on /data/mydata type ext4(rw,noexec,noatime,seclabel,data=ordered)
二、建立一個大小爲1G的swap分區,並建立好文件系統,並啓用之;
[root@localhost ~]# fdisk /dev/sdb //管理分區sdb
命令(輸入 m 獲取幫助):n //輸入命令n建立一個新分區
Select (default p): p //設置分區類型爲主分區
分區號 (2-4,默認 2):2 //設置分區號
起始 扇區 (20973568-41943039,默認爲20973568): //直接按回車從默認起始扇區開始
將使用默認值20973568
Last 扇區, +扇區 or +size{K,M,G} (20973568-41943039,默認爲 41943039):+1G //設置分區大小
命令(輸入 m 獲取幫助):t //修改分區類型
分區號 (1,2,默認 2):2 //輸入要修改的分區號
Hex 代碼(輸入 L 列出全部代碼):82 //修改成swap類型,類型代碼爲82
已將分區「Linux」的類型更改成「Linuxswap / Solaris」
命令(輸入 m 獲取幫助):w //輸入w保存退出
[root@localhost ~]# partx -a /dev/sdb //強制重讀磁盤分區表
[root@localhost ~]# mkswap /dev/sdb2 //建立swap設備
正在設置交換空間版本 1,大小 = 1048572KiB
無標籤,UUID=06f2a16f-9705-4b94-bdb6-05e164ff31c1
[root@localhost ~]# free //掛載前swap分區大小
[root@localhost ~]# swapon /dev/sdb2 //將分區sdb2啓用爲交換分區
[root@localhost ~]# free //啓用以後swap分區大小
三、寫一個腳本
(1)、獲取並列出當前系統上的全部磁盤設備;
(2)、顯示每一個磁盤設備上每一個分區相關的空間使用信息;
#!/bin/bash # fdisk -l | grep "^/.*" echo " " df -lh
四、總結RAID的各個級別及其組合方式和性能的不一樣;
RAID:Redundant Arrays of Inexpensive Disks, 不貴的磁盤冗餘陣列
級別:RAID0,RAID1,RAID5,RAID10,RAID01,RAID6
RAID-0:同時對兩塊磁盤進行讀寫,性能上有較大提高
無容錯能力,
可用空間:所有磁盤
通常適用於有較高IO,但數據不是很重要的場景,相似緩存一類的場景;
RAID-1:又稱磁盤鏡像,兩塊磁盤進行全盤備份,一份數據會同時寫入兩塊磁盤中,因此在數據
讀取的時候性能會有提高,可是寫入的時候性能就會降低,
全盤備份,安全性高,可是磁盤利用率降低一半
適用於對數據安全性要求較高的場景
RAID-5:分佈式奇偶校驗的磁盤結構
讀寫性能有提高,
有冗餘能力
可用空間:N-1
這是目前用的比較多的陣列方式,3塊磁盤,損壞一塊仍能保證數據完整性,同時又有
較高的磁盤利用率。屬於物美價廉型。
RAID-10:高可靠性與高效磁盤結構
先作RAID-1,再作RAID-0
讀性能有提高,
可用空間降低一半
冗餘能力:每組最多隻能壞一塊,
RAID-01:先作RAID-0,再作RAID-1
讀寫性能有提高
可用空間降低一半
冗餘能力:每組能夠同時壞兩塊
五、建立一個大小爲10G的RAID1,要求有一個空閒盤,並且CHUNK大小爲128k;
一、 格式化三塊大小爲5G(因爲磁盤過小就5G)的分區/dev/sdb1 /dev/sdb2 /dev/sdb3 二、建立raid1 [root@localhost ~]# mdadm -C /dev/md0 -l 1 -c 128 -n 2 -x 1 /dev/sdb1 /dev/sdb2 /dev/sdb3 mdadm: /dev/sdb1 appears to contain an ext2fs file system size=10485760K mtime=Thu Oct 13 10:38:39 2016mdadm: 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.90Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started. 三、查看raid1狀態 [root@localhost ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Thu Oct 13 15:06:49 2016 Raid Level : raid1 Array Size : 5238784 (5.00 GiB 5.36 GB) Used Dev Size : 5238784 (5.00 GiB 5.36 GB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Thu Oct 13 15:07:12 2016 State : clean, resyncing Active Devices : 2 Working Devices : 3 Failed Devices : 0 Spare Devices : 1 Resync Status : 91% complete Name : localhost.localdomain:0 (local to host localhost.localdomain) UUID : ebc9581c:20f4b9a8:102088b0:8e7ad539 Events : 14 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 18 1 active sync /dev/sdb2 2 8 19 - spare /dev/sdb3
六、建立一個大小爲4G的RAID5設備,chunk大小爲256k,格式化ext4文件系統,要求可開機自動掛載至/backup目錄,並且不更新訪問時間戳,且支持acl功能;
一、格式化三塊大小爲5G的分區/dev/sdb1 /dev/sdb2 /dev/sdb3 二、建立raid5設備 [root@localhost ~]# mdadm -C /dev/md0 -l 5 -n 3 -c 256 /dev/sdb1 /dev/sdb2 /dev/sdb3mdadm: /dev/sdb1 appears to contain an ext2fs file system size=10485760K mtime=Thu Oct 13 10:38:39 2016 mdadm: /dev/sdb1 appears to be part of a raid array: level=raid1 devices=2 ctime=Thu Oct 13 15:06:49 2016 mdadm: /dev/sdb2 appears to be part of a raid array: level=raid1 devices=2 ctime=Thu Oct 13 15:06:49 2016 mdadm: /dev/sdb3 appears to be part of a raid array: level=raid1 devices=2 ctime=Thu Oct 13 15:06:49 2016 Continue creating array? t Continue creating array? (y/n) y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started. 三、查看raid5 [root@localhost ~]# mdadm -D /dev/md0/dev/md0: Version : 1.2 Creation Time : Thu Oct 13 15:18:35 2016 Raid Level : raid5 Array Size : 10477568 (9.99 GiB 10.73 GB) Used Dev Size : 5238784 (5.00 GiB 5.36 GB) Raid Devices : 3 Total Devices : 3 Persistence : Superblock is persistent Update Time : Thu Oct 13 15:19:05 2016 State : clean Active Devices : 3 Working Devices : 3 Failed Devices : 0 Spare Devices : 0 Layout : left-symmetric Chunk Size : 256K Name : localhost.localdomain:0 (local to host localhost.localdomain) UUID : 94fc5461:5eb50e79:7e2a1c15:f98de37b Events : 18 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 18 1 active sync /dev/sdb2 3 8 19 2 active sync /dev/sdb3 四、格式化爲ext4系統 [root@localhost ~]# mke2fs -t ext4 /dev/md0mke2fs 1.42.9 (28-Dec-2013) 文件系統標籤= OS type: Linux 塊大小=4096 (log=2) 分塊大小=4096 (log=2) Stride=64 blocks, Stripe width=128 blocks 655360 inodes, 2619392 blocks 130969 blocks (5.00%) reserved for the super user 第一個數據塊=0 Maximum filesystem blocks=2151677952 80 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, 1605632 Allocating group tables: 完成 正在寫入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 五、開機掛載/backup [root@localhost ~]# mkdir /backup[root@localhost ~]# vim /etc/fstab ## /etc/fstab# Created by anaconda on Tue Aug 2 19:48:21 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#/dev/mapper/centos-root / xfs defaults 0 0 UUID=5f193028-e7c9-4aed-9506-3a37e24ce9d4 /boot xfs defaults 0 0 /dev/mapper/centos-home /home xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0#/dev/sdb2 swap swap defaults 0 0/dev/md0 /backup ext4 defaults,noatime,acl 0 0
七、寫一個腳本
(1) 傳遞兩個以上字符串看成用戶名;
(2) 建立這些用戶;且密碼同用戶名;
(3) 總結說明共建立了幾個用戶;
#!/bin/bashif [ $# -lt 1 ];then echo 'at least one param' exit 1fiTEXT=0for i in $*;do if [ ! -f $i ];then echo "${i} is not a ASCII text"; else echo "${i} have "$(wc -l < ${i})" lines" let TEXT++ fidoneecho "count file numbers:"$TEXT
八、寫一腳本,分別統計/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#號開頭的行數之和,以及總的空白行數;
#!/bin/bash startWithJingLines=0 spaceLines=0 files='/etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab'for i in $files;do if [ -f $i ];then let startWithJingLines=${startWithJingLines}+$(grep '^#' ${i}|wc -l) let spaceLines=${spaceLines}+$(grep '^$' ${i}|wc -l) else echo "$i not exits." fidoneecho "There has $startWithJingLines lines begin with # in the three files."echo "There has $spaceLines space-line in the three files."
九、寫一個腳本,顯示當前系統上全部,擁有附加組的用戶的用戶名;並說明共有多少個此類用戶;
#!/bin/bash userName="The user that has odditionalgroup has:" num=0 for i in $(cat /etc/passwd|cut -d: -f1);do name=$(echo $i | cut -d: -f1) groups=$(id -G $name | wc -w) if [ $groups -ge 2 ];then userName=$userName\ $name let num++ fi done echo $userName echo "The sum user that has odditionalgroup is:"$num
十、建立一個由至少兩個物理卷組成的大小爲20G的卷組;要求,PE大小爲8M;而在卷組中建立一個大小爲5G的邏輯卷mylv1,格式化爲ext4文件系統,開機自動掛載至/users目錄,支持acl;
一、先格式化2個分區,且調整分區系統類型爲8e二、建立pv [root@localhost ~]# pvcreate /dev/sdc1 Can't open /dev/sdc1 exclusively. Mounted filesystem? [root@localhost ~]# dmsetup remove sdc1[root@localhost ~]# pvcreate /dev/sdc1 Physical volume "/dev/sdc1" successfully created [root@localhost ~]# pvdisplay /dev/sdc1 "/dev/sdc1" is a new physical volume of "10.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc1 VG Name PV Size 10.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID GsVY4P-kA1b-Ji2I-pERJ-coFA-x6ml-q9eAbo [root@localhost ~]# dmsetup remove sdc2[root@localhost ~]# pvcreate /dev/sdc2 Physical volume "/dev/sdc2" successfully created三、建立vg [root@localhost ~]# vgcreate -s 8M /dev/sdc1 /dev/sdc2 /dev/sdc1: already exists in filesystem Run `vgcreate --help' for more information.[root@localhost ~]# vgcreate -s 8M myvg /dev/sdc1 /dev/sdc2 Volume group "myvg" successfully created [root@localhost ~]# vgdisplay myvg --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 15.01 GiB PE Size 8.00 MiB Total PE 1921 Alloc PE / Size 0 / 0 Free PE / Size 1921 / 15.01 GiB VG UUID yW3dZh-TPgd-AUPN-3gfW-f8UR-cUU8-W1tXdf四、建立lvm [root@localhost ~]# lvcreate -L 5G -n mylvm myvg Logical volume "mylvm" created. [root@localhost ~]# lvdisplay /dev/myvg/mylvm --- Logical volume --- LV Path /dev/myvg/mylvm LV Name mylvm VG Name myvg LV UUID jEkZ4X-BicC-mDfQ-TmQ6-lIgZ-MTNN-JSnXgb LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-10-14 06:36:18 +0800 LV Status available # open 0 LV Size 5.00 GiB Current LE 640 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2五、格式化爲ext4文件系統 [root@localhost ~]# mke2fs -t ext4 /dev/myvg/mylvmmke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks327680 inodes, 1310720 blocks65536 blocks (5.00%) reserved for the super user First data block=0Maximum filesystem blocks=134217728040 block groups32768 blocks per group, 32768 fragments per group8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 23 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override六、開機掛載 vim /etc/fstab [root@localhost ~]# mkdir /users[root@localhost ~]# vim /etc/fstab/dev/myvg/mylvm /users ext4 defaults,acl 0 0
十一、擴展mylv1至9G,確保擴展完成後原有數據徹底可用;
[root@localhost ~]# lvextend -L 9G /dev/myvg/mylvm Size of logical volume myvg/mylvm changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents). Logical volume mylvm successfully resized. [root@localhost ~]# lvdisplay /dev/myvg/mylvm --- Logical volume --- LV Path /dev/myvg/mylvm LV Name mylvm VG Name myvg LV UUID jEkZ4X-BicC-mDfQ-TmQ6-lIgZ-MTNN-JSnXgb LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-10-14 06:36:18 +0800 LV Status available # open 0 LV Size 9.00 GiB Current LE 1152 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 [root@localhost ~]# resize2fs /dev/myvg/mylvm resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylvm to 2359296 (4k) blocks. The filesystem on /dev/myvg/mylvm is now 2359296 blocks long.
十二、縮減mylv1至7G,確保縮減完成後原有數據徹底可用;
一、先取消掛載 [root@localhost ~]# umount /dev//myvg/mylvm 二、文件系統強制檢測 [root@localhost ~]# e2fsck -f /dev/myvg/mylvm e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/myvg/mylvm: 11/589824 files (0.0% non-contiguous), 72671/2359296 blocks [root@localhost ~]# resize2fs /dev/myvg/mylvm 7G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylvm to 1835008 (4k) blocks. The filesystem on /dev/myvg/mylvm is now 1835008 blocks long.三、縮減邏輯邊界 [root@localhost ~]# lvreduce -L 7G /dev/myvg/mylvm WARNING: Reducing active logical volume to 7.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce myvg/mylvm? [y/n]: y Size of logical volume myvg/mylvm changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents). Logical volume mylvm successfully resized.
1三、對mylv1建立快照,並經過備份數據;要求保留原有的屬主屬組等信息;
[root@localhost ~]# lvcreate -L 512M -p r -s -n mylvm_snapshot /dev/myvg/mylvm Logical volume "mylvm_snapshot" created. [root@localhost ~]# mount /dev/myvg/mylvm_snapshot /mnt
1四、如何將Linux主機接入到TCP/IP網絡,請描述詳細的步驟。(手動指定的方式)
(1)使用ifconfig查看網卡配置信息,看主要使用的網卡名稱
(2)到/etc/sysconfig/network-scripts/目錄下查找對應的網卡配置文件
(3)主要配置項
DEVICE(設備名稱)
TYPE(設備類型。端口爲Ethernet)
ONBOOT(系統啓動時讀取)
BOOTPROTO=static(ip配置協議。當前爲靜態ip,可設置DHCP,)
IPADDR(ip地址)
NETMASK(子網掩碼)
GATEWAY(網關地址)
DNS1(DNS服務器地址)
(4)重啓網絡服務 service network restart
(5)在CLI中ping baidu.com測試網絡鏈接
1五、爲Linux主機配置網絡信息的方式有哪些,請描述各個過程。
(1)臨時修改方式vim /etc/sysconfig/network-scripts/ifcfg-eno16777736
(3)圖形界面配置。
1六、寫一個腳本,使用ping命令探測172.16.250.1-172.16.250.254之間的全部主機的在線狀態;
在線的主機使用綠色顯示;
不在線的主使用紅色顯示;
#!/bin/bash # for i in {1..254};do ping -c 1 -w 1 "192.168.1.$i" > /dev/null if [ $? -eq 0 ];then echo -e "\033[0;32;1m172.16.250.$i is up \033[0m" else echo -e "\033[0;31;1m172.16.250.$i is down \033[0m" fi done
1七、經常使用的網絡管理類工具備哪些,並用示例形式描述他們的使用方法。
(1)ifconfig
常見用法:
查看網卡信息:ifconfig [interface]
查看全部網卡信息:ifconifg -a
開啓或關閉網卡:ifconfig IFACE [up|down]
配置網卡參數:ifconfig interface [aftype] options | address ...
配置網卡IP地址:
ifconfig IFACE IP/mask [up]
ifconfig IFACE IP network MASK
(2)ip:顯示/配置路由,接口,策略路由和隧道
常見用法:
網卡添加多一個IP地址,並設置別名爲eth0:0
ip addr IPADDR/MASK dev eth0label 'eth0:0'
顯示網卡設備的信息: ip addr show
清除IP地址信息:ip addr flush dev interface
添加路由:ip route add TARGET via GW dev INTERFACE src SOURCE_IP
刪除單條路由: ip route del TARGET
(3)route:路由管理命令
查看路由條目:route -n
添加路由條目:route add
route add [-net|-host] target [netmask Nm] [gw Gw] [[dev] if]
刪除路由:route del
route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]
(4)netstat :顯示網絡鏈接、路由表、接口數據統計、假裝鏈接、組播成員關係
經常使用選項:
-t : 顯示TCP協議相關已創建的鏈接(ESTABLISHED)
-u : 顯示UDP協議相關已創建的鏈接(ESTABLISHED)
-r : 顯示raw socket(裸套接字)相關已創建的鏈接(ESTABLISHED)
-l : 處於監聽狀態的鏈接
-n : 以數字顯示IP和協議端口(默認顯示協議名稱,如SSH)
-a : 顯示全部狀態的鏈接
-e : 擴展格式
-p : 顯示相關進程及PID
經常使用組合:
-tan , -uan , -tnl, -unl,-tunlp
(5)ss:網絡狀態查看工具
常見選項:
-t : tcp協議相關
-u : udp協議相關
-w : 裸套接字相關
-x : unix sock相關
-l : listen狀態的鏈接
-a : 全部
-n : 數字格式
-p : 相關的程序及PID
-e : 擴展的信息
-m : 內存用量
-o : 計時器信息
經常使用組合:
-tan , -tanl , -tanlp ,-uan,-tunlp
1八、寫一個腳本,完成如下功能
(1) 假設某目錄(/etc/rc.d/rc3.d/)下分別有K開頭的文件和S開頭的文件若干;
(2) 顯示全部以K開頭的文件的文件名,而且給其附加一個stop字符串;
(3) 顯示全部以S開頭的文件的文件名,而且給其附加一個start字符串;
(4) 分別統計S開頭和K開頭的文件各有多少;
#!/bin/bash declare -i k=0,s=0 for i in $(ls /etc/rc.d/rc3.d/K* | grep -o"[^/]*$");do echo "$i stop" let k++ done for j in $(ls /etc/rc.d/rc3.d/S* | grep -o"[^/]*$");do echo "$j start" let s++ done echo "S開頭的文件有$s個" echo "K開頭的文件有$k個"
1九、寫一腳本,用ping命令測試172.16.250.20-172.16.250.100之內有哪些主機在線,將在線的顯示出來;
#!/bin/bash #author:BaoZhang online_number=0 no_online_number=0 for i in 192.168.88.{100..200} do ping-c 2 -W 2 $i &>/dev/null if[ $? -eq 0 ];then echo "$i is onlline" let online_number++ else echo "$i not online" let no_online_number++ fi done echo "$online_number hostonline,$no_online_number not online "
20、打印九九乘法表;
#!/bin/bash #author:BaoZhang for((i=1;i<10;i++)) do for((j=1;j<=i;j++)) do echo -n "$j*$i=$[ $j * $i ]" echo -n " " done echo "" done