rsync服務、系統日誌、screen工具

11月1日任務
10.32/10.33 rsync經過服務同步
10.34 linux系統日誌
10.35 screen工具
 

rsync經過服務同步

須要開啓一個服務,採用C/S架構,服務器端開啓rsync服務,並開啓端口(默認873);而後客戶端就能夠經過該端口與服務器端開始通訊。mysql

配置文件/etc/rsyncd.conf

啓動服務前須要編輯配置文件linux

# rsyncd.conf樣例
# 服務的默認端口號,能夠修改,但需重啓才能生效
# 修改默認端口後,同步時需加--port參數
port=873    

# 指定log日誌文件
log file=/var/log/rsync.log

# 指定pid文件
pid file=/var/run/rsyncd.pid

# 指定監聽的ip
address=192.168.133.130

# 模塊名
[test]

# 指定數據存放的目錄
path=/root/rsync

# 表示在傳輸文件前首先chroot到path參數所指定的目錄下。這樣作的緣由是實現額外的安全防禦,
# 但缺點是須要以roots權限,而且不能備份指向外部的符號鏈接所指向的目錄文件。

# 修改該值無需重啓服務就會生效
use chroot=true

# 最大同時鏈接客戶端數,默認爲0,及不限制
max connections=4

# 
read only=true

# list爲true時,執行rsync 192.168.65.133:: 回車會返回模塊名;若爲false就不會顯示
list=true

# 指定傳輸文件時使用的身份
uid=root
gid=root

# 指定傳輸時要使用的用戶名
auth users=test

# 指定密碼文件,配合auth使用
secrets file=/etc/rsyncd.passwd

# 容許鏈接的主機(能夠是多個,也能夠是一個網段的)
hosts allow=192.168.133.134 1.1.1.1 2.2.2.2  192.168.133.0/24

建立配置文件中指定的目錄,並修改權限git

[root@server ~]# mkdir /tmp/rsync
[root@server ~]# chmod 777 !$
chmod 777 /tmp/rsync
[root@server ~]# ls -ld /tmp/rsync/
drwxrwxrwx. 2 root root 6 12月  6 20:59 /tmp/rsync/

啓動rsync服務

服務器端啓動服務rsync --daemon(默認狀況下 )sql

[root@server ~]# rsync --daemon
[root@server ~]# ps aux | grep rsync
root       2592  0.0  0.0 114656   528 ?        Ss   20:58   0:00 rsync --daemon
root       2594  0.0  0.0 112680   972 pts/0    S+   20:58   0:00 grep --color=auto rsync
[root@server ~]# netstat -ltnp
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      890/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2153/master         
tcp        0      0 192.168.65.134:873      0.0.0.0:*               LISTEN      2592/rsync          
tcp6       0      0 :::3306                 :::*                    LISTEN      2042/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      890/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      2153/master  

## 若是rsyncd.conf文件存儲到了其餘路徑,那麼使用時須要加上--conf-file=路徑/rsyncd.conf

同步文件(這裏已經將鏈接問題解決了)centos

[root@client ~]# rsync -avP /tmp/1.txt 192.168.65.134::test/server.txt
sending incremental file list
1.txt
        1082 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 1151 bytes  received 27 bytes  2356.00 bytes/sec
total size is 1082  speedup is 0.92

rsyncd.conf參數說明

  • use chroot
# 默認狀況下chroot值爲true,若是你的數據當中有軟鏈接文件,建議你設置成false。在rsync命令指定-L參數後,將沒法將鏈接所指的源文件同步

[root@server ~]# ls -l /tmp/rsync/passwd.ln 
lrwxrwxrwx. 1 root root 11 12月  6 21:24 /tmp/rsync/passwd.ln -> /etc/passwd

# 加上-L參數也沒法將連接文件所指的文件同步過來
[root@client ~]# rsync -avPL 192.168.65.134::test/passwd.ln /tmp/1.ln
receiving incremental file list
rsync: link_stat "/passwd.ln" (in test) failed: No such file or directory (2)

sent 4 bytes  received 8 bytes  8.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1518) [Receiver=3.0.9]

