Linux基礎(day38)

10.32/10.33 rsync經過服務同步

rsync 經過服務的方式同步

  • 要編輯配置文件/etc/rsyncd.conf
  • 啓動服務rsync --daemon
  • 格式:rsync -av test1/ 192.168.202.130::module/dir/

rsync同步文件

  • rsync 經過服務的方式同步,首先須要開啓一個服務,是cs架構,客戶端和服務端
    • 服務端,開啓一個rsync服務,而且一個端口,默認是873——>(端口是能夠自定義的)
  • 格式:rsync -av test1/ 192.168.133.130::module/dir/
  1. 在啓動服務以前,首先要編輯配置文件,文件的默認地址是在 /etc/rsyncd.conf——>也能夠更改路徑,可是在更改路徑後,就須要在啓動服務的時候,去rsync --daemon//conf=後跟路徑
  2. 而後啓動服務rsync --daemon

從hf機器的文件傳輸到hf-02機器

  • rsyncd.conf樣例
port=873        //監聽端口默認爲873,也能夠是別的端口
log file=/var/log/rsync.log        //指定日誌
pid file=/var/run/rsyncd.pid        //指定pid
address=192.168.202.130        #能夠定義綁定的ip
[test]        #爲模塊名,自定義
path=/root/rsync         // 指定該模塊對應在哪一個目錄下
use chroot=true            //是否限定在該目錄下,默認爲true,當有軟鏈接時,須要改成fasle,若是爲true就限定爲模塊默認目錄
max connections=4        //指定最大能夠鏈接的客戶端數
read only=no        //是否爲只讀,若是是no的話,客服端推送給服務端的時候不成功,這時候要改爲yes
list=true        //是否能夠列出模塊名    rsync --port 8730   172.16.37.139::  若是爲yes的話會列出客戶端全部的模塊名字。  
uid=root            //以哪一個用戶的身份來傳輸
gid=root            //以哪一個組的身份來傳輸
auth users=test        //指定驗證用戶名,能夠不設置,不設置默認不用密碼,設置的話安全性更高點
secrets file=/etc/rsyncd.passwd        //指定密碼文件,若是設定驗證用戶,這一項必須設置,設定密碼權限爲400.
hosts allow=192.168.133.132 1.1.1.1 2.2.2.2  192.168.133.0/24        //設置能夠容許訪問的主機,能夠是網段,多個Ip地址用空格隔開
  1. 首先在虛擬機hf上打開/etc/rsyncd.conf文件
[root@hanfeng ~]# vim /etc/rsyncd.conf
  1. 而後將上述代碼複製到 /etc/rsyncd.conf 文件中——>在打開文件的文件時候,會看到全部文件都存在的,但被註釋掉的,咱們能夠另起一行,粘貼進去
port=873        #監聽端口默認爲873,也能夠是別的端口
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.202.130        
[test]
path=/root/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.202.132
  1. 啓動服務rsync --daemon
[root@hanfeng ~]# rsync --daemon
[root@hanfeng ~]#
  1. 在hf虛擬機上檢查服務是否已經啓動
[root@hanfeng ~]# ps aux |grep rsync
root      2473  0.0  0.0 114640   536 ?        Ss   21:21   0:00 rsync --daemon
root      2475  0.0  0.0 112656   988 pts/0    R+   21:21   0:00 grep --color=auto rsyn
[root@hanfeng ~]#
  1. 在hf虛擬機上檢查監聽的端口是否爲873
    • 會看到監聽的端口爲873
    • 這裏而且指定監聽的IP,如果不寫監聽的IP,就會綁定0.0.0.0,就是全部的網段,所有的IP
