Linux+Python高端运维班第四次作业
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux+Python高端运维班第四次作业相关的知识,希望对你有一定的参考价值。
1、创建一个10G分区,并格式为ext4文件系统;
[[email protected] ~]# fdisk /dev/sdb //管理分区sdb
命令(输入 m 获取帮助):n //输入命令n创建一个新分区
Select (default p): p //设置分区类型为主分区
分区号 (1-4,默认 1):1 //设置分区号
起始 扇区 (2048-41943039,默认为 2048): //设置起始扇区,因为这里是sdb上第一个分区所以不输入任何信息,从默认起始扇区开始
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+10G //设置分区大小
分区 1 已设置为 Linux 类型,大小设为 10GiB
命令(输入 m 获取帮助):w //输入w保存并退出
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
[[email protected] ~]# 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目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[[email protected] ~]# mount -oacl,noatime,noexec -L MYDATA /data/mydata/
[[email protected] ~]# mount | grep sdb1 //查看sdb1分区是否挂载
/dev/sdb1 on /data/mydata type ext4(rw,noexec,noatime,seclabel,data=ordered)
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
[[email protected] ~]# 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保存退出
[[email protected] ~]# partx -a /dev/sdb //强制重读磁盘分区表
[[email protected] ~]# mkswap /dev/sdb2 //创建swap设备
正在设置交换空间版本 1,大小 = 1048572KiB
无标签,UUID=06f2a16f-9705-4b94-bdb6-05e164ff31c1
[[email protected] ~]# free //挂载前swap分区大小
[[email protected] ~]# swapon /dev/sdb2 //将分区sdb2启用为交换分区
[[email protected] ~]# free //启用之后swap分区大小
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash # fdisk -l | grep "^/.*" echo " " df -lh
4、总结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
读写性能有提升
可用空间下降一半
冗余能力:每组可以同时坏两块
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
1、 格式化三块大小为5G(由于磁盘太小就5G)的分区/dev/sdb1 /dev/sdb2 /dev/sdb3 2、创建raid1 [[email protected] ~]# 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. 3、查看raid1状态 [[email protected] ~]# 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
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
1、格式化三块大小为5G的分区/dev/sdb1 /dev/sdb2 /dev/sdb3 2、创建raid5设备 [[email protected] ~]# 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. 3、查看raid5 [[email protected] ~]# 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 4、格式化为ext4系统 [[email protected] ~]# 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: 完成 5、开机挂载/backup [[email protected] ~]# mkdir /backup[[email protected] ~]# 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
7、写一个脚本
(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
8、写一脚本,分别统计/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."
9、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/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
10、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
1、先格式化2个分区,且调整分区系统类型为8e2、创建pv [[email protected] ~]# pvcreate /dev/sdc1 Can‘t open /dev/sdc1 exclusively. Mounted filesystem? [[email protected] ~]# dmsetup remove sdc1[[email protected] ~]# pvcreate /dev/sdc1 Physical volume "/dev/sdc1" successfully created [[email protected] ~]# 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 [[email protected] ~]# dmsetup remove sdc2[[email protected] ~]# pvcreate /dev/sdc2 Physical volume "/dev/sdc2" successfully created3、创建vg [[email protected] ~]# vgcreate -s 8M /dev/sdc1 /dev/sdc2 /dev/sdc1: already exists in filesystem Run `vgcreate --help‘ for more information.[[email protected] ~]# vgcreate -s 8M myvg /dev/sdc1 /dev/sdc2 Volume group "myvg" successfully created [[email protected] ~]# 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-W1tXdf4、创建lvm [[email protected] ~]# lvcreate -L 5G -n mylvm myvg Logical volume "mylvm" created. [[email protected] ~]# 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:25、格式化为ext4文件系统 [[email protected] ~]# 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 override6、开机挂载 vim /etc/fstab [[email protected] ~]# mkdir /users[[email protected] ~]# vim /etc/fstab/dev/myvg/mylvm /users ext4 defaults,acl 0 0
11、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[[email protected] ~]# 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. [[email protected] ~]# 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 [[email protected] ~]# 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.
12、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
1、先取消挂载 [[email protected] ~]# umount /dev//myvg/mylvm 2、文件系统强制检测 [[email protected] ~]# 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 [[email protected] ~]# 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.3、缩减逻辑边界 [[email protected] ~]# 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.
13、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
[[email protected] ~]# lvcreate -L 512M -p r -s -n mylvm_snapshot /dev/myvg/mylvm Logical volume "mylvm_snapshot" created. [[email protected] ~]# mount /dev/myvg/mylvm_snapshot /mnt
14、如何将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测试网络连接
15、为Linux主机配置网络信息的方式有哪些,请描述各个过程。
(1)临时修改方式ifconfig eno16777736 192.168.x.x/24 #ifconfig命令
ip addr add 192.168.x.x/24 dev eth0 #ip命令
(2)编辑配置文件
vim /etc/sysconfig/network-scripts/ifcfg-eno16777736
(3)图形界面配置。
16、写一个脚本,使用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
17、常用的网络管理类工具有哪些,并用示例形式描述他们的使用方法。
(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
18、写一个脚本,完成以下功能
(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个"
19、写一脚本,用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
本文出自 “学海无涯” 博客,谢绝转载!
以上是关于Linux+Python高端运维班第四次作业的主要内容,如果未能解决你的问题,请参考以下文章
马哥2016全新Linux+Python高端运维班第四期-第五次作业
马哥2016全新Linux+Python高端运维班第四期-第六次作业