2018-06-15 第十六课

2018-06-21  本文已影响0人  chocolee911

目录

  1. rsync
    1.1 语法
    1.2 选项
    1.3 本地同步
    1.4 ssh 方式同步
    1.5 daemon 方式同步
  2. Linux 日志
  3. screen

1. rsync

1.1 语法

rsync [OPTION] SRC DEST
rsync [OPTION] SRC [USER@][host]:DEST
rsync [OPTION] [USER@]HOST:SRC DEST
rsync [OPTION] [USER@]HOST::SRC DEST
rsync [OPTION] SRC [USER@]HOST::DEST
rsync [OPTION] rsync://[USER@]HOST[:PORT]/SRC [DEST]

1.2 选项

1.3 本地同步

[root@localhost ~]# ls /root/source/
aa1  aa2  aa3  aa4  aa5  aa6  aa7  aa8  aa9

[root@localhost ~]# ls /tmp/destination/

[root@localhost ~]# rsync -av /root/source/ /tmp/destination/
sending incremental file list
./
aa1
aa2
aa3
aa4
aa5
aa6
aa7
aa8
aa9
sent 555 bytes  received 190 bytes  1,490.00 bytes/sec
total size is 23  speedup is 0.03

[root@localhost ~]# ls /tmp/destination/
aa1  aa2  aa3  aa4  aa5  aa6  aa7  aa8  aa9

1.4 ssh 方式同步

该方式不必开启 rsync 服务,可以直接利用 ssh 服务进行数据的同步

ps.如果不加远端主机的用户名,默认是以本端当前用户作为远程登录的用户名

[root@localhost ~]# ls /root/source/
aa1  aa2  aa3  aa4  aa5  aa6  aa7  aa8  aa9


[root@localhost ~]# rsync -av --progress /root/source/ root@192.168.1.101:/tmp/destination
The authenticity of host '192.168.1.101 (192.168.1.101)' can't be established.
ECDSA key fingerprint is SHA256:KmYyolIGgz640Ec8tfynePNLyyafUfiD1GmhZRZ+4p4.
ECDSA key fingerprint is MD5:61:3b:a2:0f:6d:ad:82:94:6e:c1:8f:a4:06:b2:3b:e4.
Are you sure you want to continue connecting (yes/no)? yes


Warning: Permanently added '192.168.1.101' (ECDSA) to the list of known hosts.
root@192.168.1.101's password: 


sending incremental file list
./
aa1
             23 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=8/10)
