2018-06-15(平常運維四)

10.28 rsync工具介紹

rsync命令是一個遠程數據同步工具,可經過LAN/WAN快速同步多臺主機間的文件。rsync使用所謂的「rsync算法」來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不一樣部分,而不是每次都整份傳送,所以速度至關快.
命令格式和用法html

rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]host:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
rsync [OPTION]... [USER@]HOST::SRC DEST
rsync [OPTION]... SRC [USER@]HOST::DEST
rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
對應於以上六種命令格式,rsync有六種不一樣的工做模式:
一、拷貝本地文件。當SRC和DES路徑信息都不包含有單個冒號":"分隔符時就啓動這種工做模式。如:rsync -a /data /backup
二、使用一個遠程shell程序(如rsh、ssh)來實現將本地機器的內容拷貝到遠程機器。當DST路徑地址包含單個冒號":"分隔符時啓動該模式。如:rsync -avz *.c foo:src
三、使用一個遠程shell程序(如rsh、ssh)來實現將遠程機器的內容拷貝到本地機器。當SRC地址路徑包含單個冒號":"分隔符時啓動該模式。如:rsync -avz foo:src/bar /data
四、從遠程rsync服務器中拷貝文件到本地機。當SRC路徑信息包含"::"分隔符時啓動該模式。如:rsync -av root@192.168.66.130::www /databack
五、從本地機器拷貝文件到遠程rsync服務器中。當DST路徑信息包含"::"分隔符時啓動該模式。如:rsync -av /databack root@192.168.66.130::www
六、列遠程機的文件列表。這相似於rsync傳輸,不過只要在命令中省略掉本地機信息便可。如:rsync -v rsync://192.168.66.130/wwwlinux

10.29/10.30 rsync經常使用選項

經常使用選項算法

-a 包含-rtplgoD
-r 同步目錄時要加上,相似cp時的-r選項
-v 同步時顯示一些信息,讓咱們知道同步的過程
-l 保留軟鏈接
-L 加上該選項後,同步軟連接時會把源文件給同步
-p 保持文件的權限屬性
-o 保持文件的屬主
-g 保持文件的屬組
-D 保持設備文件信息
-t 保持文件的時間屬性
-P 顯示同步過程,好比速率,比-v更加詳細
-u 加上該選項後,若是DEST中的文件比SRC新,則不一樣步
-z 傳輸時壓縮
--delete 刪除DEST中SRC沒有的文件
--exclude 過濾指定文件,如--exclude 「logs」會把文件名包含logs的文件或者目錄過濾掉,不一樣步shell

經常使用選項的操做:vim

一、將root下的src目錄同步到tmp下,並更改命稱爲dest,保持文件屬性一致,在源目錄和目標目錄的後邊要加上/,其中src下ln.txt爲軟鏈接文件安全

[root@luo src]# ll /root/src/
總用量 0
-rw-r--r-- 1 root root  0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root  0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root  0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root  0 6月  18 11:15 bb.txt
lrwxrwxrwx 1 root root 11 6月  18 11:17 ln.txt -> /tmp/cc.txt
[root@luo src]# ll /tmp/
總用量 0
-rw-r--r-- 1 root root  0 6月  18 11:16 cc.txt
drwx------ 3 root root 17 6月  15 20:34 systemd-private-bd3913ea59c345b0b0a13da70a90c32a-chronyd.service-9Gwi3G
drwx------ 3 root root 17 6月  15 20:34 systemd-private-bd3913ea59c345b0b0a13da70a90c32a-vgauthd.service-YNBlPW
drwx------ 3 root root 17 6月  15 20:34 systemd-private-bd3913ea59c345b0b0a13da70a90c32a-vmtoolsd.service-RlrcGd
[root@luo src]# rsync -avP /root/src/ /tmp/dest/                   #加上-P選項後能夠看到詳細的傳輸速率和進度
sending incremental file list
created directory /tmp/dest
./
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=4/6)
2.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=3/6)
aa.txt
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=2/6)
bb.txt
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=1/6)
ln.txt -> /tmp/cc.txt
sent 317 bytes  received 130 bytes  894.00 bytes/sec
total size is 11  speedup is 0.02
[root@luo src]# ll /tmp/dest/
總用量 0
-rw-r--r-- 1 root root  0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root  0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root  0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root  0 6月  18 11:15 bb.txt
lrwxrwxrwx 1 root root 11 6月  18 11:17 ln.txt -> /tmp/cc.txt