[root@hanfeng ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2033/master         
tcp        0      0 192.168.202.130:873     0.0.0.0:*               LISTEN      2473/rsync          
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1149/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2033/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1149/sshd           
[root@hanfeng ~]#
  1. 在hf虛擬機上檢查path指定的路徑
  • 這裏如果指定root,權限就會有可能很差把握,因此放在tmp目錄下
    • 修改/etc/rsyncd.conf目錄中的
      • 將path=/root/rsync改成path=/tmp/rsync
    • 並新建目錄mkdir=/tmp/rsync
[root@hanfeng ~]# vim /etc/rsyncd.conf

將path=/root/rsync改成path=/tmp/rsync

[root@hanfeng ~]# mkdir /tmp/rsync
[root@hanfeng ~]#
  1. 在hf虛擬機上設置權限——>這裏設置的777權限是爲了方便測試
[root@hanfeng ~]# chmod 777 /tmp/rsync
[root@hanfeng ~]#
  1. 在另外一臺虛擬機hf-02上,運行
  • rsync -avP /tmp/hanfeng.txt 192.168.202.130::test/hanfeng-02.txt
    • 192.168.202.130它是hf虛擬機的IP
    • ::後面跟模塊的名稱——>模塊名稱就是/etc/rsyncd.conf中設置的 test 模塊
      • 這裏模塊名稱表明的路徑就是/tmp/rsync
[root@hf-02 ~]# rsync -avP /tmp/hanfeng.txt 192.168.202.130::test/hanfeng-02.txt
rsync: failed to connect to 192.168.202.130 (192.168.202.130): No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(122) [sender=3.0.9]
  1. 在hf-02虛擬機上,會提示沒有路由到遠程機器上去,遇到這種問題,首先在hf-02虛擬機上檢查網絡連通性
[root@hf-02 ~]# ping 192.168.202.130
PING 192.168.202.130 (192.168.202.130) 56(84) bytes of data.
64 bytes from 192.168.202.130: icmp_seq=1 ttl=64 time=18.3 ms
64 bytes from 192.168.202.130: icmp_seq=2 ttl=64 time=0.335 ms
^C
--- 192.168.202.130 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.335/9.366/18.398/9.032 ms
[root@hf-02 ~]#
  1. 這時在hf-02虛擬機上,檢查是否爲端口的問題
  • telnet 命令,用於登陸遠程主機,對遠程主機進行管理。
  • 安裝telnet包——>yum install -y telnet
  • telnet 192.168.202.130 873 //檢查端口是否相通的命令
[root@hf-02 ~]# telnet 192.168.202.130 873
Trying 192.168.202.130...
telnet: connect to address 192.168.202.130: No route to host
[root@hf-02 ~]#
  1. 這時候會發現873端口是不通的
  2. 首先檢查是否爲iptables的問題
[root@hf-02 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 2278 5435K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    1    84 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    1    52 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
   18  3551 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 1727 packets, 116K bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@hf-02 ~]#
  1. 會看到是iptables的問題,咱們須要停掉firewalld 服務停掉
[root@hf-02 ~]# systemctl stop firewalld
[root@hf-02 ~]# 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         
[root@hf-02 ~]#
  1. 這時會發現端口通了
  • 若想退出,按ctrl+],而後在quit就退出了
[root@hf-02 ~]# telnet 192.168.202.130 873
Trying 192.168.202.130...
Connected to 192.168.202.130.
Escape character is '^]'.
@RSYNCD: 30.0
^]
telnet> quit
Connection closed.
[root@hf-02 ~]#
  1. 這時在hf-02虛擬機上,執行rsync -avP /tmp/hanfeng.txt 192.168.202.130::test/hanfeng-02.txt命令,會看到提示要輸出密碼——由於在/etc/rsyncd.conf文件中,有定義密碼
  2. 這時能夠在hf虛擬機中的/etc/rsyncd.conf文件中,註釋掉輸入密碼
#auth users=test
#secrets file=/etc/rsyncd.passwd
  1. 這時在hf-02虛擬機上執行 rsync -avP /tmp/hanfeng.txt 192.168.202.130::test/hanfeng-02.txt
  • 這時就不須要輸入密碼,就能夠傳輸文件了
[root@hf-02 ~]# rsync -avP /tmp/hanfeng.txt 192.168.202.130::test/hanfeng-02.txt
sending incremental file list
hanfeng.txt
          50 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 125 bytes  received 27 bytes  304.00 bytes/sec
