[toc]linux
10.32/10.33 rsync經過服務同步shell
10.34 linux系統日誌apache
10.35 screen工具vim
這種方式能夠理解爲:在遠程主機上創建一個rsync服務器,在服務器上配置好rsync的各類應用,而後將本機做爲rsync的一個客戶端鏈接遠程rsync服務器,下面是步驟說明:centos
[root@localhost ~]# vim /etc/rsyncd.conf log file=/var/log/rsync.log pid file=/var/run/rsyncd.pid address=192.168.72.132 [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.72.133
[root@localhost ~]# rsync --daemon [root@localhost ~]# ps aux |grep rsync root 4708 0.0 0.0 114656 524 ? Ss 10:59 0:00 rsync --daemon root 4710 0.0 0.0 112676 972 pts/0 S+ 10:59 0:00 grep --color=auto rsync
查看端口是否啓動,netstat -lntp,剛纔在/etc/rsyncd.conf中指定了端口,下圖所示: 安全
爲了方便測試把rsyncd.conf文件內容從新編輯爲 path=/tmp/rsync,新建該臨時目錄並賦予權限bash
[root@localhost ~]# vi /etc/rsyncd.conf [root@localhost ~]# mkdir /tmp/rsync [root@localhost ~]# chmod 777 /tmp/rsync服務器
在須要後臺服務的另一臺機上輸入以下命令:網絡
[root@xavi-002 tmp]# rsync -avP /tmp/xavi.txt 192.168.72.133::test/xavi-02.txt rsync: failed to connect to 192.168.72.133 (192.168.72.133): Connection refused (111) rsync error: error in socket IO (code 10) at clientserver.c(122) [sender=3.0.9]
如今發現這裏有錯誤,沒法執行同步命令,查看下是否存在ip不通的狀況,ping ip地址,而後用telnet查看其網絡狀況,若是沒有telnet,請用yum安裝一下less
[root@xavi-002 ~]# ping 192.168.72.130 PING 192.168.72.130 (192.168.72.130) 56(84) bytes of data. 64 bytes from 192.168.72.130: icmp_seq=1 ttl=64 time=2.44 ms 64 bytes from 192.168.72.130: icmp_seq=2 ttl=64 time=0.592 ms
[root@xavi-002 ~]# telnet 192.168.72.130 873 Trying 192.168.72.130... Connected to 192.168.72.130. Escape character is '^]'. @RSYNCD: 30.0
再次看下iptables是否有問題
[root@xavi-002 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT udp -- virbr0 * 0.0.0.0/0 0.0.0.0/0 udp dpt:53 0 0 ACCEPT tcp -- virbr0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53 0 0 ACCEPT udp -- virbr0 * 0.0.0.0/0 0.0.0.0/0 udp dpt:67 0 0 ACCEPT tcp -- virbr0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:67 8964 14M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 256 16892 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 6416 553K INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0 6416 553K INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0 6416 553K 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 6393 552K 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)
這裏說明咱們要關閉的是firewalld.?????
[root@xavi-002 ~]# systemctl stop firewalld [root@xavi-002 ~]# 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
同時把主機的firewalld服務也關閉
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# iptables -nvL Chain INPUT (policy ACCEPT 21405 packets, 15M bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT udp -- virbr0 * 0.0.0.0/0 0.0.0.0/0 udp dpt:53 0 0 ACCEPT tcp -- virbr0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53 0 0 ACCEPT udp -- virbr0 * 0.0.0.0/0 0.0.0.0/0 udp dpt:67 0 0 ACCEPT tcp -- virbr0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:67 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- * virbr0 0.0.0.0/0 192.168.122.0/24 ctstate RELATED,ESTABLISHED 0 0 ACCEPT all -- virbr0 * 192.168.122.0/24 0.0.0.0/0 0 0 ACCEPT all -- virbr0 virbr0 0.0.0.0/0 0.0.0.0/0 0 0 REJECT all -- * virbr0 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable 0 0 REJECT all -- virbr0 * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain OUTPUT (policy ACCEPT 10672 packets, 784K bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT udp -- * virbr0 0.0.0.0/0 0.0.0.0/0 udp dpt:68
把兩臺機器的firewalld服務關閉後,在來看下telnet命令下端口狀況
[root@xavi-002 ~]# telnet 192.168.72.130 873 Trying 192.168.72.130... Connected to 192.168.72.130. Escape character is '^]'. @RSYNCD: 30.0
這裏表示已經成功鏈接,退出並關閉,輸入「ctrl+]」,而後輸入quit
[root@xavi-002 ~]# telnet 192.168.72.130 873 Trying 192.168.72.130... Connected to 192.168.72.130. Escape character is '^]'. @RSYNCD: 30.0 ^] telnet> quit Connection closed.
再次運行命令,提示要輸入密碼,這是咱們能夠在主機上把/etc/rsyncd.conf配置文件再修改一下,把secret和auth users行加#號註釋掉.
[root@xavi-002 ~]# rsync -avP /tmp/xavi.txt 192.168.72.130::test/xavi-02.txt Password:
再次在從機上運行後臺服務命令,從遠程機器上傳輸到主機上該文件
[root@xavi-002 ~]# rsync -avP /tmp/xavi.txt 192.168.72.130::test/xavi-02.txt sending incremental file list xavi.txt 2391 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) sent 2463 bytes received 27 bytes 711.43 bytes/sec total size is 2391 speedup is 0.96
在主機上查看到了xavi-02文件
[root@localhost ~]# ls /tmp/rsync xavi-02.txt
固然咱們也能夠在從機上把主機的文件拉到從機上
[root@xavi-002 ~]# rsync -avP 192.168.72.130::test/xavi-02.txt /tmp/123.txt receiving incremental file list xavi-02.txt 2391 100% 2.28MB/s 0:00:00 (xfer#1, to-check=0/1) sent 45 bytes received 2497 bytes 221.04 bytes/sec total size is 2391 speedup is 0.94
如今咱們在來回歸一下剛纔編輯/etc/rsyncd.conf的配置內容解析:
log file=/var/log/rsync.log pid file=/var/run/rsyncd.pid address=192.168.72.132 [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.72.133
[root@localhost rsync]# ln -s /etc/passwd /tmp/rsync/test.txt [root@localhost rsync]# ls -l /tmp/rsync/test.txt lrwxrwxrwx 1 root root 11 2月 3 18:10 /tmp/rsync/test.txt -> /etc/passwd
而後再到從機133上執行同步,
[root@xavi-002 ~]# rsync -avL test@192.168.72.130::test/ /tmp/test1/ receiving incremental file list symlink has no referent: "/test.txt" (in test) created directory /tmp/test1 ./ xavi-02.txt sent 48 bytes received 2567 bytes 5230.00 bytes/sec total size is 2391 speedup is 0.91 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1518) [generator=3.0.9]
上例中看出,若是設置use chroot爲ture,則同步軟連接文件會有問題,下面把主機的rsync配置文件修改一下,把ture改成false
在回到從機133上,再次同步
[root@xavi-002 ~]# rsync -avL test@192.168.72.130::test/ /tmp/test1/ receiving incremental file list test.txt sent 45 bytes received 2528 bytes 5146.00 bytes/sec total size is 4782 speedup is 1.86
沒有報錯,問題解決,另外修改玩rsycnd.conf配置文件後不須要重啓rsyncd服務,這是rsync的一個特定機制,配置文件是即時生效的.
[root@xavi-002 ~]# ls -l /tmp/test1/ 總用量 8 -rw-r--r-- 1 root root 2391 1月 7 15:53 test.txt -rw-r--r-- 1 root root 2391 2月 3 11:33 xavi-02.txt
[root@localhost rsync]# killall rsync [root@localhost rsync]# !ps ps aux |grep vmstat root 8382 0.0 0.0 112676 968 pts/1 R+ 20:02 0:00 grep --color=auto vmstat
==未能實現??????????==
這裏咱們看到密碼配置文件在/etc/rsyncd.passwd,編輯它
[root@localhost rsync]# vim /etc/rsyncd.passwd
同時開放它的權限
[root@localhost rsync]# vim /etc/rsyncd.passwd [root@localhost rsync]# chmod 600 !$
[root@xavi-002 ~]# vim /etc/rsync_pass.txt
這樣在寫shell腳本的時候就能夠不用輸入密碼了
日誌記錄了系統天天發生的各類各樣的事情,好比監測系統情況、排查系統故障等,你能夠經過他來檢查錯誤發生的緣由,或者受到攻擊時攻擊者留下的痕跡。日誌的主要功能是審計和監測,還能夠實時的監測系統狀態,監測和追蹤侵入者等等。
[root@localhost ~]# ls /var/log/messages /var/log/messages [root@localhost ~]# less !$
查看其大小
[root@localhost ~]# du -sh !$ du -sh /var/log/messages 900K /var/log/messages
一般,/var/log/messages是在作故障診斷時首先要查看的文件。
系統有一個日誌輪詢的機制,每星期切換一個日誌,切換後的日誌名字相似於messages-20180123.會存放在/var/log/目錄下面,連同messages一共有5個這樣的日誌文件,這裏的20180123就是日期,表示日子切割的年月日.
[root@localhost ~]# ls /var/log/messages* /var/log/messages /var/log/messages-20180123 /var/log/messages-20180204 /var/log/messages-20180115 /var/log/messages-20180128
這是經過logrotate工具的控制來實現的,它的配置文件是/etc/logrotate.conf,(若是沒有特殊需求請不要修改這個配置文件)。
[root@localhost ~]# cat /etc/logrotate.conf # see "man logrotate" for details # rotate log files weekly weekly # keep 4 weeks worth of backlogs rotate 4 //保留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.
上述logrotate.conf配置文件的內容能夠參考英文註釋去理解,/var/log/messages是由rsyslogd這個守護進程產生的,若是中止這個服務則系統不會產生/var/log/messages,因此這個服務不要中止。
[root@localhost ~]# ls /etc/logrotate.d chrony glusterfs libvirtd numad psacct sssd wpa_supplicant cups iscsiuiolog libvirtd.qemu ppp samba syslog yum [root@localhost ~]# 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 }
[root@localhost ~]# 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 (builder@kbuilder.dev.centos.org) (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=12ac0c7f-ed4e-49b8-b862-68d478ecc7e4 ro crashkernel=auto rhgb quiet LANG=zh_CN.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
[root@localhost ~]# ls /var/log/dmesg /var/log/dmesg [root@localhost ~]# less /var/log/dmesg [ 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 (builder@kbuilder.dev.centos.org) (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=12ac0c7f-ed4e-49b8-b862-68d478ecc7e4 ro crashkernel=auto rhgb quiet LANG=zh_CN.UTF-8 [ 0.000000] Disabled fast string operations [ 0.000000] e820: BIOS-provided physical RAM map: [ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009ebff] usable
3.11 last 命令查看Linux的歷史信息,用法以下:
[root@localhost ~]# less /var/log/dmesg [root@localhost ~]# last root pts/0 192.168.72.1 Mon Feb 5 19:15 still logged in reboot system boot 3.10.0-514.el7.x Mon Feb 5 18:51 - 20:37 (01:45) root pts/0 192.168.72.1 Sun Feb 4 16:02 - crash (1+02:49) reboot system boot 3.10.0-514.el7.x Sun Feb 4 15:29 - 20:37 (1+05:07) root pts/1 192.168.72.1 Sat Feb 3 15:21 - crash (1+00:07) root pts/0 192.168.72.1 Sat Feb 3 10:30 - 17:09 (06:38) root pts/0 192.168.72.1 Thu Feb 1 19:59 - 22:44 (02:44) reboot system boot 3.10.0-514.el7.x Thu Feb 1 19:54 - 20:37 (4+00:42)
上例中,從左至右依次爲帳戶名稱、登陸終端、登陸客戶端ip、登陸日期及時長。last命令輸出的信息其實是讀取了二進制日誌文件/var/log/wtmp, 只是這個文件不能直接使用cat, vim, head, tail等工具查看。 。
[root@localhost ~]# lastb btmp begins Thu Feb 1 20:35:01 2018
另一個和登錄信息有關的安全日誌文件爲/var/log/secure, 該日誌文件記錄驗證和受權等方面的信息,好比ssh登錄系統成功或者失敗,都會把相關信息記錄在這個日誌裏
[root@localhost ~]# ls /var/log/secure /var/log/secure [root@localhost ~]# less /var/log/secure Feb 4 17:59:53 localhost polkitd[580]: Registered Authentication Agent for unix-process:3970:905339 (system bus name :1.63 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) Feb 5 18:51:55 localhost polkitd[536]: Loading rules from directory /etc/polkit-1/rules.d Feb 5 18:51:55 localhost polkitd[536]: Loading rules from directory /usr/share/polkit-1/rules.d Feb 5 18:51:55 localhost polkitd[536]: Finished loading, compiling and executing 6 rules Feb 5 18:51:55 localhost polkitd[536]: Acquired the name org.freedesktop.PolicyKit1 on the system bus Feb 5 18:52:03 localhost sshd[865]: Server listening on 0.0.0.0 port 22. Feb 5 18:52:03 localhost sshd[865]: Server listening on :: port 22. Feb 5 19:15:30 localhost sshd[2474]: Accepted password for root from 192.168.72.1 port 51786 ssh2 /var/log/secure...skipping... Feb 4 17:59:53 localhost polkitd[580]: Registered Authentication Agent for unix-process:3970:905339 (system bus name :1.63 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) Feb 5 18:51:55 localhost polkitd[536]: Loading rules from directory /etc/polkit-1/rules.d Feb 5 18:51:55 localhost polkitd[536]: Loading rules from directory /usr/share/polkit-1/rules.d Feb 5 18:51:55 localhost polkitd[536]: Finished loading, compiling and executing 6 rules
有時候咱們須要執行一個命令或腳本,須要幾小時甚至幾天,在這個過程當中,若是中途斷網或者出現有其餘意外狀況怎麼辦,固然也能夠吧命令或者腳本放到後臺運行,不過不保險,沒法在屏幕上隨時查看,這就要用到虛擬終端screen。
[root@localhost ~]# yum install -y screen 已加載插件:fastestmirror, langpacks base | 3.6 kB 00:00:00
[root@localhost ~]# screen [root@localhost ~]# w 20:54:48 up 2:03, 1 user, load average: 0.04, 0.03, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/1 192.168.72.1:S.0 20:54 0.00s 0.03s 0.00s w
[root@localhost ~]# screen -ls There is a screen on: 3451.pts-0.localhost (Attached) 1 Socket in /var/run/screen/S-root.
[root@localhost ~]# screen [detached from 3451.pts-0.localhost]
[root@localhost ~]# screen -r 3451 1 0 0 1330772 932 378496 0 0 23 2 21 31 0 0 100 0 0 0 0 0 1330756 932 378528 0 0 0 0 43 55 0 0 100 0 0 0 0 0 1330756 932 378528 0 0 0 0 24 36 0 0 100 0 0 0 0 0 1330756 932 378528 0 0 0 0 38 50 0 0 99 0 0 0 0 0 1330756 932 378528 0 0 0 0 30 46 0 0 100 0 0
[root@localhost ~]# screen -ls No Sockets found in /var/run/screen/S-root.
[root@localhost ~]# screen [detached from 3670.pts-0.localhost] [root@localhost ~]# screen [detached from 3704.pts-0.localhost] [root@localhost ~]# screen [detached from 3733.pts-0.localhost] [root@localhost ~]# sreen -ls bash: sreen: 未找到命令... [root@localhost ~]# screen -ls There are screens on: 3733.pts-0.localhost (Detached) 3704.pts-0.localhost (Detached) 3670.pts-0.localhost (Detached) 3 Sockets in /var/run/screen/S-root.
[root@localhost ~]# screen -S "test_screen" [root@localhost ~]# sleep 100 [root@localhost ~]# screen -ls There are screens on: 3806.test_screen (Attached) 3733.pts-0.localhost (Detached) 3704.pts-0.localhost (Detached) 3670.pts-0.localhost (Detached) 4 Sockets in /var/run/screen/S-root.