能夠看到自動建立了目標目錄,而且軟連接原來指向哪同步後也是指向哪bash

二、加上-L選項以後,l選項會失效,會把軟連接的源文件同步服務器

[root@luo src]# rsync -avPL /root/src/ /tmp/dest/
sending incremental file list
ln.txt
              4 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/6)
sent 185 bytes  received 35 bytes  440.00 bytes/sec
total size is 4  speedup is 0.02
[root@luo src]# ll /tmp/dest/                            #能夠看到軟連接文件已經消失,雖然仍是ln.txt但實際爲源文件/tmp/cc.txt的內容
總用量 4
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt
-rw-r--r-- 1 root root 4 6月  18 11:30 ln.txt
[root@luo src]# cat /tmp/cc.txt 
111
[root@luo src]# cat /tmp/dest/ln.txt 
111

三、--delete 刪除DEST中SRC沒有的文件,若是要讓目標目錄和源目錄保持一致,可使用這個選項架構

[root@luo src]# ll /root/src/
總用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt
[root@luo src]# ll /tmp/dest/                          #能夠看到這裏多個3.txt
總用量 4
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:38 3.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt
[root@luo src]# rsync -avP --delete /root/src/ /tmp/dest/     #加上--delete選項後輸出刪除了3.txt
sending incremental file list
deleting 3.txt
./
sent 121 bytes  received 28 bytes  298.00 bytes/sec
total size is 0  speedup is 0.00
[root@luo src]# ll /tmp/dest/                             #能夠看到和源目錄/root/src/保持了一致
總用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt
[root@luo src]# ll /root/src/
總用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt

四、在拷貝時添加--exclude,能夠過濾指定的文件,支持多個--exclude命令共同使用dom

[root@luo src]# rm -rf /tmp/dest/*                       #先清空dest下的文件
[root@luo src]# ll /tmp/dest/
總用量 0
[root@luo src]# ll /root/src/
總用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt
[root@luo src]# rsync -avP --exclude "2.txt" --exclude "a*" /root/src/ /tmp/dest/           #這裏過濾了2.txt和a開頭的
sending incremental file list
./
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=1/3)
bb.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=0/3)
sent 166 bytes  received 57 bytes  446.00 bytes/sec
total size is 0  speedup is 0.00
[root@luo src]# ll /tmp/dest/                       #能夠看到只同步了1.txt和bb.txt,2.txt和a開頭的已通過濾了
總用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt

五、-u 加上該選項後,若是DEST中的文件比SRC新,則不一樣步

[root@luo src]# cat /root/src/1.txt                   源文件1.txt爲空
[root@luo src]# echo "1111" >/tmp/dest/1.txt      更新下目標文件1.txt內容
[root@luo src]# cat /tmp/dest/1.txt 
1111
不加-u選項同步,會把目標下的1.txt文件覆蓋
[root@luo src]# rsync -avP /root/src/ /tmp/dest/
sending incremental file list
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=3/5)
2.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=2/5)
aa.txt
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=1/5)
sent 235 bytes  received 73 bytes  616.00 bytes/sec
total size is 0  speedup is 0.00
[root@luo src]# cat /tmp/dest/1.txt        #能夠看到這裏也爲空
加上-u選項再測試
[root@luo src]# !ec
echo "1111" >/tmp/dest/1.txt                              #更新下目標的內容
[root@luo src]# rsync -avPu /root/src/ /tmp/dest/                #加上-u選項後,目標比源新,不一樣步
sending incremental file list