# 修改chroot=false後,就能夠同步源文件了!
[root@client ~]# rsync -avPL 192.168.65.134::test/passwd.ln /tmp/1.ln
receiving incremental file list
passwd.ln
        1082 100%    1.03MB/s    0:00:00 (xfer#1, to-check=0/1)

sent 45 bytes  received 1186 bytes  2462.00 bytes/sec
total size is 1082  speedup is 0.88

[root@server ~]# ls -l /tmp/rsync/1.ln 
-rw-r--r--. 1 root root 1082 11月 28 21:25 /tmp/rsync/1.ln
  • read only參數
# 服務端設置了read only爲true,客戶端將沒法推(同步)數據到服務端!
  • port參數
# rsync服務默認端口爲873
# 若是修改了rsyncd.conf內的端口,能夠在同步時添加--port參數來開放端口
# 修改了端口的rsyncd服務須要重啓才能生效!!
[root@client ~]# rsync -avP --port 8730 192.168.65.134::test/1.txt /tmp/test
receiving incremental file list
1.txt
        1082 100%    1.03MB/s    0:00:00 (xfer#1, to-check=0/1)

sent 45 bytes  received 1186 bytes  2462.00 bytes/sec
total size is 1082  speedup is 0.88
  • list參數
# 該參數是安全選項,最好設置爲false
# 設置爲true
[root@client ~]# rsync -avP 192.168.65.134::
test

# 設置爲false,不顯示模塊名,更安全
[root@client ~]# rsync -avP 192.168.65.134::
  • uid/gid參數
# 指定傳輸時使用的用戶,本處設置爲了root,這樣同步後的文件的全部者和全部組都會使root
  • auth users和secrets file參數
rsyncd.conf裏設置了auth users和secret file參數的,使用下列方式同步,明確用戶名,同時須要輸入密碼:
[root@centos7 ~]# rsync -avP /root/test.cap test@192.168.65.133::test/test.txt
Password:
sending incremental file list
test.cap
         546 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 635 bytes  received 27 bytes  1324.00 bytes/sec

也能夠在服務端經過指定密碼文件的方式來免輸密碼:在/tmp/rsync_pass
[root@client ~]# vi /tmp/rsync_pass
輸入  "模塊名:密碼"
保存退出


#避免客戶端手動輸入密碼的方法:

在客戶端建立一個密碼文件/etc/rsync_passwd,權限改成600,文件內只放入密碼便可;而後同步是使用--password-file=/tmp/rsync_pass 指定
[root@client ~]# vi /tmp/rsync_pass
輸入test用戶的密碼便可(跟服務器密碼文件不一樣)
保存退出

[root@centos7 ~]# rsync -avP /root/test.cap test@192.168.65.133::test/test.txt
sending incremental file list
test.cap
         546 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 635 bytes  received 27 bytes  1324.00 bytes/sec
  • host allow參數
容許哪些主機來進行rsync同步,多個ip使用空格鏈接

錯誤和排查

鏈接出現以下錯誤安全

[root@centos7 ~]# rsync -avP /root/test.cap 192.168.65.133::test/test.txtrsync: failed to connect to 192.168.65.133 (192.168.65.133): No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(122) [sender=3.0.9]

檢測排錯思路:服務器

  1. 網絡是否連通 ping
[root@centos7 ~]# ping 192.168.65.133
PING 192.168.65.133 (192.168.65.133) 56(84) bytes of data.
64 bytes from 192.168.65.133: icmp_seq=1 ttl=64 time=0.248 ms
64 bytes from 192.168.65.133: icmp_seq=2 ttl=64 time=0.295 ms
^C
--- 192.168.65.133 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.248/0.271/0.295/0.028 ms

# 說明ip是通的
  1. 端口是否通telnet
# 格式 telnet IP PORT 檢測目標的端口是否可通
[root@client ~]# telnet 192.168.65.134 873
Trying 192.168.65.134...
telnet: connect to address 192.168.65.134: No route to host

# 說明端口不通

# 若鏈接成功,使用ctrl+]、ctrl+d退出telnet模式
  1. 查看iptables
[root@centos7 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 5069   12M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
   36  2356 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
   63  3336 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 3933 packets, 233K bytes)
 pkts bytes target     prot opt in     out     source               destination   
 
 # 執行 systemctl stop firewalld命令關閉防火牆(客戶端、服務器端都關閉)
 # 而後驗證telnet
 [root@client ~]# telnet 192.168.65.134 873
Trying 192.168.65.134...
Connected to 192.168.65.134.
Escape character is '^]'.
@RSYNCD: 30.0
^]
telnet> Connection closed.

Linux系統日誌

/var/log/messages 系統日誌(不包括已指定本身的日誌的的服務)

記錄的內容比較複雜,包括服務的開始、中止等,隨時間的變化,這個文件會變的很大,須要進行日誌的切割:logrotate命令網絡

# 自動切換後的messages日誌
[root@client ~]# ls -l /var/log/messages*
-rw-------. 1 root root    9259 12月  6 21:50 /var/log/messages
-rw-------. 1 root root  468677 11月 13 21:38 /var/log/messages-20171113
-rw-------. 1 root root  274123 11月 21 20:10 /var/log/messages-20171121
-rw-------. 1 root root  136526 11月 28 20:40 /var/log/messages-20171128
-rw-------. 1 root root 1081595 12月  6 21:46 /var/log/messages-20171206
  • logrotate命令的配置文件
[root@client ~]# cat /etc/logrotate.conf 
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs 保留4個星期(即1個月)的日誌文件
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.
  • 對系統日誌的自動切割操做
[root@client ~]# 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
}

