大纲
磁盘管理基本操作
grep and regular expression
find基本操作
特殊权限
环境声明:Operating system:CentOS.7.0
1.1)创建一个10G的文件系统,类型为ext4,要求开机可自动挂载至单独数据/data目录;
首先可以使用fdisk命令查看当前系统上磁盘信息,此处我们只查看/dev/sdb磁盘设备的信息,确保有足够的空间后,再来分区操作
[root@ctserver7 ~]# fdisk -l /dev/sdb Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
从以上信息可以看出,/dev/sdb设备有50GB左右大小,且为划分任何分区。现在来为/dev/sdb磁盘设备划分一个10GB大小ext4文件系统类型的分区,且开机自动挂载在/data目录下
使用fdisk命令进行磁盘管理,进入模式后可以使用p命令查看当前设备上的分区情况(当前设备无任何分区)
[root@ctserver7 ~]# fdisk /dev/sdb Command (m for help): p Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xdda9498c Device Boot Start End Blocks Id System Command (m for help):
那么我们来创建一个新分区,使用n选项;
此处让我们选择一个分区类型,默认为主分区,选择主分区
输入扇区起始位,我们使用默认,回车即可
输入扇区大小,可以使用+{K,M,G},使用+10G
提示大小为10GB的分区设置完成
Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-104857599, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): +10G Partition 1 of type Linux and of size 10 GiB is set
再次使用p命令查看,确定无误后使用w命令保存退出
Command (m for help): p Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xdda9498c Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table.
退出fdisk命令状态后,可以使用fdisk -l查看/dev/sdb的分区信息
[root@ctserver7 ~]# fdisk -l /dev/sdb Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xdda9498c Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux
可以看到分区已经成功创建,创建完成之后,开始格式化分区
可以使用mkfs.{ext2|ext3|ext4|xfs|...},mke2fs等命令来创建ext系列的分区
分别使用mkfs.ext4和mke2fs命令来创建ext4类型的分区,然后查看/dev/sdb设备信息
[root@ctserver7 ~]# mkfs.ext4 /dev/sdb1 [root@ctserver7 ~]# mke2fs -t ext4 [root@ctserver7 ~]# fdisk -l /dev/sdb Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xdda9498c Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux
查看分区UUID及类型
[root@ctserver7 ~]# blkid /dev/sdb1 /dev/sdb1: UUID="f5749efa-ca95-44dd-ae6e-8b04cbc23920" TYPE="ext4"
创建自动挂载目录和设置自动挂载(使用默认挂载选型,不备份不自检)
[root@ctserver7 ~]# mkdir /data [root@ctserver7 ~]# echo "UUID=f5749efa-ca95-44dd-ae6e-8b04cbc23920 /data ext4 defaults 0 0" >>/etc/fstab
设置完成后使用mount -a命令更新挂载信息并且查看挂载情况,确认可以挂载后重启系统,以后再次启动时,/dev/sdb1分区就会自动挂载到/data目录
[root@ctserver7 ~]# mount -a [root@ctserver7 ~]# mount | tail -1 /dev/sdb1 on /data type ext4 (rw,relatime,seclabel,data=ordered)
2.1)过滤netstat -ant结果中,以LISTEN后跟0个或多个空白字符的行
[root@ctserver7 ~]# netstat -ant | egrep "LISTEN[[:space:]]*$" tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp6 0 0 ::1:25 :::* LISTEN tcp6 0 0 :::22 :::* LISTEN
2.2)创建nginx、zabbix、tomcat、nologin以及hadoop,nologin用户默认shell为/sbin/nologin,查找出/etc/passwd文件中其用户名与shell相同的
[root@ctserver7 ~]# useradd nginx ... [root@ctserver7 ~]# useradd nologin -s /sbin/nologin [root@ctserver7 ~]# egrep "^([[:alnum:]]+\b).*\1$" /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt nologin:x:1001:1001::/home/nologin:/sbin/nologin
2.3)找出/etc/rc.d/init.d/functions文件中某单词(单词中间可以存在下划线)后面跟着一组小括号的行
[root@ctserver7 ~]# egrep "[_[:alpha:]]+\(\)" /etc/rc.d/init.d/functions checkpid() { __pids_var_run() { __pids_pidof() { daemon() { ... is_ignored_file() { is_true() { is_false() { apply_sysctl() {
2.4)使用echo输出一个路径,而后egrep找出其路径基名;进一步的使用egrep取出其目录名(注意是目录名,而非目录路径)
[root@ctserver7 ~]# echo "/hello/world/linux" |egrep -o "[^/]+/?$" |cut -d"/" -f1 linux [root@ctserver7 ~]#
3.1)查找/usr目录下不属于root、bin或hadoop的所有文件
[root@ctserver7 ~]# find /usr -! \( -user root -o -user bin -o -user hadoop \) -ls 67623245 0 drwx------ 2 polkitd root 6 Jun 9 2014 /usr/share/polkit-1/rules.d
3.2)查找当前系统上没有属主或属组,且最近一周内曾被访问过的所有文件;
另外,需要查找/etc目录下大于20k且类型为普通文件的所有文件;
为了显示效果,在/learntest目录下用普通用户创建两个文件,并把其中一个的访问时间改到一周之前,然后删除其创建用户
[root@ctserver7 ~]# useradd usert [root@ctserver7 ~]# su -l usert [usert@ctserver7 ~]$ touch /learntest/match && touch -a -d 20151218 /learntest/nomatch [usert@ctserver7 ~]$ exit logout [root@ctserver7 ~]# userdel -r usert [root@ctserver7 ~]# stat /learntest/{match,nomatch} File: ‘/learntest/match’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd01h/64769dInode: 34314527 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1002/ UNKNOWN) Gid: ( 1002/ UNKNOWN) Context: unconfined_u:object_r:default_t:s0 Access: 2015-12-28 02:38:22.255070654 -0500 Modify: 2015-12-28 02:38:22.255070654 -0500 Change: 2015-12-28 02:38:22.255070654 -0500 Birth: - File: ‘/learntest/nomatch’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd01h/64769dInode: 34314529 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1002/ UNKNOWN) Gid: ( 1002/ UNKNOWN) Context: unconfined_u:object_r:default_t:s0 Access: 2015-12-18 00:00:00.000000000 -0500 Modify: 2015-12-28 02:38:22.260070655 -0500 Change: 2015-12-28 02:38:22.260070655 -0500 Birth: - [root@ctserver7 ~]# find / -type f \( -nouser -o -nogroup \) -a -atime -7 find: ‘/proc/2618/task/2618/fdinfo/6’: No such file or directory find: ‘/proc/2618/fdinfo/6’: No such file or directory 34314527 0 -rw-rw-r-- 1 1002 1002 0 Dec 28 02:38 /learntest/match [root@ctserver7 ~]# find /etc -type f -size +20k /etc/pki/ca-trust/extracted/java/cacerts /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem /etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem ... /etc/selinux/targeted/modules/active/modules/xserver.pp /etc/selinux/targeted/policy/policy.29 /etc/ssh/moduli /etc/lvm/lvm.conf
4.1)创建目录/test/data,让某组内普通用户对其有写权限,且创建的所有文件的属组为目录所属的组;此外,每个用户仅能删除自己的文件。
[root@ctserver7 ~]# grep "usgroup" /etc/group usgroup::1006:usert,userf [root@ctserver7 ~]# mkdir /test/data && chown :usgroup /test/data && chmod g+ws o+t /test/data [root@ctserver7 ~]# ls -ld /test/data drwxrwsr-t. 2 root usgroup 4096 Dec 21 17:56 /test/data [root@ctserver7 ~]# su -l usert [usert@ctserver7 ~]$ touch /test/data/tfile && mkdir /test/data/tdir [usert@ctserver7 ~]$ exit logout [root@ctserver7 ~]# su -l userf [userf@ctserver7 ~]$ touch /test/data/ffile && mkdir /test/data/fdir [userf@ctserver7 ~]$ ll /test/data/ total 8 drwxrwsr-x. 2 userf usgroup 4096 Dec 21 18:08 fdir -rw-rw-r--. 1 userf usgroup 0 Dec 21 18:08 ffile drwxrwsr-x. 2 usert usgroup 4096 Dec 21 18:07 tdir -rw-rw-r--. 1 usert usgroup 0 Dec 21 18:07 tfile [userf@ctserver7 ~]$ rm /test/data/ffile [userf@ctserver7 ~]$ rm /test/data/tfile rm: cannot remove `/test/data/tfile': Operation not permitted [userf@ctserver7 ~]$ ll /test/data/ total 8 drwxrwsr-x. 2 userf usgroup 4096 Dec 21 18:08 fdir drwxrwsr-x. 2 usert usgroup 4096 Dec 21 18:07 tdir -rw-rw-r--. 1 usert usgroup 0 Dec 21 18:07 tfile
本文出自 “Whang” 博客,转载请与作者联系!