aa2
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=7/10)
aa3
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=6/10)
aa4
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=5/10)
aa5
              0 100%    0.00kB/s    0:00:00 (xfr#5, to-chk=4/10)
aa6
              0 100%    0.00kB/s    0:00:00 (xfr#6, to-chk=3/10)
aa7
              0 100%    0.00kB/s    0:00:00 (xfr#7, to-chk=2/10)
aa8
              0 100%    0.00kB/s    0:00:00 (xfr#8, to-chk=1/10)
aa9
              0 100%    0.00kB/s    0:00:00 (xfr#9, to-chk=0/10)

sent 555 bytes  received 190 bytes  78.42 bytes/sec
total size is 23  speedup is 0.03
root@kali:/tmp# rsync -av --progress root@192.168.1.10:/root/source/ /tmp/destination/
root@192.168.1.10's password: 


receiving incremental file list
./
aa1
             23 100%   22.46kB/s    0:00:00 (xfr#1, to-chk=8/10)
aa2
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=7/10)
aa3
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=6/10)
aa4
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=5/10)
aa5
              0 100%    0.00kB/s    0:00:00 (xfr#5, to-chk=4/10)
aa6
              0 100%    0.00kB/s    0:00:00 (xfr#6, to-chk=3/10)
aa7
              0 100%    0.00kB/s    0:00:00 (xfr#7, to-chk=2/10)
aa8
              0 100%    0.00kB/s    0:00:00 (xfr#8, to-chk=1/10)
aa9
              0 100%    0.00kB/s    0:00:00 (xfr#9, to-chk=0/10)

sent 198 bytes  received 555 bytes  301.20 bytes/sec
total size is 23  speedup is 0.03

root@kali:/tmp# ls /tmp/destination/
aa1  aa2  aa3  aa4  aa5  aa6  aa7  aa8  aa9

目的主机的 sshd 已改变端口号为 2222

root@kali:/tmp# netstat -lntp | grep 22
tcp        0      0 0.0.0.0:2222            0.0.0.0:*               LISTEN      1578/sshd           
tcp6       0      0 :::2222                 :::*                    LISTEN      1578/sshd           
root@kali:/tmp# rm -rf /tmp/destination/*
root@kali:/tmp# ls /tmp/destination/

通过指定端口号的 ssh 方式进行同步:-e "ssh -p PORT"

[root@localhost ~]# rsync -av --progress /root/source/ -e "ssh -p 2222" root@192.168.1.101:/tmp/destination/


root@192.168.1.101's password: 
sending incremental file list
./
aa1
             23 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=8/10)
aa2
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=7/10)
aa3
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=6/10)
aa4
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=5/10)
aa5
              0 100%    0.00kB/s    0:00:00 (xfr#5, to-chk=4/10)
aa6
              0 100%    0.00kB/s    0:00:00 (xfr#6, to-chk=3/10)
aa7
              0 100%    0.00kB/s    0:00:00 (xfr#7, to-chk=2/10)
aa8
              0 100%    0.00kB/s    0:00:00 (xfr#8, to-chk=1/10)
aa9
              0 100%    0.00kB/s    0:00:00 (xfr#9, to-chk=0/10)

sent 555 bytes  received 190 bytes  135.45 bytes/sec
total size is 23  speedup is 0.03

1.5 daemon 方式同步

既然 rsync 通过远程 shell 就能实现两端主机上的文件同步,还要使用rsync的服务干什么?
试想下,你有的机器上有一堆文件需要时不时地同步到众多机器上去,比如目录 a、b 是专门传输到 web 服务器上的,c、d 是专门传输到 ftp 服务器上的,还要对这些目录中的某些文件进行排除,如果通过远程shell连接方式,无论是使用排除规则还是包含规则,甚至一条一条 rsync 命令地传输,这都没问题,但太过繁琐且每次都要输入同样的命令。使用 rsync daemon 就可以解决这种问题。而且,rsync daemon 是向外提供服务的,这样只要告诉了别人 rsync 的 url 路径,外人就能像使用 ftp 服务器一样获取文件列表并进行选择性地下载

1.5.1 daemon 方式的启动
1.5.2 配置文件解释:/etc/rsyncd.conf
********************全局配置参数********************
uid = nobody # rsync 服务的运行用户,默认是nobody,文件传输成功后属主将是这个uid

gid = nobody #用户组

port = 873 #指定rsync端口。默认873

use chroot = no # rsync daemon在传输前是否切换到指定的path目录下,并将其监禁在内

max connections = 200 #最大连接数

timeout = 300 #超时时间

pid file = /var/run/rsyncd.pid #进程对应的进程号文件

lock file = /var/run/rsync.lock #锁文件

log file = /var/log/rsyncd.log #日志文件 出错

********************模块配置参数********************

[backup] #模块名称

path = /backup/ #模块的路径

ignore errors #忽略错误

read only = false #是否制度

list = true #客户端请求显示模块列表时,该模块是否显示出来。默认true

hosts allow = 192.168.90.0/24 #指定允许连接到该模块的机器

host deny = 0.0.0.0/32  #指定不允许连接到该模块的机器,四个0表示谁都可以过来

auth users = rsync_backup #指定连接到该模块的用户列表,只有列表里的用户才能连接到模块,用户名和对应密码保存在secrts file中
# 这里使用的不是系统用户,而是虚拟用户。不设置时,默认所有用户都能连接,但使用的是匿名连接

secrets file = /etc/rsync.password #虚拟账号里面对应的用户和密码
1.5.3 语法
rsync [OPTIONS] [USER@]HOST::SRC  DEST
rsync [OPTIONS] rsync://[USER@]HOST[:PORT]/SRC  DEST

Push

rsync [OPTIONS] SRC  [USER@]HOST::DEST
rsync [OPTIONS] SRC rsync://[USER@]HOST[:PORT]/DEST

连接命令有两种类型,一种是rsync风格使用双冒号的"rsync user@host::src dest",一种是url风格的"rsync://user@host:port/src dest"。对于rsync风格的连接命令,如果想要指定端口号,则需要使用选项"--port"。

1.5.4 示例
  1 # /etc/rsyncd: configuration file for rsync daemon mode
  2 
  3 # See rsyncd.conf man page for more options.
  4 
  5 # configuration example:
  6 
  7 uid = nobody
  8 gid = nobody
  9 use chroot = yes
 10 max connections = 4
 11 pid file = /var/run/rsyncd.pid
 12 secrets file=/etc/rsyncd.passwd
 13 # exclude = lost+found/
 14 # transfer logging = yes
 15 # timeout = 900
 16 # ignore nonreadable = yes
 17 # dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
 18 
 19 [ftp]
 20 path = /ftp
 21 comment = ftp export area
 22 read only = no
 23 auth users=alibaba
[root@localhost ~]# echo "alibaba:12341234" > /etc/rsyncd.passwd 
[root@localhost ~]# cat !$
cat /etc/rsyncd.passwd
alibaba:12341234
root@kali:/tmp# rsync --list-only  alibaba@192.168.1.10::ftp /tmp/destination/
Password: 
drwxr-xr-x            105 2018/06/21 08:30:03 .
-rw-r--r--             23 2018/06/21 08:30:03 aa1
-rw-r--r--              0 2018/06/21 08:29:40 aa2
-rw-r--r--              0 2018/06/21 08:29:40 aa3
-rw-r--r--              0 2018/06/21 08:29:40 aa4
-rw-r--r--              0 2018/06/21 08:29:40 aa5
-rw-r--r--              0 2018/06/21 08:29:40 aa6
-rw-r--r--              0 2018/06/21 08:29:40 aa7
-rw-r--r--              0 2018/06/21 08:29:40 aa8
-rw-r--r--              0 2018/06/21 08:29:40 aa9
root@kali:/tmp/destination# rsync -av --progress /tmp/destination/ alibaba@192.168.1.10::ftp
Password: 
sending incremental file list
rsync: failed to set times on "/." (in ftp): Operation not permitted (1)
./
alibaba
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/2)
rsync: chgrp "/.alibaba.pASDTJ" (in ftp) failed: Operation not permitted (1)

sent 108 bytes  received 196 bytes  86.86 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(1196) [sender=3.1.2]

2. Linux 系统日志

2.1/var/log/messages:几乎所有的开机系统发生的错误都会在此记录

[root@localhost logrotate.d]# head /var/log/messages
Jun 19 15:30:01 localhost rsyslogd: [origin software="rsyslogd" swVersion="8.24.0" x-pid="850" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Jun 19 15:32:01 localhost systemd: Created slice User Slice of chocolee911.
Jun 19 15:32:01 localhost systemd: Starting User Slice of chocolee911.
Jun 19 15:32:01 localhost systemd: Started Session 38 of user chocolee911.
Jun 19 15:32:01 localhost systemd: Starting Session 38 of user chocolee911.
Jun 19 15:32:01 localhost systemd: Removed slice User Slice of chocolee911.
Jun 19 15:32:01 localhost systemd: Stopping User Slice of chocolee911.
Jun 19 15:34:01 localhost systemd: Created slice User Slice of chocolee911.
Jun 19 15:34:01 localhost systemd: Starting User Slice of chocolee911.
Jun 19 15:34:01 localhost systemd: Started Session 39 of user chocolee911.

2.2日志轮循


  1 # see "man logrotate" for details
  2 # rotate log files weekly
  3 weekly
  4 
  5 # keep 4 weeks worth of backlogs
  6 rotate 4
  7 
  8 # create new (empty) log files after rotating old ones
  9 create
 10 
 11 # use date as a suffix of the rotated file
 12 dateext
 13 
 14 # uncomment this if you want your log files compressed
 15 #compress
 16 
 17 # RPM packages drop log rotation information into this directory
 18 include /etc/logrotate.d
 19 
 20 # no packages own wtmp and btmp -- we'll rotate them here
 21 /var/log/wtmp {
 22     monthly
 23     create 0664 root utmp
 24         minsize 1M
 25     rotate 1
 26 }
 27 
 28 /var/log/btmp {
 29     missingok
 30     monthly
 31     create 0600 root utmp
 32     rotate 1
 33 }
 34 
 35 # system-specific logs may be also be configured here.

看一下 bootlog 的 logrotate 设置

[root@localhost logrotate.d]# vim bootlog 

  1 /var/log/boot.log
  2 {
  3     missingok
  4     daily
  5     copytruncate
  6     rotate 7
  7     notifempty
  8 }

配置参数解释:

2.3/var/log/dmesg:内核日志,多与硬件相关

[root@localhost logrotate.d]# head /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-862.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-862.el7.x86_64 root=UUID=e08d3039-e060-47b0-83cf-1f205278e8d3 ro crashkernel=auto rhgb quiet LANG=en_US.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

2.4dmesg:查看硬件信息

[root@localhost logrotate.d]#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-862.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-862.el7.x86_64 root=UUID=e08d3039-e060-47b0-83cf-1f205278e8d3 ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8
[    0.000000] Disabled fast string operations
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] Initializing cgroup subsys cpuset

2.5last/var/log/wtmp:查看成功登录的日志

[root@localhost ~]# last -n 5
root     pts/0        192.168.1.1      Thu Jun 21 08:25   still logged in   
reboot   system boot  3.10.0-862.el7.x Thu Jun 21 08:13 - 10:18  (02:05)    
root     pts/0        192.168.1.1      Wed Jun 20 10:26 - crash  (21:46)    
reboot   system boot  3.10.0-862.el7.x Wed Jun 20 10:24 - 10:18  (23:53)    
root     pts/1        192.168.1.1      Tue Jun 19 14:43 - 14:45  (00:02)    

wtmp begins Tue Jun 12 08:41:39 2018

2.6lastb/var/log/btmp:查看登录失败的日志

[root@localhost ~]# lastb -n 5
papa     ssh:notty    192.168.1.101    Wed Jun 20 11:19 - 11:19  (00:00)    

btmp begins Wed Jun 20 11:19:01 2018

2.7/var/log/secure:记录登录系统存取数据的文件

[root@localhost ~]# head /var/log/secure
Jun 19 17:00:29 localhost polkitd[512]: Registered Authentication Agent for unix-process:2905:864377 (system bus name :1.203 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8)
Jun 20 10:24:59 localhost polkitd[506]: Loading rules from directory /etc/polkit-1/rules.d
Jun 20 10:24:59 localhost polkitd[506]: Loading rules from directory /usr/share/polkit-1/rules.d
Jun 20 10:24:59 localhost polkitd[506]: Finished loading, compiling and executing 2 rules
Jun 20 10:24:59 localhost polkitd[506]: Acquired the name org.freedesktop.PolicyKit1 on the system bus
Jun 20 10:25:55 localhost sshd[850]: Server listening on 0.0.0.0 port 22.
Jun 20 10:25:55 localhost sshd[850]: Server listening on :: port 22.
Jun 20 10:26:47 localhost sshd[1124]: Accepted password for root from 192.168.1.1 port 51749 ssh2
Jun 20 10:26:47 localhost sshd[1124]: pam_unix(sshd:session): session opened for user root by (uid=0)
Jun 20 10:56:08 localhost sshd[1299]: Accepted password for root from 192.168.1.101 port 48398 ssh2

3. screen

Screen是一款由GNU计划开发的用于命令行终端切换的自由软件。用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换。GNU Screen可以看作是窗口管理器的命令行界面版本。它提供了统一的管理多个会话的界面和相应的功能。

3.1 特点

3.2 选项

3.3 语法

screen -S NAME #新建一个叫 NAME 的 session
screen -ls #列出当前所有的 session
screen -r NAME #回到 NAME 这个 session
screen -d NAME #远程 detach 某个 session
screen -d -r NAME #结束当前 session 并回到 NAME 这个 session

3.4 动作(快捷键)

快捷键非常多,仅记最常用的即可

上一篇 下一篇

猜你喜欢

热点阅读