linux运维实战练习案例-2015年12月20日-12月31日(第一次)

1、创建一个10G的文件系统,类型为ext4,要求开机可自动挂载至单独数据/data目录;

//确定是MBR/GPT
[root@a ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    2M  0 part 
├─sda2            8:2    0    1G  0 part /boot
└─sda3            8:3    0 18.9G  0 part 
  ├─centos-root 253:0    0   10G  0 lvm  /
  ├─centos-swap 253:1    0    1G  0 lvm  [SWAP]
  └─centos-home 253:2    0  7.9G  0 lvm  /home
sr0              11:0    1 1024M  0 rom  
[root@a ~]# parted /dev/sda print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: pmbr_boot
Number  Start   End     Size    File system  Name  Flags
 1      1049kB  3146kB  2097kB                     bios_grub
 2      3146kB  1077MB  1074MB  xfs
 3      1077MB  21.3GB  20.3GB                     lvm
 //分割指定容量的磁盘
[root@a ~]# gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.6
Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): n
Partition number (4-128, default 4): 
First sector (34-41943006, default = 41662464) or {+-}size{KMGTP}: 
Last sector (41662464-41943006, default = 41943006) or {+-}size{KMGTP}: +10G 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'
Command (? for help): P
Disk /dev/sda: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 974D2867-3522-4343-84A3-A2B26850627E
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 180157 sectors (88.0 MiB)
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048            6143   2.0 MiB     EF02  
   2            6144         2103295   1024.0 MiB  0700  
   3         2103296        41662463   18.9 GiB    8E00  
   4        41662464        41764863   10 GiB    8300  Linux filesystem
//写入磁盘分割表
Command (? for help): W
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
//更新磁盘分割表
[root@a ~]# partprobe -s
/dev/sda: gpt partitions 1 2 3 4
[root@a ~]# lsblk /dev/sda
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    2M  0 part 
├─sda2            8:2    0    1G  0 part /boot
├─sda3            8:3    0 18.9G  0 part 
│ ├─centos-root 253:0    0   10G  0 lvm  /
│ ├─centos-swap 253:1    0    1G  0 lvm  [SWAP]
│ └─centos-home 253:2    0  7.9G  0 lvm  /home
└─sda4            8:4    0   10G   0 part 
//格式化为ext4
[root@a ~]# mkfs.ext4 /dev/sda4
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
12824 inodes, 51200 blocks
2560 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
7 block groups
8192 blocks per group, 8192 fragments per group
1832 inodes per group
Superblock backups stored on blocks: 
        8193, 24577, 40961
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
//挂载至/data
[hi@a ~]$ vi /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sun Jan 10 22:09:58 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=4aa9d018-6054-4ae1-bc34-b91d5a06e84c /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sda4               /data                   etx4    defaults        0 0


2、显示`netstat -tan`命令结果中以‘LISTEN’后跟0个、1个或者多个空白字符结尾的行;

//0个
[root@a ~]# netstat -tan | grep -E "LISTEN[^[:space:]]"
//1个或者多个
[root@a ~]# netstat -tan | grep -E "LISTEN[[:space:]]+"
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN

3、添加用户nginx、zabbix、tomcat、nologin以及hadoop用户(nologin用户的shell为/sbin/nologin);而后找出/etc/passwd文件中用户名与其shell名相同的行;

//添加用户
[root@a ~]# useradd nginx
[root@a ~]# useradd zabbix
[root@a ~]# useradd tomcat
[root@a ~]# useradd nologin -s /sbin/nologin
[root@a ~]# useradd hadoop
//找出/etc/passwd文件中用户名与其shell名相同的行
[root@a ~]# grep "^([[:alnum:]]\{1,\}):.*\l$" /etc/passwd


4、找出/etc/rc.d/init.d/functions文件中某单词(单词中间可以存在下划线)后面跟着一组小括号的行;

[root@a ~]# grep -n "[[:alpha:]]()" /etc/rc.d/init.d/functions


5、使用echo输出一个路径,而后egrep找出其路径基名;进一步的使用egrep取出其目录名(注意是目录名,而非目录路径);

[root@a ~]# echo "/home/hi" | grep -E -o "[^/]+/?$" | cut -d"/" -f1
[root@a ~]$ echo "/home/hi" | egrep -o "^/[[:alnum:]]+"


6、查找/usr目录下不属于root、bin或hadoop的所有文件;

[root@a hi]# find /usr -not -user root -a -not -user bin -a -not -user hadoop

7、某天系统被入侵了,黑客在你系统下留下木马文件:

现需要查找当前系统上没有属主或属组,且最近一周内曾被访问过的所有文件;

另外,需要查找/etc目录下大于20k且类型为普通文件的所有文件;

[root@a hi]# find /etc -mtime -7 -a -not -user root -a -not -user hadoop
[hi@a ~]$ find /etc -size +20k -type f


8、创建目录/test/data,让某组内普通用户对其有写权限,且创建的所有文件的属组为目录所属的组;此外,每个用户仅能删除自己的文件。

[root@a hi]# mkdir -p /test/data 
[root@a hi]# chown root.tomcat /test/data
[root@a hi]# usermod -g tomcat nginx
[root@a hi]# usermod -g tomcat hadoop
[root@a hi]# chmod -g+ws,o+t /test/data


你可能感兴趣的:(linux)