【2-1】Linux之系统管理及网络服务
一、设备识别
设备接入系统后都是以文件的形式存在
设备文件名称:
SATA/SAS/USB /dev/sda,/dev/sdb ##s=SATA d=DISK a=第几块
IDE /dev/hd0, /dev/hd1 ##h=hard
VIRTIO=BLOCK /dev/vda,/dev/vdb ##v=virtio
M2(SSD) /dev/nvme0,/dev/nvme1 ##nvme=m2
SD/MMC/EMMC(卡) /dev/mmcblk0,/dev/mmcblk1 ##mmcblk=mmc卡
光驱 /dev/cdrom,/dev/sr0,/dev/sr1
设备查看
fdisk -l
查看磁盘分区情况
lsblk
设备使用情况
blkid
设备管理方式及设备id
df
查看正在被系统挂载的设备
cat /proc/partitions
查看系统识别设备
二、设备挂载
在系统中有设备id的设备是可以被系统使用的
挂载命令:
mount -o 挂载参数 挂载点
rw 读写 remount重新挂载 ro 只读
mount 设备 挂载点
umount 设备|挂载点
mount
查看挂载信息
mount -o rw /dev/vda1 /westos
mount -o remount,ro /westos
##转换挂载参数由读写变为只读
在卸载时当出现设备正忙:
fuser -kvm 设备|挂载点
-k 结束进程 -v 现实详细信息 -m 显示进程
三、设备中的文件查找
find -name
find -user
find -group
find -type f d s b l
find -perm 222 /222 -222
find -exec
find -maxdepth 1
find -mindepth 1
find -cmin 1 -1 +1
find +|-| 1M
find -o
find -a
find -not
find -name
[root@node126 ~]# find /etc/ -name passwd
[root@node126 ~]# touch /test/
[root@node126 ~]# chmod 777 /test/
[root@node126 ~]# su - westos
[westos@node126 ~]$ touch /test/444
[westos@node126 ~]$ exit
logout
[root@node126 ~]# touch /test/555
[root@node126 ~]# su - lee
[lee@node126 ~]$ touch /test/666
[lee@node126 ~]$ exit
logout
[root@node126 ~]# chgrp lee /test/444
[root@node126 ~]#
find -user
[root@node126 ~]# find /test/ -user westos
/test/444
[root@node126 ~]#
[root@node126 ~]# find /test/ -not -user westos
/test/
/test/555
/test/666
[root@node126 ~]#
find -group
find -type f d s b l
[root@node126 ~]# find /test/ -type d
/test/
[root@node126 ~]#
[root@node126 ~]# find /test/ -type f
/test/444
/test/555
/test/666
[root@node126 ~]#
find -perm 222
ugo权限正好对应222
[root@node126 ~]# find /test/ -perm 644
/test/555
[root@node126 ~]#
find -perm /222
文件权限u或g或o含有2
[root@node126 ~]# find /test/ -perm /020
/test/
/test/444
/test/666
[root@node126 ~]#
find -perm -222
文件权限u位有2,g位有2,o位有2
[root@node126 ~]# find /test/ -perm -640
/test/
/test/444
/test/555
/test/666
[root@node126 ~]# find /test/ -perm -620
/test/
/test/444
/test/666
[root@node126 ~]#
find -exec
find -maxdepth 1
[root@node126 ~]# find /etc/ -maxdepth 1 -name passwd
find -mindepth 1
[root@node126 ~]# find /etc/ -maxdepth 2 -mindepth 2 -name passwd
find -cmin 1 -1 +1
find +|-| 1M
find -o
[root@node126 ~]# find /test/ -user westos -o -user lee
/test/444
/test/666
[root@node126 ~]#
find -a
[root@node126 ~]# find /test/ -user westos -a -group lee
/test/444
[root@node126 ~]#
find -not
[root@node126 ~]# find /test/ -perm /000 -type f -exec rm -fr {} \;
{}表示find命令查找结果
\为了转译‘;’
四、分区
分区方式: 位数 分区表大小 支持分区个数 支持单个分区大小
legacy (MBR)32 64byte 主分区4个 2.2TB
-----------------------------------------------------------------
UEFI (GPT)64 128byte 理论上无限制 8ZiB
MBR分区方式:
主分区
主分区表记录分区的信息并可以直接使用的分区
扩展分区
主分区表记录的分区,不可直接使用,只是逻辑分区容器
逻辑分区
扩展分区之上划分的分区叫做逻辑分区
分区方法
[root@node226 ~]# fdisk /dev/vdb
##进入分区软件
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x06f53088.
Command (m for help): m
##显示帮助
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition ##更改 /删除
F list free unpartitioned space
l list known partition types ##列出所有分区类型
n add a new partition ##新建
p
print the partition table ##显示分区表
t change a partition type ##更改分区类型
v verify the partition table
i print information about a partition ##
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit ##保存更改
q quit without saving changes ##退出
Create a new label
g create a new empty GPT partition table ##设定分区方式为GPT
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table ##设定分区方式为mbr
s create a new empty Sun partition table
Command (m for help):
[root@node226 ~]# dd if=/dev/zero of=/dev/vdb bs=1M count=1
清空文件系统标示
parted
parted /dev/vdb mklabel msdos
parted /dev/vdb mkpart primary 1 100
parted /dev/vdb rm 2
常用文件系统对比
文件系统 FAT16 FAT32 NTFS EXT3 EXT4 XFS
最大卷 2G 4G 256T 32T 1EB 16EB
最大单个文件 4G 8TB 16T 2TB 16TB 8EB
最大文件个数 65536 4177920 4294967295 32000个子目录 无限子目录
代表系统 DOS Win95 Win98 Win2000以后 RHEL5 RHEL6 RHEL7~
五、swap管理
临时操作
swapon -s
查看swap分区信息
创建分区并设定分区的类型为linuxswap
mkswap /dev/sda1
格式化设备swap格式
swapon /dev/sda1 -p 0-32767
-p表示指定swap的优先级
swapon -s
查看swap分区信息
创建分区并设定分区的类型为linuxswap
mkswap /dev/sda1
格式化设备swap格式
[root@localhost ~]# swapon -s
swapon /dev/sda1 -p 0-32767
-p表示指定swap的优先级
[root@localhost ~]# swapon /dev/vdb2 -p 12
[root@localhost ~]# swapon -s
如果永久添加swap分区
vim /etc/fstab
/dev/sda1 swap swap pri=4 0 0
swapon -a
enable all swaps from /etc/fstab
删除swap
vim /etc/fstab
/dev/sda1 swap swap pri=4 0 0 ##删除此行
swapoff /dev/sda1
六、磁盘配额
作用:设定用户能写入指定设备的最大额度
设定方法:
mount /dev/sda1 /pub/ -o usrquota
临时
挂载设备并激活配额参数
quotaon -uv /dev/sda1
激活配额
edquota -u lee
设定用户lee配额
Disk quotas for user lee (uid 1001):
设备 用户已经创建数据 软限 硬限 用户已创文件个数 软限 硬限
Filesystem blocks soft hard inodes soft hard
/dev/sda1 20480 0 20480 1 0 0
永久开启配额
vim /etc/fstab
/dev/sda1 /pub xfs defaults,usrquota 0 0
测试:
su - lee
cd /pub
dd if=/dev/zero of=/pub/leefile bs=1M count=22
截取数据失败只能写入20M的数据
关闭配额:
quataoff -uv /dev/sda1
reboot后限额恢复
vim /etc/fstab
去掉配额参数usrquota
reboot
测试练习:
1.在虚拟机中添加一块新的硬盘,大小为10G
2.在新硬盘中建立分区并挂载到/westosdir目录中,分区大小为500M
3.设定westos 用户和lee用户能写入/westosdir目录中的最大数据不能超过10M
4.设定/westosdir目录中所有用户可以自由新建文件但不能删除不属于自己的文件
5.在系统中添加swap分区,大小为1G,优先级为1
6.以上要求在重启主机后仍然生效