1. 程式人生 > >配置並驗證Split分離解析

配置並驗證Split分離解析

domain serial 顯示 信息 sem except spl 直接 驗證

Top
NSD SERVICES DAY03

案例1:配置並驗證Split分離解析
案例2:查看進程信息
案例3:進程調度及終止
案例4:系統日誌分析

1 案例1:配置並驗證Split分離解析
1.1 問題

本例要求配置一臺智能DNS服務器,針對同一個FQDN,當不同的客戶機來查詢時能夠給出不同的答案。需要完成下列任務:

從主機192.168.4.207查詢時,結果為:www.tedu.cn ---> 192.168.4.100
從其他客戶端查詢時,www.tedu.cn ---> 1.2.3.4

1.2 方案

在配置DNS服務器時,通過view視圖設置來區分不同客戶機、不同地址庫:

view  "視圖1" {
    match-clients { 客戶機地址1; .. .. ; };          //匹配第1類客戶機地址
    zone "目標域名" IN {                              //同一個DNS區域
        type master;
        file "地址庫1";                             //第1份地址庫
    };
};
view  "視圖2" {
    match-clients { 客戶機地址2; .. .. ; };          //匹配第2類客戶機地址
    match-clients { any; };                          //匹配任意地址
    zone "目標域名" IN {                              //同一個DNS區域
        type master;
        file "地址庫2";                             //第2份地址庫
    };
};
.. ..
view  "視圖n" {
    match-clients { any; };                          //匹配任意地址
    zone "目標域名" IN {                              //同一個DNS區域
        type master;
        file "地址庫n";                             //第n份地址庫
    };
};

1.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:配置Split分離解析

1)為tedu.cn區域建立兩份解析記錄文件

第一份解析記錄文件提供給客戶機192.168.4.207、網段192.168.7.0/24,對應目標域名www.tedu.cn的A記錄地址為192.168.4.100。相關操作及配置如下:

[root@svr7 ~]# cd  /var/named/
[root@svr7 named]# cp  -p  tedu.cn.zone  tedu.cn.zone.lan
[root@svr7 named]# vim  tedu.cn.zone.lan
$TTL 1D
@   IN SOA  @ rname.invalid. (
                    0   ; serial
                    1D  ; refresh
                    1H  ; retry
                    1W  ; expire
                    3H )    ; minimum
@       NS  svr7.tedu.cn.
svr7    A   192.168.4.7
pc207   A   192.168.4.207
www     A   192.168.4.100

第二份解析記錄文件提供給其他客戶機,對應目標域名www.tedu.cn的A記錄地址為1.2.3.4。相關操作及配置如下:

[root@svr7 named]# cp  -p  tedu.cn.zone  tedu.cn.zone.other
[root@svr7 named]# vim  tedu.cn.zone.other
$TTL 1D
@   IN SOA  @ rname.invalid. (
                    0   ; serial
                    1D  ; refresh
                    1H  ; retry
                    1W  ; expire
                    3H )    ; minimum
@       NS  svr7.tedu.cn.
svr7    A   192.168.4.7
pc207   A   192.168.4.207
www     A   1.2.3.4

2)修改named.conf配置文件,定義兩個view,分別調用不同解析記錄文件

[root@svr7 ~]# vim  /etc/named.conf
options {
        directory  "/var/named";
};
acl "mylan" {                                      //名為mylan的列表
        192.168.4.207; 192.168.7.0/24;
};
.. ..
view "mylan" {
    match-clients { mylan; };                      //檢查客戶機地址是否匹配此列表
    zone "tedu.cn" IN {
        type master;
        file "tedu.cn.zone.lan";
    };
};
view "other" {
    match-clients { any; };                          //匹配任意客戶機地址
    zone "tedu.cn" IN {
        type master;
        file "tedu.cn.zone.other";
    };
};

3)重啟named服務

[root@svr7 ~]# systemctl  restart  named

步驟二:測試分離解析效果

1)從mylan地址列表中的客戶機查詢

在客戶機192.168.4.207(或網段192.168.7.0/24內的任意客戶機)上查詢www.tedu.cn,結果是 192.168.4.100:

[root@pc207 ~]# host  www.tedu.cn 192.168.4.7
Using domain server:
Name: 192.168.4.7
Address: 192.168.4.7#53
Aliases: 
www.tedu.cn has address 192.168.4.100