sent 114 bytes  received 12 bytes  252.00 bytes/sec
total size is 0  speedup is 0.00
[root@luo src]# cat /tmp/dest/1.txt                                 #能夠看到沒有同步
1111

10.31 rsync經過ssh同步

rsync不啓動服務能夠經過ssh同步,保證兩臺機器A和B能夠通訊,而且都要安裝rsync,這裏以192.168.66.130和192.168.66.131爲例:

[root@luo src]# hostname                主機名luo的ip爲66.130
luo
[root@localhost ~]# hostname              主機名爲localhost的66.131
localhost.localdomain

需求:把130機器/root/src/目錄同步到遠程131機器/root/下並更名爲src1

[root@luo src]# ll /root/src/
總用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt
[root@luo src]# rsync -avPz /root/src/ root@192.168.66.131:/root/src1/    #加上-z選項會使用壓縮,同步過去後再解壓
root@192.168.66.131's password: 
sending incremental file list
created directory /root/src1
./
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=3/5)
2.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=2/5)
aa.txt
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=1/5)
bb.txt
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=0/5)
sent 265 bytes  received 128 bytes  60.46 bytes/sec
total size is 0  speedup is 0.00
切換到131機器
[root@localhost ~]# ll /root/src1/                        #能夠看到已經同步完成
總用量 0
-rw-r--r-- 1 root root 0 6月  18 11:15 1.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 2.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 aa.txt
-rw-r--r-- 1 root root 0 6月  18 11:15 bb.txt

咱們也能夠去遠程主機拉文件,把131機器/root/src1/目錄拉到130機器的/tmp/src1/

[root@luo src]# rsync -avP 192.168.66.131:/root/src1/ /tmp/src1/        #不指定遠程主機的用戶,默認使用當前主機的用戶
root@192.168.66.131's password: 
receiving incremental file list
created directory /tmp/src1
./
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=3/5)
2.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=2/5)
aa.txt
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=1/5)
bb.txt
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=0/5)
sent 103 bytes  received 277 bytes  108.57 bytes/sec
total size is 0  speedup is 0.00

默認ssh爲22端口,若是遠程主機的ssh修改成其它的,例如:1122,能夠經過-e指定ssh端口號

rsync -av -e "ssh -p 1122" test1/ 192.168.66.131:/tmp/test1/

10.32/10.33 rsync經過服務同步

rsync還能夠經過c/s架構方式同步,即客戶端和服務端鏈接,在使用該方法進行同步以前須要先在服務的開啓相應服務同時要監聽一個端口(可自定義),默認是監聽873端口,開啓服務以前編輯rsync配置文件「/etc/rsyncd.conf」(將服務添加進去)而後使用命令:
「rsync –daemon(服務名稱)」啓動服務。
或者將配置文件寫到一個自定義文件中,使用命令「rsync –configfile」指定配置文件的路徑啓動服務。 配置完成後客戶端能夠經過指定端口與服務端進行通訊。

rsync --configfile=/usr/local/rsyncd.conf --daemon

服務端 192.168.66.130(主機名luo)
客戶端 192.168.66.131 (主機名localhost)
配置文件具體步驟:

1.在/etc/rsyncd.conf配置文件中添加:

[root@luo ~]# vi /etc/rsyncd.conf
port=873                               
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.66.130
[test]
path=/tmp/rsync
use chroot=true
max connections=4
read only=no
list=true
uid=root
gid=root
auth users=test
secrets file=/etc/rsyncd.passwd
hosts allow=192.168.66.131

說明:
配置文件分爲兩部分:全局配置部分和模塊配置部分,全局部分就是幾個參數而已,就像rsyncd.conf中port, log
file, pid file,
address這些都屬於全局配置,而[test]如下部分就是模塊配置部分了。一個配置文件中能夠有多個模塊,模塊名自定義,格式就像rsyncd.conf中的這樣。其實模塊中的一些參數例如use
chroot, max connections, udi, gid, auth users, secrets file以及hosts
allow均可以配置成全局的參數。
參數意義:

port 指定啓動端口,默認873;
log file 指定日誌文件;
pid file 指定pid文件,這個文件的做用涉及到服務的啓動以及中止等進程管理操做;
address 指定啓動rsyncd服務的IP,假如你的機器有多個IP,就能夠指定其中一個啓動rsyncd服務,默認是在所有IP上啓動;
[test] 指定模塊名,自定義;
path 指定數據存放的路徑;
use chroot true|false
默認是true,意思是在傳輸文件之前首先chroot到path參數所指定的目錄下。這樣作的緣由是實現額外的安全防禦,可是缺點是須要以roots權限,而且不能備份指向外部的符號鏈接所指向的目錄文件。默認狀況下chroot值爲true,若是你的數據當中有軟鏈接文件的話建議設置成false;
max connections 指定最大的鏈接數,默認是0即沒有限制;
read only ture|false 若是爲true則不能上傳到該模塊指定的路徑下;
list 指定當用戶查詢該服務器上的可用模塊時,該模塊是否被列出,設定爲true則列出,false則隱藏;
uid/gid 指定傳輸文件時,以哪一個用戶/組的身份傳輸;
auth users 指定傳輸時要使用的用戶名;
secrets file 指定密碼文件,格式爲用戶名:密碼該參數連同上面的參數若是不指定則不使用密碼驗證,注意該密碼文件的權限必定要是600;
hosts allow 指定被容許鏈接該模塊的主機,能夠是IP或者網段,若是是多個,之間用空格隔開;

2.啓動服務

[root@luo ~]# rsync --daemon

說明:若是想開機啓動,請把 rsync --daemon --configfile=/etc/rsyncd.conf 寫入到/etc/rc.d/rc.local文件
說明:若是啓動完此服務進程以後,想殺死此進程須要使用killall rsync(語法爲 killall 進程名)
3.檢測是否啓動服務

[root@luo src]# rsync --daemon
[root@luo src]# ps aux |grep 'rsync'
root       2461  0.0  0.0 114740   552 ?        Ss   12:41   0:00 rsync --daemon
root       2470  0.0  0.0 112720   972 pts/1    R+   12:41   0:00 grep --color=auto rsync

4.檢查監聽端口

[root@luo src]# netstat -lntp |grep rsync
tcp        0      0 192.168.66.130:873      0.0.0.0:*               LISTEN      2461/rsync
  1. 建立數據存放的路徑並賦予權限(也就是配置文件中的path,若是有這個文件就無需建立了)
    [root@luo src]# mkdir /tmp/rsync
    [root@luo src]# chmod 777 /tmp/rsync                   #爲了方便測試把目錄權限設爲777
    [root@luo src]# ll -d /tmp/rsync/
    drwxrwxrwx 2 root root 6 6月  18 12:43 /tmp/rsync/

    6.爲了避免影響實驗過程,還須要把兩臺機器的firewalld服務關閉,並設置成不開機啓動。

[root@luo src]# systemctl stop firewalld //兩臺機器都須要設置
7.在客戶機131上查看服務主機130的873端口是不是通的

[root@localhost ~]# telnet 192.168.66.130 873
Trying 192.168.66.130...
Connected to 192.168.66.130.
Escape character is '^]'.
@RSYNCD: 31.0
^] //按Ctrl+] 退出telnet

7.由於在服務機131的配置文件上指定了傳輸的用戶和密碼文件,修改其內容,並必定要把權限設置爲600

[root@luo src]# cat /etc/rsyncd.passwd 
test:test123                                                          #這個格式爲用戶名:密碼
[root@luo src]# cat /etc/rsyncd.passwd 
test:test123
[root@luo src]# chmod 600 !$
chmod 600 /etc/rsyncd.passwd
[root@luo src]# ll !$
ll /etc/rsyncd.passwd
-rw------- 1 root root 13 6月  18 14:06 /etc/rsyncd.passwd

8.準備完成後在localhost131客戶端上測試

