天天看點

馬哥linux運維實戰練習案例(一)

1、建立一個10G的檔案系統,類型為ext4,要求開機可自動挂載至單獨資料/data目錄;

第一步:首先使用df -h 指令,檢視本機磁盤分區使用情況:

[root@centos7 ~]# df -h

檔案系統        容量  已用  可用 已用% 挂載點

/dev/sda3        18G 4.3G   14G   25% /

devtmpfs        482M    0  482M    0% /dev

tmpfs           490M   80K 490M    1% /dev/shm

tmpfs           490M 7.0M  483M    2% /run

tmpfs           490M     0 490M    0% /sys/fs/cgroup

/dev/sda1       497M 121M  377M   25% /boot

第二步:使用fdisk -l 指令檢視所有的挂載點

[root@centos7 ~]# fdisk -l /dev/sd[a-z]

磁盤 /dev/sda:21.5 GB, 21474836480 位元組,41943040 個扇區

Units = 扇區 of 1 * 512 = 512bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁盤标簽類型:dos

磁盤辨別符:0x000bd9cb

   裝置 Boot      Start         End      Blocks  Id  System

/dev/sda1  *        2048     1026047     512000   83  Linux

/dev/sda2         1026048     5220351    2097152   82  Linux swap / Solaris

/dev/sda3         5220352    41943039   18361344   83  Linux

磁盤 /dev/sdb:21.5 GB, 21474836480 位元組,41943040 個扇區

扇區大小(邏輯/實體):512 位元組 / 512 位元組 

I/O 大小(最小/最佳):512 位元組 / 512 位元組 

第三步:用fdisk指令對新的磁盤進行分區:

[root@centos7 ~]# fdisk /dev/sdb    ←fdisk後面不跟任何參數接裝置檔案進入磁盤分區操作

歡迎使用 fdisk (util-linux 2.23.2)。

更改将停留在記憶體中,直到您決定将更改寫入磁盤。

使用寫入指令前請三思。

Device does not contain a recognized partitiontable

使用磁盤辨別符 0xf245c75d 建立新的 DOS 磁盤标簽。

指令(輸入 m 擷取幫助):m  ←使用m來檢視fdisk所有操作指令

指令操作

  a   toggle a bootable flag

  b   edit bsd disklabel

  c   toggle the dos compatibilityflag

  d   delete a partition

  g   create a new empty GPTpartition table

  G   create an IRIX (SGI) partitiontable

  l   list known partition types

  m   print this menu

  n   add a new partition

………………………………………………

指令(輸入 m 擷取幫助):n  ←使用n來建立新的磁盤分區

Partition type:

  p   primary (0 primary, 0extended, 4 free)   ← p 主分區

  e   extended       ← e 擴充分區

Select (default p): p

分區号 (1-4,預設 1):1

起始扇區 (2048-41943039,預設為 2048):

将使用預設值 2048

Last 扇區, +扇區 or+size{K,M,G} (2048-41943039,預設為 41943039):+10G           ← 使用+給分區設定大小為10G   

分區 1 已設定為 Linux 類型,大小設為 10 GiB

指令(輸入 m 擷取幫助):P   ← 使用P來檢視已經建立好的分區

磁盤辨別符:0xf245c75d

   裝置 Boot      Start         End     Blocks   Id  System

/dev/sdb1            2048    20973567   10485760   83  Linux

指令(輸入 m 擷取幫助):W   ←使用W儲存執行的操作後退出,也可以使用q放棄目前所有操作

The partition table has been altered!

Calling ioctl() to re-read partition table.

正在同步磁盤。     ←表明磁盤分區建立完畢

第四步:再次使用fdisk -l 指令檢視挂載點

/dev/sda1  *        2048     1026047      512000  83  Linux

第五步:使用mke2fs指令對新的分區進行格式化,建立檔案系統,卷标等資訊:

[root@centos7 ~]# mke2fs -t ext4  /dev/sdb1