total size is 50  speedup is 0.33
[root@hf-02 ~]#
  1. 在hf虛擬機中,檢查文件
[root@hanfeng ~]# ls /tmp/rsync
hanfeng-02.txt
[root@hanfeng ~]#
  1. 這時就表示實驗成功了

從hf-02機器的文件拉到hf機器

[root@hf-02 ~]# rsync -avP 192.168.202.130::test/hanfeng-02.txt /tmp123.txt
receiving incremental file list
hanfeng-02.txt
          50 100%   48.83kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 45 bytes  received 159 bytes  408.00 bytes/sec
total size is 50  speedup is 0.25
[root@hf-02 ~]#

rsyncd.conf配置文件詳解

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 test@192.168.133.130::test/test1/  /tmp/test8/ --password-file=/etc/pass 
 其中/etc/pass內容就是一個密碼,權限要改成600
  • 查看日誌文件命令cat /var/log/rsync.log
[root@hanfeng ~]# cat /var/log/rsync.log
2017/12/06 21:16:31 [2445] rsyncd version 3.0.9 starting, listening on port 873
2017/12/06 21:16:31 [2445] bind() failed: Cannot assign requested address (address-family 2)
2017/12/06 21:16:31 [2445] unable to bind any inbound sockets on port 873
2017/12/06 21:16:31 [2445] rsync error: error in socket IO (code 10) at socket.c(555) [Receiver=3.0.9]
2017/12/06 21:21:22 [2473] rsyncd version 3.0.9 starting, listening on port 873
2017/12/06 23:10:53 [2619] name lookup failed for 192.168.202.132: Name or service not known
  • 查看模塊名
[root@hanfeng ~]# rsync --port=873 192.168.202.130::
test           	
[root@hanfeng ~]#
  • 若將 list 改成 false ,則模塊名不會列出,會被隱藏
[root@hanfeng ~]# rsync --port=873 192.168.202.130::  	
[root@hanfeng ~]#

rsync傳輸時設置密碼

  • rsync傳輸時設置密碼html

    • auth users=test 設置用戶名爲test
    • secrets file=/etc/rsyncd.passwd 並在/etc/rsyncd.passwd中設置密碼文件
  • 在/etc/rsyncd.passwd文件中編輯node

    • 格式爲test:hanfeng,而後保存退出
  • 而後修改權限爲600linux

    • chmod 600 /etc/rsyncd.passwd
  • rsync -avP /tmp/test/ test@192.168.202.130::test/shell

    • 這時候就須要輸入用戶名了 test@
  • 在rsync傳輸文件的時候,在寫shell腳本輸入密碼很差,好比天天凌晨半夜更新數據庫文件啥的,那就會很麻煩數據庫

    • 這時候在hf-02客戶端也定義一個密碼文件
    • vim /etc/rsync_pass.txt
      • 格式:只寫一個密碼便可 hanfeng 並保存——>客戶端上只需寫一個密碼就行
      • 並修改權限爲600
        • chmod 600 /etc/rsync_pass.txt
      • rsync -avP /tmp/test/ --password-file=/etc/rsync_pass.txt test@192.168.202.130::test/
        • 這裏須要加上--password-file=/etc/rsync_pass.txt

hosts allow

  • hosts allow,用來定義你容許那些機器去作同步,容許哪個IP鏈接,如果多個IP,就用空格隔開,也能夠寫IP段192.168.133.0/24

10.34 linux系統日誌

linux系統日誌

  • /var/log/messages //是linux系統一個總的日誌——>除非某些服務,有定義單獨的日誌
  • /etc/logrotate.conf 日誌切割配置文件
  • 參考日誌文件文章
  • dmesg命令
  • /var/log/dmesg 日誌
  • last命令,調用的文件/var/log/wtmp
  • lastb命令查看登陸失敗的用戶,對應的文件時/var/log/btmp
  • /var/log/secure

系統日誌

  • /var/log/messages //是linux系統一個總的日誌——>除非某些服務,有定義單獨的日誌
    • 系統中存有一個日誌切割機制,日誌的滾動,在增加到必定級別了,就會自動切割