dmesg命令:列出系統中硬件相關的日誌

硬盤損壞、網卡故障等會出如今該日誌中架構

demsg命令顯示的日誌是存儲在內存中的,使用dmesg -c命令能夠清空,後續出現故障或重啓主機後,該日誌將再也不爲空ssh

[root@client ~]# dmesg
...
[   37.108427] IPv6: ADDRCONF(NETDEV_UP): ens37: link is not ready
[   37.113334] e1000: ens37 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[  110.845838] e1000: ens33 NIC Link is Down
[  115.218991] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[ 1086.395698] sched: RT throttling activated
[ 3673.122093] e1000: ens33 NIC Link is Down
[ 3679.139262] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None

/var/log/dmesg

last命令:成功登陸服務器的用戶記錄

[root@client ~]# last
root     pts/1        192.168.65.1     Wed Dec  6 20:37 - 20:38  (00:00)    
root     pts/0        192.168.65.1     Wed Dec  6 20:23   still logged in   
root     pts/0        192.168.65.1     Tue Dec  5 18:50 - 19:43  (00:53)    
...

last命令調用的是/var/log/wtmp文件,該文件是二進制文件,沒法直接查看

與之相對應的還有一個命令:lastb --> 用來查看登陸失敗的用戶記錄;其調用的文件是/var/log/btmp,一樣沒法直接查看內容。

/var/log/secure文件

記錄登陸系統的信息,包括登陸成功的、失敗的,經過查看該文件能夠發現服務器是否非正常登陸(暴力破解)

screen工具基本使用

screen命令能夠看作是一個虛擬的屏幕(終端),能夠在遠程登陸過程當中不會由於意外(網絡)而致使任務的中斷,既能夠在後臺運行,也能夠調到前臺運行。

  • 安裝
[root@client ~]# yum install -y screen
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.zju.edu.cn
 * epel: mirrors.ustc.edu.cn
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.zju.edu.cn
...
已安裝:
  screen.x86_64 0:4.1.0-0.23.20120314git3c2946.el7_2           

完畢!
  • 使用 直接輸入screen後回車,就進入了screen虛擬終端

將screen放到後臺運行:ctrl+A 再+d

[root@centos ~]# screen
[detached from 4530.pts-0.client]
  • 查看當前由多少個screen在後臺運行
[root@client ~]# screen -ls
There are screens on:
	4565.pts-0.client	(Detached)
	4530.pts-0.client	(Detached)
2 Sockets in /var/run/screen/S-root.
  • 進入指定的screen
[root@client ~]# screen -r 4530
[detached from 4530.pts-0.client]
  • 定義自定義的screen
[root@client ~]# screen -S "test"
[detached from 4594.test]
[root@client ~]# screen -ls
There are screens on:
	4594.test	(Detached)
	4565.pts-0.client	(Detached)
	4530.pts-0.client	(Detached)
3 Sockets in /var/run/screen/S-root.
[root@client ~]# screen -r test
[detached from 4594.test] # 未退出
  • 退出screen:進入後輸入exit退出
[root@client ~]# screen -r test
[screen is terminating] #退出狀態
相關文章
相關標籤/搜索