首页 > 代码库 > 马哥第四次作业
马哥第四次作业
1、创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
答:
[root@localhost ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e):
Using default response e
已选择分区 4
起始 扇区 (19963904-62914559,默认为 19963904):
将使用默认值 19963904
Last 扇区, +扇区 or +size{K,M,G} (19963904-62914559,默认为 62914559):
将使用默认值 62914559
分区 4 已设置为 Extended 类型,大小设为 20.5 GiB
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 5
起始 扇区 (19965952-62914559,默认为 19965952):
将使用默认值 19965952
Last 扇区, +扇区 or +size{K,M,G} (19965952-62914559,默认为 62914559):+10G
分区 5 已设置为 Linux 类型,大小设为 10 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
正在同步磁盘。
[root@localhost ~]# part
parted partprobe partx
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-3
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-5
[root@localhost ~]# partx -a /dev/sda
[root@localhost ~]# mkfs.ext4 -b 2048 -m 2 -L MYDATA /dev/sda5
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: 完成
[root@localhost ~]# tune2fs -o acl /dev/sda5
tune2fs 1.42.9 (28-Dec-2013)
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@localhost ~]# mkdir -pv /data/mydata
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/mydata"
[root@localhost ~]# mount -o noexec,noatime /dev/sda5 /data/mydata
[root@localhost ~]# mount | grep "/dev/sda5"
/dev/sda5 on /data/mydata type ext4 (rw,noexec,noatime,seclabel,data=http://www.mamicode.com/ordered)
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
答:
查看当前SWAP分区:
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 480 91 223 4 165 343
Swap: 2047 0 2047
新建分区:
[root@localhost ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 6
起始 扇区 (40939520-62914559,默认为 40939520):
将使用默认值 40939520
Last 扇区, +扇区 or +size{K,M,G} (40939520-62914559,默认为 62914559):+1G
分区 6 已设置为 Linux 类型,大小设为 1 GiB
命令(输入 m 获取帮助):t
分区号 (1-6,默认 6):6
(输入 L 列出所有代码):82
已将分区“Linux”的类型更改为“Linux swap / Solaris”
命令(输入 m 获取帮助):p
磁盘 /dev/sda:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000264ae
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 6293503 2097152 82 Linux swap / Solaris
/dev/sda3 6293504 19963903 6835200 83 Linux
/dev/sda4 19963904 62914559 21475328 5 Extended
/dev/sda5 19965952 40937471 10485760 83 Linux
/dev/sda6 40939520 43036671 1048576 82 Linux swap / Solaris
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
正在同步磁盘。
通知内核重新读取硬盘分区表:
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-6
格式化分区sda6为swap分区:
[root@localhost ~]# mkswap /dev/sda6
正在设置交换空间版本 1,大小 = 1048572 KiB
无标签,UUID=a16cc71f-dae1-45e7-9f56-01159d374ace
激活swap分区:
[root@localhost ~]# swapon /dev/sda6
查看swap分区的大小:
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 480 92 222 4 166 342
Swap: 3071 0 3071
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
答:
[root@localhost ~]# vim displaydisk.sh
[root@localhost ~]# cat displaydisk.sh
#!/bin/bash
#
#列出当前系统上的所有磁盘设备;
fdisk -l | grep -o "^/[^[:space:]]\+"
#显示每个磁盘设备上每个分区相关的空间使用信息;
df -h
[root@localhost ~]# bash displaydisk.sh
/dev/sda1
/dev/sda2
/dev/sda3
/dev/sda4
/dev/sda5
/dev/sda6
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 6.6G 1.2G 5.4G 18% /
devtmpfs 231M 0 231M 0% /dev
tmpfs 241M 0 241M 0% /dev/shm
tmpfs 241M 4.6M 236M 2% /run
tmpfs 241M 0 241M 0% /sys/fs/cgroup
/dev/sda1 1014M 131M 884M 13% /boot
tmpfs 49M 0 49M 0% /run/user/0
4、总结RAID的各个级别及其组合方式和性能的不同;
答:
RAID-0:
读、写性能提升;
可用空间:N*min(S1,S2,...)
无容错能力
最少磁盘数:2, 2+
RAID-1:
读性能提升、写性能略有下降;
可用空间:1*min(S1,S2,...)
有冗余能力
最少磁盘数:2, 2+
RAID-4:
1101, 0110, 1011
RAID-5:
读、写性能提升
可用空间:(N-1)*min(S1,S2,...)
有容错能力:1块磁盘
最少磁盘数:3, 3+
RAID-6:
读、写性能提升
可用空间:(N-2)*min(S1,S2,...)
有容错能力:2块磁盘
最少磁盘数:4, 4+
混合类型:
RAID-10:
读、写性能提升
可用空间:N*min(S1,S2,...)/2
有容错能力:每组镜像最多只能坏一块;
最少磁盘数:4, 4+
RAID-01:
RAID-50、RAID7
JBOD:Just a Bunch Of Disks
功能:将多块磁盘的空间合并一个大的连续空间使用;
可用空间:sum(S1,S2,...)
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
答:
[root@localhost ~]# mdadm -C /dev/md1 -l 1 -x 1 -n 2 -c 128 /dev/sdb{1,2,3}
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=http://www.mamicode.com/0.90
Continue creating array?
Continue creating array? (y/n) y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
[root@localhost ~]# mdadm -D /dev/md1
/dev/md1:
Version : 1.2
Creation Time : Thu Mar 2 15:10:16 2017
Raid Level : raid1
Array Size : 10477568 (9.99 GiB 10.73 GB)
Used Dev Size : 10477568 (9.99 GiB 10.73 GB)
Raid Devices : 2
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Thu Mar 2 15:10:55 2017
State : clean, resyncing
Active Devices : 2
Working Devices : 3
Failed Devices : 0
Spare Devices : 1
Resync Status : 19% complete
Name : localhost.localdomain:1 (local to host localhost.localdomain)
UUID : 74e87cbb:e8a4f358:7cc5e429:9da2ab7b
Events : 3
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功能;
答 :
创建RAID5设备:
[root@localhost ~]# mdadm -C /dev/md2 -c 256 -l 5 -n 3 -a yes /dev/sdc{1,2,3}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md2 started.
[root@localhost ~]# mdadm -D /dev/md2
/dev/md2:
Version : 1.2
Creation Time : Thu Mar 2 15:44:48 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 : Thu Mar 2 15:45:00 2017
State : clean
Active Devices : 3
Working Devices : 3
Failed Devices : 0
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 256K
Name : localhost.localdomain:2 (local to host localhost.localdomain)
UUID : e9c5eb35:e541316c:d8bcd176:a0e532d1
Events : 18
Number Major Minor RaidDevice State
0 8 33 0 active sync /dev/sdc1
1 8 34 1 active sync /dev/sdc2
3 8 35 2 active sync /dev/sdc3
格式化分区为ext4文件系统:
[root@localhost ~]# mkfs.ext4 /dev/md2
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=64 blocks, Stripe width=128 blocks
262144 inodes, 1047552 blocks
52377 blocks (5.00%) reserved for the super user
第一个数据块=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
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
挂载至/backup目录:
创建backup目录:
[root@localhost ~]# mkdir /backup
vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Jan 13 17:19:57 2017
#
# 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
#
UUID=28bcc455-9cdd-4568-83a7-218dbafef644 / xfs defaults 0 0
UUID=df7314df-b9eb-4496-b19d-fe7d74eb5959 /boot xfs defaults 0 0
UUID=f148e1b5-56cf-4107-badf-eb6ec3c610f3 swap swap defaults 0 0
/dev/md2 /backup ext4 defaults,acl,noatime 0 0
[root@localhost ~]# mount | grep "^/dev/md2"
/dev/md2 on /backup type ext4 (rw,relatime,seclabel,stripe=128,data=http://www.mamicode.com/ordered)
7、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
答:
[root@localhost ~]# cat useradd1.sh
#!/bim/bash
#
#
if [ $# -lt 2 ];then
echo "Please enter at least 2 parameters."
else
num=0
for i in $@;do
if id $i &> /dev/null;then
echo "$i is exists."
else
useradd $i && echo $i | passwd --stdin $i &>/dev/null
echo "user: $i created successfuily!"
let num=$num+1
fi
done
echo "Created a total of $num users."
fi
[root@localhost ~]# bash useradd1.sh test1 test2 bob
user: test1 created successfuily!
user: test2 created successfuily!
user: bob created successfuily!
Created a total of 3 users.
8、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
答:
[root@localhost ~]# cat 8.sh
#!/bin/bash
#
#
#
declare -i sum1=0
declare -i sum2=0
for i in {/etc/fstab,/etc/rc.d/rc.sysinit,/etc/rc.d/init.d/functions}; do
sum1+=$( grep -c "^#" $i )
sum2+=$( grep -c "^[[:space:]]*#" $i )
done
echo "# line: $sum1"
echo "space line: $sum2"
[root@localhost ~]# bash 8.sh
# line: 94
space line: 167
9、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
答:
[root@localhost ~]# cat othergroup_user.sh
#!/bin/bash
#
#
declare -i sum=0
for user in `awk -F: ‘{print $1}‘ /etc/passwd`; do
othergroup=$(id $user | awk -F, ‘{print $2}‘)
if [ -n "$othergroup" ]; then
echo "$user"
let sum++
fi
done
echo "othergroup_user_sum:$sum"
[root@localhost ~]# bash othergroup_user.sh
postfix
test1
test2
othergroup_user_sum:3
10、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
答:
[root@localhost ~]# pvcreate /dev/sda5
Physical volume "/dev/sda5" successfully created
[root@localhost ~]# pvcreate /dev/sda6
Physical volume "/dev/sda6" successfully created
创建名为myvg1的卷组:
[root@localhost ~]# vgcreate myvg1 -s 8 /dev/sda5 /dev/sda6
Volume group "myvg1" successfully created
查看卷组的信息:
[root@localhost ~]# vgdisplay myvg1
--- Volume group ---
VG Name myvg1
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 20.00 GiB
PE Size 8.00 MiB
Total PE 2560
Alloc PE / Size 0 / 0
Free PE / Size 2560 / 20.00 GiB
VG UUID lRLwpx-yabx-pVdl-JW8r-sg97-4Lj9-Jm07W4
在卷组myvg1上创建逻辑卷mylv1:
[root@localhost ~]# lvcreate -L 5G -n mylv1 myvg1
Logical volume "mylv1" created.
格式化逻辑卷mylv1为ext4格式:
[root@localhost ~]# mkfs.ext4 /dev/myvg1/mylv1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1342177280
40 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
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
开机挂载逻辑卷mylv1至users:
[root@localhost ~]# echo "/dev/myvg1/mylv1 /users ext4 defaults,acl 0 0" >> /etc/fstab
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Jan 14 19:08:03 2017
#
# 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
#
UUID=1819c92e-cb45-4a97-94af-684851485f8b / ext4 defaults 1 1
UUID=3120ff18-0e06-4a72-a17d-c89fc762682a /boot ext4 defaults 1 2
UUID=61a7e0f6-c335-4ff4-b59c-482dde280801 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/myvg1/mylv1 /users ext4 defaults,acl 0 0
[root@localhost ~]# mount -a
[root@localhost ~]# mount
/dev/sda2 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)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/myvg1-mylv1 on /users type ext4 (rw,acl)
11、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
答:
[root@localhost ~]# lvextend -L +4G /dev/myvg1/mylv1
Size of logical volume myvg1/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents).
Logical volume mylv1 successfully resized.
[root@localhost ~]# resize2fs /dev/myvg1/mylv1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg1/mylv1 is mounted on /users; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/myvg1/mylv1 to 2359296 (4k) blocks.
The filesystem on /dev/myvg1/mylv1 is now 2359296 blocks long.
[root@localhost ~]# df -ah
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 7.5G 2.2G 5.0G 31% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
tmpfs 242M 0 242M 0% /dev/shm
/dev/sda1 488M 32M 431M 7% /boot
none 0 0 0 - /proc/sys/fs/binfmt_misc
/dev/mapper/myvg1-mylv1 8.8G 12M 8.3G 1% /users
12、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
答:
缩减逻辑卷前必须先卸载:
[root@localhost ~]# umount /dev/myvg1/mylv1
[root@localhost ~]# df -ah
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 7.5G 2.2G 5.0G 31% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
tmpfs 242M 0 242M 0% /dev/shm
/dev/sda1 488M 32M 431M 7% /boot
none 0 0 0 - /proc/sys/fs/binfmt_misc
[root@localhost ~]# e2fsck -f /dev/myvg1/mylv1
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/myvg1/mylv1: 12/589824 files (0.0% non-contiguous), 72672/2359296 blocks
[root@localhost ~]# resize2fs /dev/myvg1/mylv1 7G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg1/mylv1 to 1835008 (4k) blocks.
The filesystem on /dev/myvg1/mylv1 is now 1835008 blocks long.
[root@localhost ~]# lvreduce -L -2G /dev/myvg1/mylv1
WARNING: Reducing active logical volume to 7.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myvg1/mylv1? [y/n]: y
Size of logical volume myvg1/mylv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents).
Logical volume mylv1 successfully resized.
重新挂载:
[root@localhost ~]# mount -a
[root@localhost ~]# df -ah
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 7.5G 2.2G 5.0G 31% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
tmpfs 242M 0 242M 0% /dev/shm
/dev/sda1 488M 32M 431M 7% /boot
none 0 0 0 - /proc/sys/fs/binfmt_misc
/dev/mapper/myvg1-mylv1 6.8G 12M 6.5G 1% /users
13、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
答:
[root@localhost ~]# lvcreate -L 3G -p r -s -n mylv1.bak /dev/myvg1/mylv1
Logical volume "mylv1.bak" created.
[root@localhost ~]# mkdir /lvbackup
[root@localhost ~]# mount /dev/myvg1/mylv1.bak /lvbackup
mount: block device /dev/mapper/myvg1-mylv1.bak is write-protected, mounting read-only
14、如何将Linux主机接入到TCP/IP网络,请描述详细的步骤。(手动指定的方式)
答:
Linux系统上配置IP地址步骤:
(1)用root用户登录系统,检查网卡设备是否被内核检测到;
[root@localhost ~]# lspci | grep Ethernet
02:00.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)
(2)配置临时IP地址,重启后失效;
[root@localhost ~]# ifup eth0 192.168.1.106 netmask 255.255.255.0
[root@localhost ~]# ifup eth0
[root@localhost ~]# ifdown eth0
(3)修改网卡配置文件,永久生效;
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:8F:B4:FF
TYPE=Ethernet
UUID=b534b8c7-2b67-445d-9621-7d936e406455
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.1.106
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=202.96.128.86
DNS2=202.96.128.166
15、为Linux主机配置网络信息的方式有哪些,请描述各个过程。
答:
(1)使用ifup命令配置,重启网络服务后失效;
[root@localhost ~]# ifup eth0 192.168.1.106 netmask 255.255.255.0
(2)修改配置文件,可永久生效;
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:8F:B4:FF
TYPE=Ethernet
UUID=b534b8c7-2b67-445d-9621-7d936e406455
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.1.106
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=202.96.128.86
DNS2=202.96.128.166
16、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;
在线的主机使用绿色显示;
不在线的主使用红色显示;
答:
本人网络和题目的网络不同,在此处更改为192.168.1.1-192.168.1.254
[root@localhost ~]# cat ping.sh
#!/bin/bash
#
net="192.168.1."
uphosts=0
downhosts=0
for i in {1..254};do
ping -c 1 -w 1 ${net}${i}&>/dev/null
if [ $? -eq 0 ];then
echo -e "\033[32m${net}${i}"
let uphosts++
else
echo -e "\033[31m${net}${i}"
let downhosts++
fi
done
echo -e "\033[37m"
echo "Up hosts is:$uphosts."
echo "Down hosts is:$downhosts."
17、常用的网络管理类工具有哪些,并用示例形式描述他们的使用方法。
答:
1)ifconfig命令
#ifconfig #显示所有网卡的信息;
#ifconfig eth0 #显示指定网卡eth0的网络信息;
#ifconfig eth0 up/down #启用或禁用网卡eth0(重启网络服务后失效);
#ifup/ifdown eth0 #启用或禁用网卡eth0(重启网络服务后失效);
(2)ip命令
常用的子命令ip { link | addr | route }
ip link:
show [ DEVICE ] [ up | down ] :显示状态是up或down的接口,默认显示up接口;
set DEVICE { up | down | arp { on | off } } :设置接口的二层属性;
ip addr:
ipaddr { add | del } IFADDR dev STRING
添加IP:
[root@ilinux~]# ip addr add 192.168.80.222/24 dev eth0:1
删除IP:
[root@ilinux~]# ip addr del 192.168.80.222 dev eth0:1
ip route:
显示路由条目;
# ip route
添加路由;
# ip route add TARGET via GW dev IFACE src SOURCE_IP
示例:
主机路由:IP
#ip route add 192.168.1.3 via 172.16.0.1 dev eth0
网络路由:NETWORK/MASK
#ip route add 192.168.0.0/24 via 172.16.1.3
添加网关;
#ip route add default via GW dev IFACE
删除路由
#ip route del TARGET
(3)netstat命令
查看当前系统网络连接,路由信息表,网卡信息;
常用:
netstat-r
netstat-ntlp
netstat-nulp
netstat-na
18、写一个脚本,完成以下功能
(1) 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干;
(2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串;
(3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串;
(4) 分别统计S开头和K开头的文件各有多少;
答:
[root@localhost ~]# cat test1.sh
#!/bin/bash
k=0
s=0
for i in $(ls /etc/rc.d/rc3.d/K* | grep -o "[^/]*$" ); do
echo "$i stop"
let k=$k+1
done
for i in $(ls /etc/rc.d/rc3.d/S* | grep -o "[^/]*$" ); do
echo "$i start"
let s=$k+1
done
echo "The number of files startingwith K : $k."
echo "The number of files startingwith S : $s."
19、写一脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来;
答:
本人网络和题目的网络不同,在此处更改为192.168.1.20-192.168.1.100
[root@localhost ~]# cat ping1.sh
#!/bin/bash
#
#
net="192.168.1."
for i in {20..100}; do
ping -c 1 -w 1 ${net}${i} &> /dev/null
if [ $? -eq 0 ]; then
echo "${net}${i}"
fi
done
20、打印九九乘法表;
答:
用while循环实现:
#/bin/bash
#
declare -i i=1
declare -i j=1
while [ $j -le 9 ];do
while [ $i -le $j ];do
echo -e -n "${i}X${j}=$[ $i*$j ]\t"
let i++
done
echo
let i=1
let j++
done
用until循环实现:
#/bin/bash
#
declare -i j=1
declare -i i=1
until [ $j -gt 9 ];do
until [ $i -gt $j ];do
echo -n -e "${i}X${j}=$[ $i*$j]\t"
let i++
done
echo
let i=1
let j++
done
用for循环实现1:
#/bin/bash
#
for j in {1..9};do
for i in `seq 1 $j`;do
echo -en "${i}X${j}=$[ $i*$j ]\t"
done
echo
done
马哥第四次作业