[root@localhost ~]# rsync -avP /tmp/1.txt test@192.168.66.130::test/2.txt
Password:                                                                          #這裏輸入服務器/etc/rsyncd.passwd上定義的密碼
sending incremental file list
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)
sent 87 bytes  received 35 bytes  34.86 bytes/sec
total size is 0  speedup is 0.00
切換到服務器上查看
[root@luo src]# ll /tmp/rsync/2.txt
-rw-r--r-- 1 root root 0 6月  18 14:11 /tmp/rsync/2.txt                       #能夠看到已經傳輸過來了

注意:
剛剛提到配置文件/etc/rsyncd.conf中有一個選項叫作 「use chroot」 默認爲true,若是是true,同步的文件中若是有軟鏈接,則會有問題。若是有軟連接須要設置成false便可。
修改完rsyncd.conf配置文件後,不須要重啓rsyncd服務,這是rsync的一個特定機制,配置文件時即時生效的,不用重啓服務,改動端口號要想生效要重啓服務。
前面都有輸入密碼,這樣一樣也不能寫入腳本中自動執行,其實這種方式也是能夠不用手動輸入密碼的,它有兩種實現方式。

第一種,指定密碼文件

在客戶端上,也就是localhost上,編輯一個密碼文件:

[root@localhost ~]# vi /usr/rsync.pass
[root@localhost ~]# cat /usr/rsync.pass 
test123
修改密碼文件的權限:
[root@localhost ~]# chmod 600 !$
chmod 600 /usr/rsync.pass

在同步的時候,指定一下密碼文件,就能夠省去輸入密碼的步驟了:

[root@localhost ~]# rsync -avP /tmp/1.txt test@192.168.66.130::test/3.txt --password-file=/usr/rsync.pass 
sending incremental file list
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)
sent 87 bytes  received 35 bytes  244.00 bytes/sec
total size is 0  speedup is 0.00

第二種:在rsync服務器端不指定用戶

在服務端130修改配置文件rsyncd.conf, 去掉關於認證帳戶的配置項(auth user 和 secrets file這兩行)

#auth users=test
#secrets file=/etc/rsyncd.passwd

而後咱們再到客戶端測試:

[root@localhost ~]# rsync -avP /tmp/1.txt 192.168.66.130::test/4.txt
sending incremental file list
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)
sent 87 bytes  received 35 bytes  244.00 bytes/sec
total size is 0  speedup is 0.00

注意,這裏不用再加test這個用戶了,默認是以當前用戶的身份拷貝的,如今已經不須要輸入密碼了。
九、更改端口873爲8730

1.編輯rsync配置文件(/etc/rsyncd.conf),將端口改成port=8730
2.重啓rsync服務
3.安裝killall工具:yum install -y psmisc
4.殺死全部rsync進程:

[root@luo ~]# killall rsync

5.啓動:

[root@luo ~]# rsync --daemon

6.檢測進程和端口:

[root@luo src]# ps aux |grep 'rsync'
root       3094  0.0  0.0 114740   556 ?        Ss   14:40   0:00 rsync --daemon
root       3103  0.0  0.0 112720   972 pts/1    R+   14:40   0:00 grep --color=auto rsync
[root@luo src]# netstat -lnp |grep rsync
tcp        0      0 192.168.66.130:8730     0.0.0.0:*               LISTEN      3094/rsync

能夠看到已經綁定了8730端口
在客戶機localhost上進行測試

root@localhost ~]# rsync -avP /tmp/1.txt 192.168.66.130::test/5.txt
rsync: failed to connect to 192.168.66.130 (192.168.66.130): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2]

說明: 由於綁定的是8730的端口,因此此時同步數據會報錯!能夠經過--port參數指定對應的端口:

[root@localhost ~]# rsync -avP --port 8730 /tmp/1.txt 192.168.66.130::test/5.txt
sending incremental file list
1.txt
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)
sent 87 bytes  received 35 bytes  244.00 bytes/sec
total size is 0  speedup is 0.00

說明: 使--port選項指定端口號。這個要和經過ssh同步數據用-e指定端口區分開來

10.34 linux系統日誌