mke2fs 1.42.9 (28-Dec-2013)

檔案系統标簽=

OS type: Linux

塊大小=4096 (log=2)

分塊大小=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

655360 inodes, 2621440 blocks

131072 blocks (5.00%) reserved for thesuper user

第一個資料塊=0

Maximum filesystem blocks=2151677952

80 block groups

32768 blocks per group, 32768 fragments pergroup

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 filesystemaccounting information: 完成

第六步:建立新的挂載空目錄;

第七步:将檔案系統挂載到新建立的/data目錄下:

[root@centos7 ~]# mkdir /data    ←建立/data挂載目錄

[root@centos7 ~]# mount /dev/sdb1/data  ← 将檔案系統挂載到/data目錄下

[root@centos7 ~]# df -h    ← 檢視檔案系統使用情況

/dev/sdb1       9.8G  37M  9.2G    1% /data

第八步:使用mount指令檢視已挂載的所有裝置

[root@centos7 ~]# mount

proc on /proc type proc(rw,nosuid,nodev,noexec,relatime)

……

/dev/sda1 on /boot type xfs(rw,relatime,seclabel,attr2,inode64,noquota)

/dev/sdb1 on /data type ext4(rw,relatime,seclabel,data=ordered)

第九步:想要開機自動挂載裝置,必須編輯/etc/fstab檔案

[root@centos7 ~]# ls -l/dev/disk/by-uuid/   ←檢視UUID号

總用量 0

lrwxrwxrwx. 1 root root 10 12月 30 19:281120ff5e-8ec9-4a19-bd9f-b97732c49bb7 -> ../../sda3

lrwxrwxrwx. 1 root root  9 12月 30 19:282015-03-31-23-50-00-00 -> ../../sr0

lrwxrwxrwx. 1 root root 10 12月 30 19:283e0c7287-7a32-4fab-b6cc-5945e4fd083c -> ../../sda2

lrwxrwxrwx. 1 root root 10 12月 30 19:285d97147b-798d-4316-9381-acf7d8f8bdfb -> ../../sdb1

lrwxrwxrwx. 1 root root 10 12月 30 19:28fbac508c-2ca0-48d1-a74a-6ee4be99ca89 -> ../../sda1

馬哥linux運維實戰練習案例(一)

2、顯示`netstat -tan`指令結果中以‘LISTEN’後跟0個、1個或者多個空白字元結尾的行;

馬哥linux運維實戰練習案例(一)

3、添加使用者nginx、zabbix、tomcat、nologin以及hadoop使用者(nologin使用者的shell為/sbin/nologin);而後找出/etc/passwd檔案中使用者名與其shell名相同的行;

[root@centos7 ~]# useradd nginx

[root@centos7 ~]# useradd zabbix

[root@centos7 ~]# useradd tomcat

[root@centos7 ~]# useradd nologin

[root@centos7 ~]# useradd hadoop

[root@centos7 ~]# tail -l /etc/passwd

avahi:x:70:70:Avahi mDNS/DNS-SDStack:/var/run/avahi-daemon:/sbin/nologin

sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

lu:x:1000:1000:lu:/home/lu:/bin/bash

nginx:x:1001:1001::/home/nginx:/bin/bash

zabbix:x:1002:1002::/home/zabbix:/bin/bash

tomcat:x:1003:1003::/home/tomcat:/bin/bash

nologin:x:1004:1004::/home/nologin:/bin/bash

hadoop:x:1005:1005::/home/hadoop:/bin/bash

^:行首錨定;用于模式的最左側;

$:行尾錨定;用于模式的最右側;

\>:詞尾錨定;用于單詞模式的右側;

\1: 從左側起,第一個左括号以及與之比對右括号之間的模式所比對到的字元;

[root@centos7 ~]# grep"^\([[:alnum:]]\+\>\).*\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

4、找出/etc/rc.d/init.d/functions檔案中某單詞(單詞中間可以存在下劃線)後面跟着一組小括号的行;

