2018-06-15 第十六课

目录

  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
  • 通过SSH方式同步
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 选项

  • -a:等于 -rlptgoD
  • -v:输出详细信息
  • -P:显示更加详细的信息,如速率等
  • -r:递归模式
  • -p:保持文件权限
  • -o:保持文件属主
  • -g:保持文件属组
  • -l:保留软链接
  • -L:使用软链接的目标文件替代软链接
  • -u:仅当源文件比目标文件更加新时才进行覆盖
  • -D:保持设备文件信息
  • -t:保持文件时间信息
  • -e:指定使用 ssh 方式进行数据同步
  • -z:以压缩的方式进行文件的传输
  • --port=PORT:指定其他的 rsync 服务端口
  • --delete:删除目标目录中,源中没有的文件
  • --exclude=PATTERN:指定排除不需要传输的文件模式
  • --progress:显示备份过程
  • --password-file=FILE:指定模块的密码文件

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.如果不加远端主机的用户名,默认是以本端当前用户作为远程登录的用户名

  • 源主机将数据 PUSH 到目的主机上
[root@localhost ~]# ls /root/source/
aa1  aa2  aa3  aa4  aa5  aa6  aa7  aa8  aa9


[root@localhost ~]# rsync -av --progress /root/source/ [email protected]:/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.
[email protected]'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
  • 目的主机从源主机上 PULL 数据到本地
root@kali:/tmp# rsync -av --progress [email protected]:/root/source/ /tmp/destination/
[email protected]'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

  • 在 ssh 同步方式中指定端口号

目的主机的 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" [email protected]:/tmp/destination/


[email protected]'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 语法
  • PULL
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  [email protected]::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
  • 在本端上,将文件 PUSH 到远端
root@kali:/tmp/destination# rsync -av --progress /tmp/destination/ [email protected]::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日志轮循

  • /etc/logrotate.conf:logrotate 的配置文件,通常不需要对它进行修改。日志文件的轮循设置在独立的配置文件中,它(们)放在/etc/logrotate.d/目录下。

  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.
  • /etc/logrotat.d/:日志文件的轮循设置在独立的配置文件中,这些文件存放在/etc/logrotate.d/目录下。

看一下 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 }

配置参数解释:

  • compress: 通过gzip压缩转储以后的日志
  • nocompress: 不压缩
  • copytruncate: 用于还在打开中的日志文件,把当前日志备份并截断
  • nocopytruncate: 备份日志文件但是不截断
  • create mode owner group: 转储文件,使用指定的文件模式创建新的日志文件
  • nocreate: 不建立新的日志文件
  • delaycompress 和 compress: 一起使用时,转储的日志文件到下一次转储时才压缩
  • nodelaycompress: 覆盖 delaycompress 选项,转储同时压缩。
  • errors address: 专储时的错误信息发送到指定的Email 地址
  • ifempty: 即使是空文件也转储,这个是 logrotate 的缺省选项。
  • notifempty: 如果是空文件的话,不转储
  • mail address: 把转储的日志文件发送到指定的E-mail 地址
  • nomail: 转储时不发送日志文件
  • olddir directory: 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
  • noolddir: 转储后的日志文件和当前日志文件放在同一个目录下
  • prerotate/endscript: 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
  • postrotate/endscript: 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
  • daily: 指定转储周期为每天
  • weekly: 指定转储周期为每周
  • monthly: 指定转储周期为每月
  • rotate count: 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
  • tabootext [+] list: 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~
  • size size: 当日志文件到达指定的大小时才转储,bytes(缺省)及KB(sizek)或MB(sizem)

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 ([email protected]) (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 ([email protected]) (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 特点

  • 会话恢复
    只要Screen本身没有终止,在其内部运行的会话都可以恢复。这一点对于远程登录的用户特别有用——即使网络连接中断,用户也不会失去对已经打开的命令行会话的控制。只要再次登录到主机上执行screen -r就可以恢复会话的运行。同样在暂时离开的时候,也可以执行分离命令detach,在保证里面的程序正常运行的情况下让Screen挂起(切换到后台)。这一点和图形界面下的VNC很相似。
  • 多窗口
    在Screen环境下,所有的会话都独立的运行,并拥有各自的编号、输入、输出和窗口缓存。用户可以通过快捷键在不同的窗口下切换,并可以自由的重定向各个窗口的输入和输出。Screen实现了基本的文本操作,如复制粘贴等;还提供了类似滚动条的功能,可以查看窗口状况的历史记录。窗口还可以被分区和命名,还可以监视后台窗口的活动。 会话共享 Screen可以让一个或多个用户从不同终端多次登录一个会话,并共享会话的所有特性(比如可以看到完全相同的输出)。它同时提供了窗口访问权限的机制,可以对窗口进行密码保护。

3.2 选项

  • -A:  将所有的视窗都调整为目前终端机的大小。
  • -d <作业名称>: 将指定的screen作业离线。
  • -h <行数>:  指定视窗的缓冲区行数。
  • -m:  即使目前已在作业中的screen作业,仍强制建立新的screen作业。
  • -r <作业名称>:  恢复离线的screen作业。
  • -R:  先试图恢复离线的作业。若找不到离线的作业,即建立新的screen作业。
  • -s:  指定建立新视窗时,所要执行的shell。
  • -S <作业名称>:  指定screen作业的名称。
  • -v:  显示版本信息。
  • -x:  恢复之前离线的screen作业。
  • -ls或--list:  显示目前所有的screen作业。
  • -wipe:  检查目前所有的screen作业,并删除已经无法使用的screen作业。

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 动作(快捷键)

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

  • Ctrl+a+d:detach,暂时离开当前 session,将目前的 screen session (可能含有多个 windows) 丢到后台执行,并会回到还没进 screen 时的状态,此时在 screen session 里,每个 window 内运行的 process (无论是前台/后台)都在继续执行,即使 logout 也不影响

你可能感兴趣的:(2018-06-15 第十六课)