[root@hf-01 ~]# 
[root@hf-01 ~]# ls /var/log/messages
/var/log/messages
[root@hf-01 ~]# less !$
less /var/log/messages
[root@hf-01 ~]# du -sh !$
du -sh /var/log/messages
388K	/var/log/messages
  • 在查看日誌的時候,會發現日誌自動切割了
    • linux系統中有一個logrotate服務,會自動切割日誌,防止無限制的增長
[root@hf-01 ~]# ls /var/log/messages*
/var/log/messages           /var/log/messages-20171120  /var/log/messages-20171203
/var/log/messages-20171112  /var/log/messages-20171127
[root@hf-01 ~]#
  • 查看日誌文件cat /etc/logrotate.conf
    • 它會告訴你
[root@hf-01 ~]# cat /etc/logrotate.conf
# see "man logrotate" for details        //會告訴你查看logrotate的幫助文檔
# 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        //是否須要壓縮,壓縮成 .tar.gz 

# 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.
[root@hf-01 ~]#
  • 查看 /etc/logrotate.d/syslog 文件
    • 它會爲cron,maillog,messages,secure,spooler這幾個日誌進行切割
      • messages日誌是由 syslogd 服務決定的,因此 kill -HUP 就會從新加載這個日誌
    • 還有一個腳本,shell命令行,在把日誌切割後(挪走),更名字生成新的日誌
  • Linux系統有一個特色,一個服務寫一個文件的時候,並非按照文件名去寫的,而是根據inode來寫的
[root@hf-01 ~]# ls /etc/logrotate.d
ppp  samba  syslog  wpa_supplicant  yum
[root@hf-01 ~]# cat /etc/logrotate.d/syslog
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    sharedscripts
    postrotate
	/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}
[root@hf-01 ~]#

參考文件日誌文章

dmesg命令

  • dmesg命令,會把系統硬件相關的日誌列出來
    • 這個日誌是保存在內存中的,並非一個文件
  • 假如你的網卡有問題了,硬盤損壞了,都會記錄在這個日誌中
  • dmesg -c //清空當前日誌
    • 可是一重啓這個系統,又會生成這些日誌

/var/log/dmesg日誌文件

  • /var/log/dmesg //這是一個日誌文件
    • 這個日誌文件和 dmesg命令 沒有任何關聯
    • 它是系統啓動的一個日誌,記錄的信息

last命令

  • last命令,查看你正確的登陸歷史
    • 調用的文件/var/log/wtmp
    • 記錄是是誰,在哪裏,來源IP,時間,登陸的時長都會有記錄
  • /var/log/wtmp日誌是一個二進制文件,不能直接cat查看的,只能用last命令去查看
[root@hf-01 ~]# last
root     pts/0        192.168.74.1     Thu Dec  7 01:41   still logged in   
reboot   system boot  3.10.0-123.el7.x Thu Dec  7 01:36 - 05:38  (04:01)    
root     pts/1        192.168.74.1     Wed Dec  6 05:11 - 05:13  (00:01)    
root     pts/0        192.168.74.1     Wed Dec  6 01:27 - crash (1+00:09)   
reboot   system boot  3.10.0-123.el7.x Wed Dec  6 01:26 - 05:38 (1+04:11)   
root     pts/0        192.168.74.1     Tue Dec  5 01:38 - crash  (23:48)    
reboot   system boot  3.10.0-123.el7.x Tue Dec  5 01:37 - 05:38 (2+04:00)   
root     pts/0        192.168.74.1     Mon Dec  4 03:23 - down   (02:57)    
root     pts/0        192.168.74.1     Sun Dec  3 17:42 - 03:23  (09:40)    
等等等,只截取了一部分

[root@hf-01 ~]# ls /var/log/wtmp
/var/log/wtmp

lastb命令

  • lastb命令,查看登陸失敗的用戶
    • 對應的文件時/var/log/btmp 日誌
  • /var/log/btmp也是二進制文件,不能直接cat的
[root@hf-01 ~]# lastb