2)從其他客戶機查詢

在DNS服務器本機或CentOS真機上查詢www.tedu.cn時,結果為 1.2.3.4:

[root@svr7 ~]# host  www.tedu.cn 192.168.4.7
Using domain server:
Name: 192.168.4.7
Address: 192.168.4.7#53
Aliases: 
www.tedu.cn has address 1.2.3.4

2 案例2:查看進程信息
2.1 問題

本例要求掌握查看進程信息的操作,使用必要的命令工具完成下列任務:

找出進程 gdm 的 PID 編號值
列出由進程 gdm 開始的子進程樹結構信息
找出進程 sshd 的父進程的 PID 編號/進程名稱
查看當前系統的CPU負載/進程總量信息

2.2 方案

查看進程的主要命令工具:

ps aux、ps –elf:查看進程靜態快照
top:查看進程動態排名
pstree:查看進程與進程之間的樹型關系結構
pgrep:根據指定的名稱或條件檢索進程

2.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:找出進程 gdm 的 PID 編號值

使用pgrep命令查詢指定名稱的進程,選項-l顯示PID號、-x精確匹配進程名:

[root@svr7 ~]# pgrep  -lx gdm
1584 gdm  

步驟二:列出由進程 gdm 開始的子進程樹結構信息

使用pstree命令,可以提供用戶名或PID值作為參數。通過前一步已知進程gdm的PID為1584,因此以下操作可列出進程gdm的進程樹結構:

[root@svr7 ~]# pstree  -p  1584
gdm(1584)-+-Xorg(1703)
          |-gdm-session-wor(2670)-+-gnome-session(2779)-+-gnom+
          |                       |                     |-gnom+
          |                       |                     |-{gno+
          |                       |                     |-{gno+
          |                       |                     `-{gno+
          |                       |-{gdm-session-wor}(2678)
          |                       `-{gdm-session-wor}(2682)
          |-{gdm}(1668)
          |-{gdm}(1671)
          `-{gdm}(1702)

步驟三:找出進程 sshd 的父進程的 PID 編號/進程名稱

要查看進程的父進程PID,可以使用ps –elf命令,簡單grep過濾即可。找到進程sshd所在行對應到的PPID值即為其父進程的PID編號。為了方便直觀查看,建議先列出ps表頭行,以分號隔開再執行過濾操作。

[root@svr7 ~]# ps  -elf  |  head  -1 ; ps  -elf  |  grep  sshd
F S UID         PID   PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root       1362      1  0  80   0 - 20636 poll_s Jan05 ?        00:00:00 /usr/sbin/sshd –D
.. ..                                 //可獲知進程sshd的父進程PID為1

然後再根據pstree –p的結果過濾,可獲知PID為1的進程名稱為systemd:

[root@svr7 ~]# pstree  -p  |  grep  ‘(1)‘
systemd(1)-+-ModemManager(995)-+-{ModemManager}(1018)

步驟四:查看當前系統的CPU負載/進程總量信息

使用top命令,直接看開頭部分即可;或者 top -n 次數:

[root@svr7 ~]# top
top - 15:45:25 up 23:55,  2 users,  load average: 0.02, 0.03, 0.05
Tasks: 485 total,   2 running, 483 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.7 us,  1.0 sy,  0.0 ni, 97.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1001332 total,    76120 free,   419028 used,   506184 buff/cache
KiB Swap:  2097148 total,  2096012 free,     1136 used.   372288 avail Mem
.. ..

觀察Tasks: 485 total部分,表示進程總量信息。

觀察load average: 0.02, 0.03, 0.05 部分,表示CPU處理器在最近1分鐘、5分鐘、15分鐘內的平均處理請求數(對於多核CPU,此數量應除以核心數)。

對於多核CPU主機,如果要分別顯示每顆CPU核心的占用情況,可以在top界面按數字鍵1進行切換:

[root@svr7 ~]# top
top - 15:47:45 up 23:57,  2 users,  load average: 0.02, 0.03, 0.05
Tasks: 485 total,   2 running, 269 sleeping,   0 stopped,   1 zombie
Cpu0  :  0.6%us,  7.8%sy,  0.0%ni, 91.6%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu1  :  0.7%us,  3.7%sy,  0.0%ni, 95.6%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu2  :  0.7%us,  1.7%sy,  0.0%ni, 97.6%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu3  :  0.3%us,  1.0%sy,  0.0%ni, 98.3%id,  0.3%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  16230564k total, 15716576k used,   513988k free,   326124k buffers
Swap:  8388604k total,   220656k used,  8167948k free, 11275304k cached
.. ..

3 案例3:進程調度及終止
3.1 問題

本例要求掌握調度及終止進程的操作,使用必要的工具完成下列任務:

運行“sleep 600”命令,再另開一個終端,查出sleep程序的PID並殺死
運行多個vim程序並都放入後臺,然後殺死所有vim進程
su切換為zhsan用戶,再另開一個終端,強制踢出zhsan用戶

3.2 方案

進程調度及終止的主要命令工具:

命令行 &:將命令行在後臺運行
Ctrl + z 組合鍵:掛起當前進程(暫停並轉入後臺)
jobs:列出當前用戶當前終端的後臺任務
bg 編號:啟動指定編號的後臺任務
fg 編號:將指定編號的後臺任務調入前臺運行
kill [-9] PID...:殺死指定PID值的進程
kill [-9] %n:殺死第n個後臺任務
killall [-9] 進程名...:殺死指定名稱的所有進程
pkill:根據指定的名稱或條件殺死進程

3.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:根據PID殺死進程

1)開啟sleep測試進程

[root@svr7 ~]# sleep 600
//.. .. 進入600秒等待狀態

2)找出進程sleep的PID

另開一個終端,ps aux並過濾進程信息(第2列為PID值):

[root@svr7 ~]# ps  aux  |  grep  sleep
root      32929  0.0  0.0   4312   360 pts/1    S+   17:25   0:00 sleep 600

3)殺死指定PID的進程

[root@svr7 ~]# kill  -9  32929

返回原終端會發現sleep進程已經被殺死:

[root@svr7 ~]# sleep 600
Killed

步驟二:根據進程名殺死多個進程

1)在後臺開啟多個vim進程

[root@svr7 ~]# vim  a.txt &
[1] 33152
[root@svr7 ~]# vim  b.txt &
[2] 33154
[1]+  已停止               vim a.txt
[root@svr7 ~]# vim  c.txt &
[3] 33155
[2]+  已停止               vim b.txt

2)確認vim進程信息

[root@svr7 ~]# jobs  -l
[1]  33152 停止 (tty 輸出)     vim a.txt
[2]- 33154 停止 (tty 輸出)     vim b.txt
[3]+ 33155 停止 (tty 輸出)     vim c.txt

3)強制殺死所有名為vim的進程

[root@svr7 ~]# killall  -9  vim
[1]   已殺死               vim a.txt
[2]-  已殺死               vim b.txt
[3]+  已殺死               vim c.txt

4)確認殺進程結果

[root@svr7 ~]# jobs  -l 
[root@svr7 ~]#

步驟三:殺死屬於指定用戶的所有進程

1)登入測試用戶zhsan

[root@svr7 ~]# useradd  zhsan
[root@svr7 ~]# su  -  zhsan
[zhsan@svr7 ~]$

2)另開一個終端,以root用戶登入,查找屬於用戶zhsan的進程

[root@svr7 ~]# pgrep  -u  zhsan
33219
[root@svr7 ~]# pstree  -up  33219                              //檢查進程樹
bash(33219,zhsan)

3)強制殺死屬於用戶zhsan的進程

[root@svr7 ~]# pkill  -9  -u  zhsan
[root@svr7 ~]#

4)返回原來用戶zhsan登錄的終端,確認已經被終止

[zhsan@svr7 ~]$ 已殺死
[root@svr7 ~]#

4 案例4:系統日誌分析
4.1 問題

本例要求熟悉Linux系統中的常見日誌文件,使用必要的命令工具完成下列任務:

列出所有包含關鍵詞8909的系統日誌消息
查看啟動時識別的鼠標設備信息
列出最近2條成功/不成功的用戶登錄消息
列出最近10條重要程度在 ERR 及以上的日誌消息
列出所有與服務httpd相關的消息
列出前4個小時內新記錄的日誌

4.2 方案

常見的系統日誌及各自用途:

/var/log/messages,記錄內核消息、各種服務的公共消息
/var/log/dmesg,記錄系統啟動過程的各種消息
/var/log/cron,記錄與cron計劃任務相關的消息
/var/log/maillog,記錄郵件收發相關的消息
/var/log/secure,記錄與訪問限制相關的安全消息

日誌消息的優先級(高-->低):

EMERG(緊急):級別0,系統不可用的情況
ALERT(警報):級別1,必須馬上采取措施的情況
CRIT(嚴重):級別2,嚴重情形
ERR(錯誤):級別3,出現錯誤
WARNING(警告):級別4,值得警告的情形
NOTICE(註意):級別5,普通但值得引起註意的事件
INFO(信息):級別6,一般信息
DEBUG(調試):級別7,程序/服務調試消息

RHEL7提供的journalctl日誌工具的常見用法:

journalctl | grep 關鍵詞
journalctl -u 服務名 -p 優先級
journalctl -n 消息條數
journalctl --since="yyyy-mm-dd HH:MM:SS" --until="yyyy-mm-dd HH:MM:SS"

4.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:分析系統日誌及用戶日誌

1)列出所有包含關鍵詞8909的系統日誌消息

簡單模擬一個故障(SELinux阻止Web開放8909端口):

[root@svr7 ~]# vim  /etc/httpd/conf.d/8909.conf          //添加開8909端口配置
Listen 8909
[root@svr7 ~]# setenforce 1                             //開啟強制模式
[root@svr7 ~]# systemctl  restart  httpd                 //起服務失敗
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.

從日誌文件/var/log/messages中檢索信息:

[root@svr7 ~]# grep  8909  /var/log/messages 
Jan  6 17:53:48 svr7 setroubleshoot: SELinux is preventing /usr/sbin/httpd from name_bind access on the tcp_socket port 8909. For complete SELinux messages. run sealert -l 6d37b8f0-ab8a-4082-9295-c784f4f57190
Jan  6 17:53:48 svr7 python: SELinux is preventing /usr/sbin/httpd from name_bind access on the tcp_socket port 8909.#012#012*****  Plugin bind_ports (92.2 confidence) suggests   ************************#012#012If you want to allow /usr/sbin/httpd to bind to network port 8909#012Then you need to modify the port type.#012Do#012# semanage port -a -t PORT_TYPE -p tcp 8909#012    where PORT_TYPE is one of the following: http_cache_port_t, http_port_t, jboss_management_port_t, jboss_messaging_port_t, ntop_port_t, puppet_port_t.#012#012*****  Plugin catchall_boolean (7.83 confidence) suggests   ******************#012#012If you want to allow nis to enabled#012Then you must tell SELinux about this by enabling the ‘nis_enabled‘ boolean.#012#012Do#012setsebool -P nis_enabled 1#012#012*****  Plugin catchall (1.41 confidence) suggests   **************************#012#012If you believe that httpd should be allowed name_bind access on the port 8909 tcp_socket by default.#012Then you should report this as a bug.#012You can generate a local policy module to allow this access.#012Do#012allow this access for now by executing:#012# grep httpd /var/log/audit/audit.log | audit2allow -M mypol#012# semodule -i mypol.pp#012
.. ..

使用完畢記得刪除測試配置文件:

[root@svr7 ~]# rm  -rf  /etc/httpd/conf.d/8909.conf
[root@svr7 ~]# systemctl  restart  httpd

2)查看啟動時識別的鼠標設備信息

[root@svr7 ~]# dmesg  |  grep  -i  mouse
[    1.020385] mousedev: PS/2 mouse device common for all mice
[    1.249422] input: ImPS/2 Generic Wheel Mouse as /devices/platform/i8042/serio1/input/input2
[    2.279665] usb 2-1: Product: VMware Virtual USB Mouse
[    2.603999] input: VMware VMware Virtual USB Mouse as /devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-1/2-1:1.0/input/input3
[    2.604222] hid-generic 0003:0E0F:0003.0001: input,hidraw0: USB HID v1.10 Mouse [VMware VMware Virtual USB Mouse] on usb-0000:02:00.0-1/input0

3)列出最近2條成功/不成功的用戶登錄消息

查看成功登錄的事件消息:

[root@svr7 ~]# last  -2
zhsan    pts/2        192.168.4.207    Fri Jan  6 18:00 - 18:00  (00:00)    
root     pts/2        192.168.4.110    Fri Jan  6 17:26 - 17:59  (00:33)    
wtmp begins Thu Aug  4 00:10:16 2016

查看失敗登錄的事件消息:

[root@svr7 ~]# lastb  -2
anonymou ssh:notty    192.168.4.207    Fri Jan  6 18:00 - 18:00  (00:00)    
anonymou ssh:notty    192.168.4.207    Fri Jan  6 18:00 - 18:00  (00:00)    
btmp begins Fri Jan  6 18:00:34 2017

步驟二:使用journalctl日誌提取工具

1)列出最近10條重要程度在 ERR 及以上的日誌消息

[root@svr7 ~]# journalctl  -p err  -n  10
-- Logs begin at Thu 2017-01-05 15:50:08 CST, end at Fri 2017-01-06 18:01:01 CST. --
Jan 06 14:56:57 svr7 setroubleshoot[23702]: SELinux is preventing /usr/sbin/vsftpd from getattr access on the file /rhel7/repodata/repomd.xml. For complete SELinux mes
Jan 06 14:56:57 svr7 setroubleshoot[23702]: SELinux is preventing /usr/sbin/vsftpd from read access on the file repomd.xml. For complete SELinux messages. run sealert 
Jan 06 14:56:57 svr7 setroubleshoot[23702]: SELinux is preventing /usr/sbin/vsftpd from read access on the file repomd.xml. For complete SELinux messages. run sealert 
Jan 06 14:56:57 svr7 setroubleshoot[23702]: SELinux is preventing /usr/sbin/vsftpd from lock access on the file /rhel7/repodata/repomd.xml. For complete SELinux messag
Jan 06 17:53:48 svr7 setroubleshoot[33743]: Plugin Exception restorecon_source
Jan 06 17:53:48 svr7 setroubleshoot[33743]: SELinux is preventing /usr/sbin/httpd from name_bind access on the tcp_socket port 8909. For complete SELinux messages. run
Jan 06 17:53:53 svr7 setroubleshoot[33743]: SELinux is preventing /usr/sbin/httpd from name_connect access on the tcp_socket port 8909. For complete SELinux messages. 
Jan 06 17:53:54 svr7 systemd[1]: Failed to start The Apache HTTP Server.
.. ..
lines 1-11/11 (END)

2)列出所有與服務httpd相關的消息

[root@svr7 ~]# journalctl   -u  httpd
-- Logs begin at Thu 2017-01-05 15:50:08 CST, end at Fri 2017-01-06 18:01:01 CST. --
Jan 06 14:57:16 svr7 systemd[1]: Starting The Apache HTTP Server...
Jan 06 14:57:16 svr7 httpd[23812]: AH00557: httpd: apr_sockaddr_info_get() failed for svr7
Jan 06 14:57:16 svr7 httpd[23812]: AH00558: httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1. Set the ‘ServerName‘ directi
Jan 06 14:57:16 svr7 systemd[1]: Started The Apache HTTP Server.
Jan 06 17:53:44 svr7 systemd[1]: Stopping The Apache HTTP Server...
Jan 06 17:53:46 svr7 systemd[1]: Starting The Apache HTTP Server...
Jan 06 17:53:46 svr7 httpd[33741]: AH00557: httpd: apr_sockaddr_info_get() failed for svr7
.. ..

3)列出前4個小時內新記錄的日誌

根據當前日期時間往前推4個小時,確定--since起始和--until結束時刻:

[root@svr7 ~]# journalctl  --since  "2017-01-06 14:11"  --until  "2017-01-06 18:11"
-- Logs begin at Thu 2017-01-05 15:50:08 CST, end at Fri 2017-01-06 18:10:01 CST. --
Jan 06 14:20:01 svr7 systemd[1]: Started Session 160 of user root.
Jan 06 14:20:01 svr7 CROND[22869]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jan 06 14:20:01 svr7 systemd[1]: Starting Session 160 of user root.
Jan 06 14:30:01 svr7 systemd[1]: Started Session 161 of user root.
Jan 06 14:30:01 svr7 CROND[23028]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jan 06 14:31:39 svr7 systemd[1]: Starting Session 162 of user root.
Jan 06 14:32:17 svr7 sshd[23046]: pam_unix(sshd:session): session closed for user root
Jan 06 14:31:39 svr7 systemd[1]: Started Session 162 of user root.
Jan 06 14:31:39 svr7 sshd[23046]: pam_unix(sshd:session): session opened for user root by (uid=0)
Jan 06 14:31:39 svr7 systemd-logind[985]: New session 162 of user root.
.. .

配置並驗證Split分離解析