2018-1-31 8周3次課 rsync服務同步,系統日誌,screen工具
·啟動服務之前,先編輯文件 /etc/rsync.conf
·啟動服務:rsync --daemon (--config=文件,如果rsync.conf不是保存在默認目錄下的話,需要--config=文件來配置)
[root@localhost ~]# rsync --daemon [root@localhost ~]# ps aux |grep rsync##檢查rsync服務是否啟動 root 1242 0.0 0.0 114652 520 ? Ss 20:50 0:00 rsync --daemon root 1244 0.0 0.0 112676 984 pts/0 S+ 20:50 0:00 grep --color=auto rsync [root@localhost ~]# 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 916/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1025/master tcp 0 0 192.168.65.128:873 0.0.0.0:* LISTEN 1242/rsync tcp6 0 0 :::22 :::* LISTEN 916/sshd tcp6 0 0 ::1:25 :::* LISTEN 1025/master
切換到另一臺主機(192.168.65.129)進行操作:
[root@localhost ~]# rsync -avP /tmp/arsenal.txt 192.168.65.128::test/arsenal-2.txt rsync: failed to connect to 192.168.65.128 (192.168.65.128): No route to host (113) rsync error: error in socket IO (code 10) at clientserver.c(122) [sender=3.0.9] [root@localhost ~]# ping 192.168.65.128 PING 192.168.65.128 (192.168.65.128) 56(84) bytes of data. 64 bytes from 192.168.65.128: icmp_seq=1 ttl=64 time=0.323 ms 64 bytes from 192.168.65.128: icmp_seq=2 ttl=64 time=0.353 ms ^C --- 192.168.65.128 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 0.323/0.338/0.353/0.015 ms [root@localhost ~]# telnet 192.168.65.128 873 -bash: telnet: 未找到命令 [root@localhost ~]# yum install -y telnet (安裝過程省略) [root@localhost ~]# telnet 192.168.65.128 873 Trying 192.168.65.128... telnet: connect to address 192.168.65.128: No route to host [root@localhost ~]# systemctl stop firewalld ##同時關閉兩臺機器 firewalld / iptables 服務 [root@localhost ~]# 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@localhost ~]# telnet 192.168.65.128 873 Trying 192.168.65.128... Connected to 192.168.65.128. Escape character is '^]'. @RSYNCD: 30.0 ^] ## ctrl + ] (右括號),在quit退出 telnet> quit Connection closed. [root@localhost ~]# rsync -avP /tmp/arsenal.txt 192.168.65.128::test/arsenal-2.txt sending incremental file list arsenal.txt 979 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) sent 1054 bytes received 27 bytes 2162.00 bytes/sec total size is 979 speedup is 0.91
切換到(192.168.65.128)主機上:
[root@localhost ~]# ls /tmp/rsync arsenal-2.txt
(以上為主機2同步數據到到主機1)
從主機1同步數據到主機2:
[root@localhost ~]# rsync -avP 192.168.65.128::test/arsenal-2.txt /tmp/123.txt receiving incremental file list sent 26 bytes received 65 bytes 182.00 bytes/sec total size is 979 speedup is 10.76 [root@localhost ~]# ls /tmp/ 123.txt systemd-private-7453042b23f0400db631b7ac80236eff-vgauthd.service-x0NkYv arsenal.txt systemd-private-7453042b23f0400db631b7ac80236eff-vmtoolsd.service-IQiCtU systemd-private-7453042b23f0400db631b7ac80236eff-chronyd.service-dFmGx7
10.33 rsync通過服務同步(下)
rsyncd.conf配置文件詳解 :
·port:指定在哪個端口啟動rsyncd服務,默認是873端口。
如果端口不是873,而是8730,那麽同步時需要 --port 8730
rsync -avP --port 8730 192.168.65.128::test/ /tmp/test/
·log file:指定日誌文件。
·pid file:指定pid文件,這個文件的作用涉及服務的啟動、停止等進程管理操作。
·address:指定啟動rsyncd服務的IP。假如你的機器有多個IP,就可以指定由其中一個啟動rsyncd服務,如果不指定該參數,默認是在全部IP上啟動。
·[]:指定模塊名,裏面內容自定義。
·path:指定數據存放的路徑。
·use chroot true|false:表示在傳輸文件前首先chroot到path參數所指定的目錄下。這樣做的原因是實現額外的安全防護,但缺點是需要以roots權限,並且不能備份指向外部的符號連接所指向的目錄文件。默認情況下chroot值為true,如果你的數據當中有軟連接文件,建議設置成false。
默認情況下:user chroot = true
在主機1中創建鏈接文件:12.txt
[root@localhost ~]# cd /tmp/rsync/ [root@localhost rsync]# ln -s /etc/passwd ./12.txt [root@localhost rsync]# ll 總用量 4 lrwxrwxrwx. 1 root root 11 1月 27 21:33 12.txt -> /etc/passwd -rw-r--r--. 1 root root 979 1月 14 21:12 arsenal-2.txt 切換到主機2中: [root@localhost ~]# rsync -avP 192.168.65.128::test/ /tmp/test/ receiving incremental file list created directory /tmp/test ./ 12.txt -> /etc/passwd arsenal-2.txt 979 100% 956.05kB/s 0:00:00 (xfer#1, to-check=0/3) sent 51 bytes received 1138 bytes 2378.00 bytes/sec total size is 990 speedup is 0.83 [root@localhost ~]# ll /tmp/test 總用量 4 lrwxrwxrwx. 1 root root 11 1月 27 21:33 12.txt -> /etc/passwd -rw-r--r--. 1 root root 979 1月 14 21:12 arsenal-2.txt [root@localhost ~]# rsync -avLP 192.168.65.128::test/ /tmp/test/##加上 L receiving incremental file list symlink has no referent: "/12.txt" (in test) ./ sent 29 bytes received 133 bytes 324.00 bytes/sec total size is 979 speedup is 6.04 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1518) [generator=3.0.9] [root@localhost ~]# ll /tmp/test 總用量 4 -rw-r--r--. 1 0 root 979 1月 14 21:12 arsenal-2.txt ##軟連接文件沒有同步過來
(chroot = true,傳輸僅限於PATH目錄下文件可以同步)
在主機1上改變chroot參數,use chroot = false
在主機2上同步:
[root@localhost ~]# rsync -avLP 192.168.65.128::test/ /tmp/test/ receiving incremental file list ./ 12.txt 979 100% 956.05kB/s 0:00:00 (xfer#1, to-check=2/4) sent 67 bytes received 1924 bytes 1327.33 bytes/sec total size is 2714 speedup is 1.36 [root@localhost ~]# ll /tmp/test/ 總用量 8 -rw-r--r--. 1 0 root 979 1月 14 21:12 12.txt -rw-r--r--. 1 0 root 979 1月 14 21:12 arsenal-2.txt ·max connections:指定最大的連接數,默認是0,即沒有限制。
·read only ture|false:如果為true,則不能上傳到該模塊指定的路徑下。
(給服務端寫服務時,服務端定義為true,則不可寫)
·list:表示當用戶查詢該服務器上的可用模塊時,該模塊是否被列出,設定為true則列出,false則隱藏。
-true:
[root@localhost ~]# rsync 192.168.65.128::
test
-false:
[root@localhost ~]# rsync 192.168.65.128:: [root@localhost ~]#
·uid/gid:指定傳輸文件時以哪個用戶/組的身份傳輸。
·auth users:指定傳輸時要使用的用戶名。
·secrets file:指定密碼文件,該參數連同上面的參數如果不指定,則不使用密碼驗證。註意該密碼文件的權限一定要是600。格式:用戶名:密碼
當設置了auth users和secrets file後,客戶端連服務端也需要用用戶名密碼了,若想在命令行中帶上密碼,可以設定一個密碼文件
[root@localhost ~]# vim /etc/rsyncd.passwd test:1234 [root@localhost ~]# chmod 600 /etc/rsyncd.passwd [root@localhost ~]# rsync -avL [email protected]::test/ /tmp/test/ --password-file=/etc/rsyncd_passwd
(可以將密碼寫入到客戶端裏文件中,名稱可自定義,再用--passwordd-file來定義,權限600)
·hosts allow:表示被允許連接該模塊的主機,可以是IP或者網段,如果是多個,中間用空格隔開。
當設置了auth users和secrets file後,客戶端連服務端也需要用用戶名密碼了,若想在命令行中帶上密碼,可以設定一個密碼文件
一個ip可以直接寫,多個可以寫192.168.65.129 1.1.1.1 2.2.2.2 或者IP段 192.168.65.0/24
10.34 Linux系統日誌
系統日誌:/var/log/messages
如果命令沒有定義單獨的日誌,那麽日誌就記錄在這裏
日誌增長到一定級別,為防止文件過大,會自動切割(logrotate)
[root@localhost ~]# ls /var/log/messages* /var/log/messages /var/log/messages-20171231 /var/log/messages-20180108 /var/log/messages-20180114 /var/log/messages-20180121
/etc/logrotate.conf 日誌切割配置文件
參考:https://my.oschina.net/u/2000675/blog/908189
dmesg命令:列出系統硬件相關日誌
清空日誌:dmesg -c
/var/log/dmesg 日誌,和dmesg命令不一樣
last命令,調用的文件/var/log/wtmp,查看正確的登錄歷史
lastb命令查看登錄失敗的用戶,對應的文件時/var/log/btmp
安全日誌: /var/log/secure
10.35 screen工具
為了不讓一個任務意外中斷
nohup 命令 & 把任務丟到後臺
screen是一個虛擬終端
yum install -y screen
進入了虛擬終端:screen直接回車就
ctrl + a組合鍵再按d退出虛擬終端,但不是結束(screen後,在虛擬終端中操作)
查看虛擬終端列表:screen -ls
進入指定的終端:screen -r id
退出screen,exit退出
可以同時進入多個虛擬終端
[root@localhost ~]# screen [detached from 1485.pts-0.localhost] [root@localhost ~]# screen [detached from 1500.pts-0.localhost] [root@localhost ~]# screen [detached from 1515.pts-0.localhost] [root@localhost ~]# screen -ls There are screens on: 1515.pts-0.localhost(Detached) 1500.pts-0.localhost(Detached) 1485.pts-0.localhost(Detached) 3 Sockets in /var/run/screen/S-root.
時間長後,根據id就無法區分screen裏運行了什麽東西
·自定義名稱:screen -S test (screen進入虛擬終端時,不用screen,而是在之後加 -S 定義名稱)
[root@localhost ~]# screen -S test [detached from 1532.test] [root@localhost ~]# screen -ls There are screens on: 1532.test(Detached) 1515.pts-0.localhost(Detached) 1500.pts-0.localhost(Detached) 1485.pts-0.localhost(Detached) 4 Sockets in /var/run/screen/S-root.
進入指定虛擬終端:screen -r test(可以加自定義名稱或者id號)
2018-1-31 8周3次課 rsync服務同步,系統日誌,screen工具