1. 程式人生 > >rsync服務同步、系統日誌及screen工具介紹

rsync服務同步、系統日誌及screen工具介紹

oot 輪詢 系統錯誤 files fast 管理器 ets red hat map

1. rsync通過服務同步

在遠程主機上建立一個rsync的服務器,在服務器上配置好rsync的各種應用,然後將本機作為rsync的一個客戶端連接遠程的rsync服務器。


實驗測試:

  1. 找兩臺主機,一個IP是172.16.111.100,另一個是172.16.111.110。

  2. 在100主機上建立並配置rsync的配置文件/etc/rsyncd.conf,如下所示:


虛擬機1操作

# vi /etc/rsyncd.conf (把以下內容編輯到rsyncd.conf文件裏)

port=873

log file=/var/log/rsync.log

pid file=/var/run/rsyncd.pid

address=172.16.111.100

[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=172.16.111.110

# rsync --daemon 啟動服務,如果地址不在默認目錄下,啟動服務時需要添加地址

# ps aux |grep rsync 檢測服務是否啟動

root 4009 0.0 0.0 114656 524 ? Ss 18:18 0:00 rsync --daemon

root 4017 0.0 0.0 112676 976 pts/0 S+ 18:20 0:00 grep --color=auto rsync

# netstat -lntp 檢查監聽的端口

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 771/sshd

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1019/master

tcp 0 0 172.16.111.100:873 0.0.0.0:* LISTEN 4009/rsync

tcp6 0 0 :::22 :::* LISTEN 771/sshd

tcp6 0 0 ::1:25 :::* LISTEN 1019/master


虛擬機2操作

# telnet 172.16.111.110 873 檢測端口是否通信

# iptables -nvL 查看iptables

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

10951 16M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED

4 286 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0

15 868 INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0

15 868 INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0

15 868 INPUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0

0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID

0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

# systemctl stop firewalld 關閉firewalld服務

# iptables -nvL 查看服務已關閉

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination


Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination


Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

# rsync -avP /tmp/ccj.txt 172.16.111.100::test/ccj-02.txt 同步文件並更改名稱,如果這裏需要輸入密碼,可以在虛擬機1裏把配置文件密碼註釋掉

sending incremental file list

aming.txt

1386 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)


sent 1459 bytes received 27 bytes 2972.00 bytes/sec

total size is 1386 speedup is 0.93


虛擬機1操作,驗證文件同步成功。

# ls /tmp/rsync/

ccj-02.txt


上面的 port,log file,pid file,address都屬於全局配置。[]內的test是模塊名,以下屬於模塊配置。一個配置文件可以有多個不同模塊。各個參數含義如下:

port:指定在哪個端口啟動rsyncd服務,默認是873端口。

log file:指定日誌文件。

pid file:指定pid文件,這個文件的作用涉及服務的啟動、停止等進程管理操作。

address:指定啟動rsyncd服務的IP。假如你的機器有多個IP,就可以指定由其中一個啟動rsyncd服務,如果不指定該參數,默認是在全部IP上啟動。

[]:指定模塊名,裏面內容自定義。

path:指定數據存放的路徑。

use chroot true|false:表示在傳輸文件前首先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或者網段,如果是多個,中間用空格隔開。



舉例說明:當設置了auth users和secrets file後,客戶端連服務端也需要用用戶名密碼了,若想在命令行中帶上密碼,可以設定一個密碼文件

rsync -avL [email protected]::test/test1/ /tmp/test8/ --password-file=/etc/pass,其中/etc/pass內容就是一個密碼,權限要改為600



2. linux系統日誌

日誌記錄了系統每天發生的各種各樣的事情,比如監測系統狀況、排查系統故障等,你可以通過日誌來檢查錯誤發生的原因,或者受到攻擊時攻擊留下的痕跡。日誌的主要功能是審計和監測,還可以實時的監測系統狀態,監測和追蹤侵入者等。


/var/log/messages 是核心的系統日誌,包含了系統啟動時的引導消息,以及系統運行時的其他狀態信息,I/0錯誤、網絡錯誤和其他系統錯誤都會記錄到這個文件中來。通常情況下,/var/log/messages是做故障診斷時首先要查看的文件。


日誌切割配置文件

系統上有很多服務,如果都把服務日誌放到這個文件/var/log/messages裏那豈不是會很大,所以系統有一個日誌輪詢的機制,每星期切換一個日誌,切換後的日誌名字類似於messages-20180131,會存放在/var/log/目錄下面,連同messages一共有5個這樣的日誌文件,這裏的20180131就是日期,它表示日誌切割的年月日。它的配置文件是/etc/logrotate.conf(如果沒有特殊要求,請不要修改這個配置文件),示例如下:

# cat /etc/logrotate.conf

# see "man logrotate" for details

# rotate log files weekly

weekly


# keep 4 weeks worth of backlogs

rotate 4


# create new (empty) log files after rotating old ones

create


# use date as a suffix of the rotated file

dateext


# uncomment this if you want your log files compressed

#compress


# RPM packages drop log rotation information into this directory

include /etc/logrotate.d


# no packages own wtmp and btmp -- we'll rotate them here

/var/log/wtmp {

monthly

create 0664 root utmp

minsize 1M

rotate 1

}


/var/log/btmp {

missingok

monthly

create 0600 root utmp

rotate 1

}


# system-specific logs may be also be configured here.