日誌主要的功能有:審計和監測,還能夠實時的監測系統狀態,監測和追蹤侵入者等等。
一、/var/log/message, 它是核心系統日誌文件,包含了系統啓動時的引導消息,以及系統運行時的其餘狀態消息

[root@localhost ~]# ls /var/log/messages
messages           messages-20180509  messages-20180513  messages-20180615  messages-20180618

能夠看到這些日誌後面都跟了一些日期,這就證實它是按日期切割的,那他是這樣作到的呢

logrotate,他就是用來切割日誌的,爲了防止日誌無限制的增長。它的配置文件/etc/logrotate.conf

二、dmesg命令
將系統裏面的硬件先關的日誌列出來,它保存在內存中,它並非一個文件。好比咱們的硬盤或者網卡出現問題,他都會記錄在這裏。咱們除了看/var/log/messages外,還要運行這個命令查看硬件的故障和錯誤。參數-c能夠清空內容,但重啓後又會自動添加內容。

三、/var/log/dmesg
這是一個日誌文件,他是記錄系統啓動的日誌,和dmesg命令沒有關係。

四、last命令
調用的文件/var/log/wtmp,用來查看正確的登錄歷史,是一個二進制文件,不能cat查看,只能用last來查看。

五、lastb命令
查看登陸失敗的用戶,對應的文件時/var/log/btmp /var/log/secure,記錄登陸失敗的日誌。是一個二進制文件,不能cat查看。

六、/var/log/secure安全日誌
會記錄登陸相關以及pam相關等等的日誌。若是有人暴力破解咱們設備,那麼也會記錄到這個文件中

10.35 screen工具

在工做中,咱們也許會有這樣的需求,要執行一個命令或者腳本,可是須要幾個小時甚至幾天。這就要考慮一個問題,就是中途斷網或出現其餘意外狀況,執行的任務中斷了怎麼辦?你能夠把命令或者腳本丟到後臺運行,不過也不保險。下面就介紹兩種方法來避免這樣的問題發生。
一、使用nohup

[root@luo ~]# vim /usr/local/sbin/sleep.sh
[root@luo ~]# cat !$
cat /usr/local/sbin/sleep.sh
#! /bin/bash
sleep 100
[root@luo ~]# nohup sh /usr/local/sbin/sleep.sh &
[1] 3703

直接加一個 ‘&’ 雖然丟到後臺了,可是當退出該終端時頗有可能這個腳本也會退出的,而在前面加上 nohup 就沒有問題了,nohup的做用就是不掛斷地運行命令。
二、screen工具的使用

簡單來講,screen是一個能夠在多個進程之間多路複用一個物理終端的窗口管理器。screen中有會話的概念,用戶能夠在一個screen會話中建立多個screen窗口,在每個screen窗口中就像操做一個真實的SSH鏈接窗口那樣。
一、打開一個會話,直接輸入screen命令而後回車,進入screen會話窗口。若是你沒有screen命令,就安裝下:

[root@luo ~]# screen
-bash: screen: 未找到命令
[root@luo ~]# yum install -y screen

而後輸入screen就進入到screen會話窗口,在會話窗口查看已經打開的會話:

[root@localhost ~]# screen -ls
There is a screen on:
    1396.pts-0.localhost    (Detached)
1 Socket in /var/run/screen/S-root.

先按Ctrl +a 再按d退出該screen會話,只是退出,並無結束。結束的話輸入Ctrl +d 或者輸入exit。
退出後還想再次登陸某個screen會話,使用sreen -r [screen 編號],這個編號就是上例中那個1396。當只有一個screen會話時,後面的編號是能夠省略的。當你有某個須要長時間運行的命令或者腳本時就打開一個screen會話,而後運行該任務。按ctrl +a 再按d退出會話,不影響終端窗口上的任何操做。

擴展:

  1. Linux日誌文件總管logrotate http://linux.cn/article-4126-1.html
  2. xargs用法詳解 http://blog.csdn.net/zhangfn2011/article/details/6776925
相關文章
相關標籤/搜索