btmp begins Sat Dec  2 04:25:01 2017
[root@hf-01 ~]# ls /var/log/btmp
/var/log/btmp
[root@hf-01 ~]#

安全日誌

  • /var/log/secure
    • 好比登陸操做系統,驗證成功會在這裏記錄一個日誌,失敗也會去記錄
[root@hf-01 ~]# ls /var/log/secure
/var/log/secure
[root@hf-01 ~]#

10.35 screen工具

screen工具

  • 爲了避免讓一個任務意外中斷
  • nohup command &
  • screen是一個虛擬終端
  • yum install -y screen
  • screen直接回車就進入了虛擬終端
  • ctral a組合鍵再按d退出虛擬終端,但不是結束
  • screen -ls 查看虛擬終端列表
  • screen -r id 進入指定的終端
  • screen -S aming
  • screen -r aming

screen

  • screen,虛擬的一個屏幕,也能夠理解爲一個虛擬的終端vim

  • 需求,執行一個腳本,須要一天一晚上,並且腳本會輸出一些東西出來,這就意味着這個腳本不能中途斷開,保證腳本不中斷,有兩種方法:安全

    • 方法一:把這個任務丟到後臺去,而後加一個日誌的輸出
      • 命令nohup command &——>nohup 加執行命令 加日誌 再加一個&符號
      • 這時即便你的終端斷開,依舊會在後臺執行——>但雖然解決了任務中斷的問題,可是沒有辦法實時查看輸出的內容
    • 方法二:screen工具,能夠把要執行的命令,放到這個終端裏,而後在退出pts/0以前,能夠把screen丟到後臺去,隨時用, 隨時查看
  • 安裝screen包——>yum install -y screen服務器

    • 在安裝完成後,直接敲screen命令,回車,會進入到screen,進入到一個窗口,這個窗口就是一個虛擬終端
    • 在虛擬終端,執行vmstat 1命令,而後 ctrl+a鍵 同時按,隨後再按 d鍵 ,就把screen丟到後臺了
    • 執行screen ls 命令,列出當前全部的session
    • 執行screen -r 再加查看到的id 命令,再回到虛擬終端
    • 這時若不須要screen了,直接殺死,按 exit 便可
    • 再次 screen -ls 查看,會發現沒有screen
[root@hf-01 ~]# screen
在虛擬終端下執行了vmstat 1,按 ctrl+a鍵 同時按,隨後再按 d鍵 ,就把screen丟到後臺了

[detached from 2841.pts-0.hf-01]
[root@hf-01 ~]# screen -ls        //列出當前全部的session
There is a screen on:
	2841.pts-0.hf-01	(Detached)
1 Socket in /var/run/screen/S-root.

[root@hf-01 ~]# screen -r 2841    //從新回到虛擬終端——>若不須要screen了,直接殺死,按 exit 便可
[screen is terminating]
[root@hf-01 ~]# screen -ls        //再次查看,會發現沒有screen
No Sockets found in /var/run/screen/S-root.

[root@hf-01 ~]#

screen命令,多個虛擬終端同時運行

  • screen能夠執行多個虛擬終端,同時運行
    • 若想進入到其中一個,指定id便可
[root@hf-01 ~]# screen
[detached from 2880.pts-0.hf-01]
[root@hf-01 ~]# screen
[detached from 2903.pts-0.hf-01]
[root@hf-01 ~]# screen
[detached from 2926.pts-0.hf-01]
[root@hf-01 ~]# screen -ls
There are screens on:
	2926.pts-0.hf-01	(Detached)
	2903.pts-0.hf-01	(Detached)
	2880.pts-0.hf-01	(Detached)
3 Sockets in /var/run/screen/S-root.

[root@hf-01 ~]#
  • 可是時間久了,有可能會忘記某一個screen是運行的是什麼(由於名字是相同的)
    • 咱們能夠給screen做業的名稱
  • screen -S "test_screen" //指定screen做業的名稱
    • screen -r 後能夠加id號,能夠是screen做業的名稱

擴展

  1. Linux日誌文件總管logrotate
  2. xargs用法詳解
相關文章
相關標籤/搜索