Linux日常运维管理技巧(四)文件同步工具-rsync、Linux系统日志、dmesg命令、lastb命令查看登录失败的用户、screen工具虚拟屏幕
目錄
Linux文件同步工具-rsync
Linux系統(tǒng)日志
dmesg命令
lastb命令
screen工具
Linux文件同步工具-rsync
rsync命令是一個遠程數(shù)據(jù)同步工具,可通過LAN/WAN快速同步多臺主機間的文件。rsync使用所謂的“rsync算法”來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不同部分,而不是每次都整份傳送,因此速度相當快。 rsync是一個功能非常強大的工具,其命令也有很多功能特色選項,我們下面就對它的選項一一進行分析說明。
eg.
rsync -av /etc/passwd /tmp/1.txt ##本機同步:A目錄同步到B目錄,并改名為1.txt。##
rsync -av /tmp/1.txt 192.168.188.128:/tmp/2.txt ##遠程同步:本機的A同步到遠程的B,或相反##
rsync常用選項
-a , --archive 歸檔模式,表示以遞歸方式傳輸文件,并保持所有文件屬性,等于-rlptgoD-r , --recursive 對子目錄以遞歸模式處理,同步目錄時要加上,類似cp時的-r選項-v , --verbose 詳細模式輸出,同步時顯示一些信息,讓我們知道同步的過程-l , --links 保留軟鏈結-L , --copy-links 想對待常規(guī)文件一樣處理軟鏈結,加上該選項后,同步軟鏈接時會把源文件給同步-p , --perms 保持文件權限,保持文件的權限屬性-o , --owner 保持文件屬主信息,保持文件的屬主-g , --group 保持文件屬組信息-D , --devices 保持設備文件信息-t , --times 保持文件時間屬性--delete 刪除DEST中SRC沒有的文件--exclude 過濾指定文件,如--exclude “l(fā)ogs”會把文件名包含logs的文件或者目錄過濾掉,不同步-P 等同于 --partial,顯示同步過程,比如速率,比-v更加詳細-u , --update加上該選項后,如果DEST中的文件比SRC新(mtime要新),則不同步-z , --compress 對備份的文件在傳輸時進行壓縮處理實例
第一次用這個rsync命令前,先安裝包
[root@zyshanlinux-001 ~]# yum install -y rsync同步目錄(目錄后記得加/),并改名
[root@zyshanlinux-001 ~]# rsync -av /root/009/ /tmp/009_dest/ sending incremental file list created directory /tmp/009_dest ./ 004.txt 011.txt.bak test01 008/ 008/005.txt 010/?sent 382 bytes received 143 bytes 1,050.00 bytes/sec total size is 0 speedup is 0.00軟鏈接也同步
?
選項-L可以把軟鏈接指向的源文件同步過去,并且覆蓋了選項-l
?
選項--delete刪除與源目錄文件不一樣的目標目錄文件
[root@zyshanlinux-001 ~]# touch /tmp/009_dest/new.txt [root@zyshanlinux-001 ~]# ls /tmp/009_dest/ 004.txt 008 010 011.txt.bak new.txt test01 [root@zyshanlinux-001 ~]# rsync -av --delete /root/009/ /tmp/009_dest/ sending incremental file list deleting new.txt ./?sent 216 bytes received 32 bytes 496.00 bytes/sec total size is 0 speedup is 0.00 [root@zyshanlinux-001 ~]# ls /tmp/009_dest/ 004.txt 008 010 011.txt.bak test01選項--exclude過濾以txt結尾的文件,再同步
[root@zyshanlinux-001 ~]# ls /tmp/009_dest/ 004.txt 008 010 011.txt.bak test01 [root@zyshanlinux-001 ~]# rm -rf /tmp/009_dest/* [root@zyshanlinux-001 ~]# [root@zyshanlinux-001 ~]# rsync -avL --exclude "*.txt" /root/009/ /tmp/009_dest/ sending incremental file list ./ 011.txt.bak test01 008/ 010/?sent 253 bytes received 65 bytes 636.00 bytes/sec total size is 0 speedup is 0.00 [root@zyshanlinux-001 ~]# ls /tmp/009_dest/ 008 010 011.txt.bak test01? [root@zyshanlinux-001 ~]# rm -rf /tmp/009_dest/* [root@zyshanlinux-001 ~]# rsync -avL --exclude "*.txt" --exclude "test*" /root/009/ /tmp/009_dest/ sending incremental file list ./ 011.txt.bak 008/ 010/?sent 189 bytes received 46 bytes 470.00 bytes/sec total size is 0 speedup is 0.00選項-P 顯示同步過程,比如速率,比-v更加詳細,大文件非常有用可以看到傳輸過程
[root@zyshanlinux-001 ~]# !rm rm -rf /tmp/009_dest/* [root@zyshanlinux-001 ~]# rsync -avP /root/009/ /tmp/009_dest/ sending incremental file list ./ 004.txt ? ? ? ? ? ? 0 100% ? 0.00kB/s ? 0:00:00 (xfr#1, to-chk=5/7) 011.txt.bak ? ? ? ? ? ? 0 100% ? 0.00kB/s ? 0:00:00 (xfr#2, to-chk=4/7) test01 ? ? ? ? ? ? 0 100% ? 0.00kB/s ? 0:00:00 (xfr#3, to-chk=3/7) 008/ 008/005.txt ? ? ? ? ? ? 0 100% ? 0.00kB/s ? 0:00:00 (xfr#4, to-chk=0/7) 010/?sent 382 bytes received 107 bytes 978.00 bytes/sec total size is 0 speedup is 0.00選項-u 加上該選項后,如果DEST中的文件比SRC新,則不同步
[root@zyshanlinux-001 009_dest]# vim test01 [root@zyshanlinux-001 009_dest]# cat !$ cat test01 123 456 789 [root@zyshanlinux-001 009_dest]# rsync -avPu /root/009/ /tmp/009_dest/ sending incremental file list ./?sent 216 bytes received 21 bytes 474.00 bytes/sec total size is 0 speedup is 0.00 [root@zyshanlinux-001 009_dest]# cat test01 123 456 789推文件:把本地的文件同步到遠程目錄
[root@zyshanlinux-001 ~]# rsync -av /etc/passwd 192.168.106.130:/tmp/zyshan.txt root@192.168.106.130's password: sending incremental file list passwd?sent 1,404 bytes received 35 bytes 261.64 bytes/sec total size is 1,312 speedup is 0.91 ######################################################### [root@zyshanlinux-02 ~]# cat /tmp/zyshan.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin ...拉文件:把遠程目錄的文件同步到本地
[root@zyshanlinux-001 ~]# rsync -avP 192.168.106.130:/tmp/zyshan.txt /tmp/123.txt root@192.168.106.130's password: receiving incremental file list zyshan.txt ? ? ? ? 1,312 100% ? 1.25MB/s ? 0:00:00 (xfr#1, to-chk=0/1)?sent 43 bytes received 1,408 bytes 263.82 bytes/sec total size is 1,312 speedup is 0.90如果不是22端口,則選用-e選項,指定端口
[root@zyshanlinux-001 ~]# rsync -avP -e "ssh -p 22" /etc/passwd 192.168.106.130:/tmp/zyshan.txt root@192.168.106.130's password: sending incremental file list?sent 45 bytes received 12 bytes 1.48 bytes/sec total size is 1,312 speedup is 23.02直接連接遠程主機
[root@zyshanlinux-001 ~]# ssh -p 22 192.168.106.130 root@192.168.106.130's password: Last login: Mon Jun 18 10:17:02 2018 from 192.168.106.1 [root@zyshanlinux-02 ~]# exit 登出 Connection to 192.168.106.130 closed.?
rsync 通過服務的方式同步
要編輯配置文件/etc/rsyncd.conf
port=873log file=/var/log/rsync.logpid file=/var/run/rsyncd.pidaddress=192.168.106.128
[test]path=/tmp/rsyncuse chroot=truemax connections=4read only=no
list=trueuid=rootgid=rootauth users=testsecrets file=/etc/rsyncd.passwdhosts allow=192.168.106.130
rsyncd.conf配置文件詳解
?port:指定在哪個端口啟動rsyncd服務,默認是873端口。
log file:指定日志文件。
pid file:指定pid文件,這個文件的作用涉及服務的啟動、停止等進程管理操作。
address:指定啟動rsyncd服務的IP。假如你的機器有多個IP,就可以指定由其中一個啟動rsyncd服務,如果不指定該參數(shù),默認是在全部IP上啟動。
[]:指定模塊名,里面內容自定義。
path:指定數(shù)據(jù)存放的路徑。
use chroot true|false:表示在傳輸文件前首先chroot到path參數(shù)所指定的目錄下。這樣做的原因是實現(xiàn)額外的安全防護,但缺點是需要以roots權限,并且不能備份指向外部的符號連接所指向的目錄文件。默認情況下chroot值為true,如果你的數(shù)據(jù)當中有軟連接文件,阿銘建議你設置成false。
max connections:指定最大的連接數(shù),默認是0,即沒有限制。
read only ture|false:如果為true,則不能上傳到該模塊指定的路徑下。
list:表示當用戶查詢該服務器上的可用模塊時,該模塊是否被列出,設定為true則列出,false則隱藏。
uid/gid:指定傳輸文件時以哪個用戶/組的身份傳輸。 auth users:指定傳輸時要使用的用戶名。
secrets file:指定密碼文件,該參數(shù)連同上面的參數(shù)如果不指定,則不使用密碼驗證。注意該密碼文件的權限一定要是600。格式:用戶名:密碼
hosts allow:表示被允許連接該模塊的主機,可以是IP或者網(wǎng)段,如果是多個,中間用空格隔開。 當設置了auth users和secrets file后,客戶端連服務端也需要用用戶名密碼了,若想在命令行中帶上密碼,可以設定一個密碼文件 rsync -avL test@192.168.133.130::test/test1/ /tmp/test8/ --password-file=/etc/pass 其中/etc/pass內容就是一個密碼,權限要改為600
啟動服務rsync --daemon
格式:rsync -av test1/ 192.168.133.130::module/dir/
?
Linux系統(tǒng)日志
/var/log/messages 是linux系統(tǒng)的總日志
[root@zyshanlinux-001 ~]# ls /var/log/messages /var/log/messages [root@zyshanlinux-001 ~]# less !$ less /var/log/messages [root@zyshanlinux-001 ~]# du -sh !$ du -sh /var/log/messages 2.0M /var/log/messages/etc/logrotate.conf 日志切割配置文件
[root@zyshanlinux-001 ~]# ls /var/log/messages* /var/log/messages ? ? ? ? ? /var/log/messages-20180527 /var/log/messages-20180610 /var/log/messages-20180521 /var/log/messages-20180603 [root@zyshanlinux-001 ~]# logrotate^C [root@zyshanlinux-001 ~]# 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.指定切割日志,腳本定義切割日志后讓系統(tǒng)在新的日志上正常寫入
[root@zyshanlinux-001 ~]# cat !$ cat /etc/logrotate.d/syslog /var/log/cron /var/log/maillog /var/log/messages /var/log/secure /var/log/spooler { ? missingok ? sharedscripts ? postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true ? endscript }參考:https://my.oschina.net/u/2000675/blog/908189
?
dmesg命令
把系統(tǒng)硬件相關日志列出來,保存在內存中并不是一個文件
[root@zyshanlinux-001 ~]# dmesg ... [root@zyshanlinux-001 ~]# dmesg -c ##清除內存中的硬件日志,當重啟系統(tǒng)或出現(xiàn)問題硬件日志又會生成##/var/log/dmesg 系統(tǒng)啟動的日志,記錄的一些信息,和dmesg命令沒有關系的
[root@zyshanlinux-001 ~]# ls /var/log/dmesg /var/log/dmesglast命令 查看正確的登錄歷史,調用的文件/var/log/wtmp(二進制文件,不能用cat看)
[root@zyshanlinux-001 ~]# last root ? ? pts/0 ? ? ? 192.168.106.1 ? Mon Jun 18 08:19 ? still logged in ? reboot ? system boot 3.10.0-693.el7.x Mon Jun 18 08:18 - 11:46 (03:28) ? ? root ? ? tty1 ? ? ? ? ? ? ? ? ? ? ? ? Fri Jun 15 22:04 - 22:04 (00:00) ? ? root ? ? pts/0 ? ? ? 192.168.106.1 ? Fri Jun 15 20:12 - 22:03 (01:51) ? ? reboot ? system boot 3.10.0-693.el7.x Fri Jun 15 20:11 - 22:04 (01:52) [root@zyshanlinux-001 ~]# last root ? ? pts/0 ? ? ? 192.168.106.1 ? Mon Jun 18 08:19 ? still logged in ? root ? ? tty1 ? ? ? ? ? ? ? ? ? ? ? ? Mon May 7 04:37 - crash (-6:-24) ? reboot ? system boot 3.10.0-693.el7.x Mon May 7 04:36 - 19:57 (2+15:21) ? ?wtmp begins Mon May 7 04:36:08 2018?
lastb命令
查看登錄失敗的用戶,對應的文件時/var/log/btmp
[root@zyshanlinux-001 ~]# lastb root ? ? tty1 ? ? ? ? ? ? ? ? ? ? ? ? Sun Jun 10 11:47 - 11:47 (00:00) ? ?? btmp begins Sun Jun 10 11:47:04 2018 [root@zyshanlinux-001 ~]# ls /var/log/btmp /var/log/btmp/var/log/secure 安全日志
[root@zyshanlinux-001 ~]# ls /var/log/secure /var/log/secure [root@zyshanlinux-001 ~]# cat !$ cat /var/log/secure Jun 10 23:05:17 zyshanlinux-01 sshd[1250]: error: Received disconnect from 192.168.106.1 port 14605:0: Jun 10 23:05:17 zyshanlinux-01 sshd[1250]: Disconnected from 192.168.106.1 port 14605 Jun 10 23:05:17 zyshanlinux-01 sshd[1250]: pam_unix(sshd:session): session closed for user root Jun 10 23:05:32 zyshanlinux-01 polkitd[551]: Registered Authentication Agent for unix-process:2138:4212583 (system bus name :1.117 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) Jun 18 08:19:20 zyshanlinux-001 sshd[1369]: pam_unix(sshd:session): session opened for user root by (uid=0)?
screen工具
虛擬屏幕(虛擬終端)
遠程運行腳本輸入命令,斷網(wǎng)了就會令命令失敗;
解決辦法有2種。
1、后臺運行,輸出日志,缺點:不能實時看到運行的情況
為了不讓一個任務意外中斷 命令:nohup command &
2、screen是一個虛擬終端
[root@zyshanlinux-001 ~]# yum install -y screenscreen的操作
[root@zyshanlinux-001 ~]# screen [detached from 2763.pts-0.zyshanlinux-001] ##Ctrl+a+d把虛擬終端放到后臺## [root@zyshanlinux-001 ~]# screen -ls ##看看在后臺運行的所有終端## There is a screen on: 2763.pts-0.zyshanlinux-001 (Detached) 1 Socket in /var/run/screen/S-root.? [root@zyshanlinux-001 ~]# screen -r 2763 ##打開指定id的虛擬終端## [screen is terminating] [root@zyshanlinux-001 ~]# screen -ls ##結束虛擬終端,exit就登出虛擬終端## No Sockets found in /var/run/screen/S-root.? [root@zyshanlinux-001 ~]#給screen定義名字,方便數(shù)量多了知道哪個screen運行什么東西
[root@zyshanlinux-001 ~]# screen -ls There are screens on: 2862.pts-0.zyshanlinux-001 (Detached) 2847.pts-0.zyshanlinux-001 (Detached) 2 Sockets in /var/run/screen/S-root.? [root@zyshanlinux-001 ~]# screen -S "test_screen" ##選項-S啟動一個自定義名字的screen## [detached from 2903.test_screen] [root@zyshanlinux-001 ~]# screen -ls There are screens on: 2903.test_screen (Detached) 2862.pts-0.zyshanlinux-001 (Detached) 2847.pts-0.zyshanlinux-001 (Detached) 3 Sockets in /var/run/screen/S-root.? [root@zyshanlinux-001 ~]# screen -r test_screen ##可以用id或者自定義名字啟動screen##?
擴展
Linux日志文件總管logrotate http://linux.cn/article-4126-1.html
xargs用法詳解 http://blog.csdn.net/zhangfn2011/article/details/6776925
總結
以上是生活随笔為你收集整理的Linux日常运维管理技巧(四)文件同步工具-rsync、Linux系统日志、dmesg命令、lastb命令查看登录失败的用户、screen工具虚拟屏幕的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多线程理解及面试
- 下一篇: 理解Linux下的SELinux(MAC