# grep –o "\([[:alpha:]_*[:alpha:]]\)\+()"  etc/rc.d/init.d/functions

馬哥linux運維實戰練習案例(一)

5、使用echo輸出一個路徑,而後egrep找出其路徑基名;進一步的使用egrep取出其目錄名(注意是目錄名,而非目錄路徑);

[root@localhost ~]# echo"/etc/init.d/functions/" | egrep -o "[[[:alpha:]]*/?$" |cut-d/ -f1

functions 

6、查找/usr目錄下不屬于root、bin或hadoop的所有檔案;

# find /usr -not \( -user root -o -user bin-o -user hadoop \)

7、某天系統被入侵了,黑客在你系統下留下木馬檔案:現需要查找目前系統上沒有屬主或屬組,且最近一周内曾被通路過的所有檔案;

另外,需要查找/etc目錄下大于20k且類型為普通檔案的所有檔案;

[root@localhost ~]# man find

-nogroup      ←沒有屬組

              No group corresponds to file’snumeric group ID.

-nouser       ←沒有屬主

              No user corresponds to file’snumeric user ID.

-atime n       ←通路時間

              File was last accessed n*24 hoursago.  When find fig-

              ures out how many 24-hourperiods  ago  the file  was

              last  accessed, any fractional part is ignored, so to

              match -atime +1, a file has tohave been  accessed  at

              least two days ago.

-size n[cwbkMG]     ←檔案大小

             File  uses n  units of space.  The following suffixes

              can be used:

              ‘b’    for 512-byte blocks (this is the defaultif  no

                     suffix is used)

              ‘k’    for Kilobytes (units of 1024 bytes)

             ‘M’    for Megabytes (units of 1048576 bytes)

              ‘G’    for Gigabytes (units of 1073741824 bytes)

-type c    ←檔案類型

              File is of type c:

            f      regular file     ←正常類型

組合條件:   與:-a         或:-o     非:-not

[root@localhost ~]# find / -nouser -o-nogroup -atime -7

find: ‘/proc/40532/task/40532/fd/6’: 沒有那個檔案或目錄

find: ‘/proc/40532/task/40532/fdinfo/6’: 沒有那個檔案或目錄

find: ‘/proc/40532/fd/6’: 沒有那個檔案或目錄

find: ‘/proc/40532/fdinfo/6’: 沒有那個檔案或目錄

[root@localhost ~]# find /etc -size +20k-type f

/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/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem

/etc/pki/nssdb/cert8.db

/etc/ssh/moduli

…………………………………..

8、建立目錄/test/data,讓某組内普通使用者對其有寫權限,且建立的所有檔案的屬組為目錄所屬的組;此外,每個使用者僅能删除自己的檔案。 

[root@localhost ~]# groupadd works        ←添加works使用者組

[root@localhost ~]# chown works/test/data/     ←改變屬組

[root@centos6 ~]# chown lu /test/data/          ←改變屬主

[root@centos6 ~]# usermod -G works lu           ←改變使用者所在的組

[root@centos6 ~]# su - lu

[lu@centos6 ~]$ groups

lu works

[root@centos6 ~]# ls -ld /test/data/

drwxr-xr-x. 2 lu works 4096 Dec 31 16:54/test/data/

[root@centos6 ~]# chmod g+ws,o+t /test/data

drwxrwsr-t. 2 lu works 4096 Dec 31 16:54/test/data/

[lu@centos6 ~]$ cd /test/data/

[lu@centos6 data]$ touch a

[lu@centos6 data]$ rm a

[lu@centos6 data]$ touch a.txt

[lu@centos6 data]$ su -

Password:

[root@centos6 ~]# useradd -G worksmage      ←建立一個mage使用者并且登入,實驗一下能不能删除lu使用者建立的a.txt檔案

[root@centos6 ~]# su - mage

[mage@centos6 ~]$ groups

mage works

繼續閱讀