備註:/var/log/messages是由rsyslogd這個守護進程產生的,如果停止這個服務則系統不會產生/var/log/messages,所以這個服務不要停止。rsyslogd服務的配置文件為/etc/rsyslog,conf,這個文件定義了日誌的級別,若沒有特殊需求,這個配置文件是不需要修改的。



dmesg

dmesg 這個命令顯示系統的啟動信息,在開機時啟動,並存儲在內存中。如果某個硬件有問題(如網卡),可以在這裏看到,示例如下。


# dmesg |head

[ 0.000000] Initializing cgroup subsys cpuset

[ 0.000000] Initializing cgroup subsys cpu

[ 0.000000] Initializing cgroup subsys cpuacct

[ 0.000000] Linux version 3.10.0-514.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Tue Nov 22 16:42:41 UTC 2016

[ 0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-514.el7.x86_64 root=UUID=cec56a03-0f96-43bf-8516-0027999cbe76 ro crashkernel=auto rhgb quiet.UTF-8

[ 0.000000] Disabled fast string operations

[ 0.000000] e820: BIOS-provided physical RAM map:

[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009ebff] usable

[ 0.000000] BIOS-e820: [mem 0x000000000009ec00-0x000000000009ffff] reserved

[ 0.000000] BIOS-e820: [mem 0x00000000000dc000-0x00000000000fffff] reserved



安全日誌

last命令 用來查看登錄linux的系統信息,示例如下:


# last

root pts/0 172.16.111.1 Tue Dec 5 15:58 still logged in

root tty1 Tue Dec 5 15:57 still logged in

reboot system boot 3.10.0-514.el7.x Tue Dec 5 15:53 - 19:43 (1+03:50)

root tty1 Tue Dec 5 15:44 - 15:53 (00:09)

reboot system boot 3.10.0-514.el7.x Tue Dec 5 15:43 - 15:53 (00:09)

root tty1 Tue Dec 5 15:42 - 15:43 (00:00)

reboot system boot 3.10.0-514.el7.x Tue Dec 5 15:38 - 15:43 (00:05)

root tty1 Tue Dec 5 15:34 - 15:37 (00:03)

reboot system boot 3.10.0-514.el7.x Tue Dec 5 15:33 - 15:37 (00:04)

root tty1 Tue Dec 5 15:29 - 15:30 (00:01)

reboot system boot 3.10.0-514.el7.x Tue Dec 5 15:28 - 15:37 (00:09)

root tty1 Tue Dec 5 15:19 - 15:23 (00:03)

root pts/0 192.168.4.84 Thu Nov 30 14:00 - down (5+01:22)

reboot system boot 3.10.0-514.el7.x Thu Nov 30 14:00 - 15:23 (5+01:22)

reboot system boot 3.10.0-514.el7.x Thu Nov 30 13:56 - 13:58 (00:02)

root tty1 Thu Nov 30 13:40 - 13:55 (00:15)

reboot system boot 3.10.0-514.el7.x Thu Nov 30 13:30 - 13:55 (00:25)

root pts/0 192.168.4.84 Wed Nov 29 19:58 - crash (17:31)

root pts/0 192.168.4.84 Wed Nov 29 19:33 - 19:58 (00:24)

last命令實際讀取的是/var/log/wtmp文件,但這個文件不能直接用cat,vi,vim,head,tail查看。


/var/log/secure


/var/log/secure記錄的也是和登錄有關的信息,如ssh登錄失敗的信息在這裏都可以記錄到。


# ls /var/log/secure

/var/log/secure



3. screen工具

有時我們要執行一個命令或者腳本,需要幾小時甚至幾天,在這個過程中,如果中途斷網或出現其他意外情況怎麽辦?以下兩種方法就是來避免這狀況發生的。


nohup

首先寫一個sleep.sh腳本,然後把它放到後臺執行,示例如下:


# nohup /usr/local/sbin/sleep.sh &

上例中,直接在sleep.sh後面加&雖然可以在後臺運行,但是當退出該終端時,這個腳本很有可能也會退出,所以在前面加上nohup就沒有問題了,執行後在當前目錄下生成一個nohup的文件,它的作用就是防止進程意外中斷,並且會把輸出信息記錄到nohup文件中。



screen工具的使用

screen是一個虛擬終端,簡單來說,screen是一個可以在多個進程之間多路復用一個物理終端的窗口管理器,screen中有會話的概念,用戶可以在一個screen會話中創建多個screen會話窗口,在每一個screen窗口中就像操作一個真實的ssh連接窗口一樣。


操作示例:

首先screen安裝 yum install -y screen

# screen 打開一個會話

[detached from 4151.pts-0.gary-tao]

# screen

[detached from 4186.pts-0.gary-tao]

# screen

[detached from 4207.pts-0.gary-tao]

# screen -ls

There are screens on:

4207.pts-0.ccj (Detached)

4186.pts-0.ccj (Detached)

4151.pts-0.ccj (Detached)

3 Sockets in /var/run/screen/S-root.


# screen -r 4207 退出後想再次進入會話,加-r,4207表示會話窗口編號,如果只打開一個窗口,後面可以省略編號


解釋說明:退出一個會話按Ctrl+A鍵,再按d退出該screen會話(只是退出,並沒有結束,結束screen會議要按Ctrl+D鍵或輸入exit)。


備註:當你在某個需要長時間運行的命令或者腳本時,就打開一個screen會話,然後運行該任務,按Ctrl+A鍵再按d退出會話,這樣不影響終端窗口上的任何操作。


rsync服務同步、系統日誌及screen工具介紹