10.32 rsync通过服务同步(上)

 

通过服务同步:

采用C/S架构;

服务端开启rsync服务,默认监听873端口

支持端口自定义;

启动服务前需编辑配置文件/etc/rsyncd.conf(配置文件路径可以自定义);

从默认路径启动服务:rsync –daemon

从自定义路径启动服务:rsync –daemon –configfile=配置文件路径

编辑配置文件:

[root@hyc-01-01 ~]# vim /etc/rsyncd.conf 默认配置文件中的内容全部被注释了

 

# /etc/rsyncd: configuration file for rsync daemon mode

 

# See rsyncd.conf man page for more options.

 

# /etc/rsyncd: configuration file for rsync daemon mode

 

# See rsyncd.conf man page for more options.

 

# configuration example:

 

# uid = nobody

# gid = nobody

# use chroot = yes

# max connections = 4

# pid file = /var/run/rsyncd.pid

# exclude = lost+found/

# transfer logging = yes

# timeout = 900

# ignore nonreadable = yes

# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

 

# [ftp]

#        path = /home/ftp

#        comment = ftp export area

新增内容:

port=873  指定启动rsync服务的端口,默认873

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

pid file=/var/run/rsyncd.pid

address=192.168.31.129 指定启动rsync服务的ip,可以指定一个或多个,若不指定默认在所有ip启动服务

[test] 模块名称

path=/tmp/rsync 将原来的/root/rsync替换,为方便测试,需要创建对应目录并给777权限

数据存放路径

use chroot=true 一个安全参数,设为truersync服务同步时只能在模块的path下活动

use chroot设为true

[root@hyc-01-01 rsync]# ls aa

1.txt  aa.passwd aa.passwd是一个链接到/etc/passwd的软链接

[root@hyc-01-01 tmp]# mv aa /tmp/rsync 移动到test模块的path

[root@hyc-01 test1]# rsync -avLP 192.168.31.129::test/aa/ /tmp/test1/  L参数执行

receiving incremental file list

symlink has no referent: "/aa/aa.passwd" (in test)

./

1.txt

              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/2)

 

sent 46 bytes  received 163 bytes  19.90 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(1650) [generator=3.1.2]

[root@hyc-01 test1]# ls

1.txt 由于不能超出path活动,aa.passwd文件没有同步过来

 

max connections=4

read only=no

list=true

uid=root

gid=root

#auth users=test

#secrets file=/etc/rsyncd.passwd

hosts allow=192.168.31.128

 

[root@hyc-01-01 ~]# mkdir /tmp/rsync

[root@hyc-01-01 ~]# chmod 777 /tmp/rsync

启动服务:

[root@hyc-01-01 ~]# rsync --daemon

 

测试:

[root@hyc-01 ~]# rsync -avP /tmp/2222.txt 192.168.31.129::test/test1/

此时会要求输入密码,为了避免输入密码,可以将配置文件中secrets fileauth users注释掉

执行不成功:

[root@hyc-01 ~]# rsync -avP /tmp/2222.txt 192.168.31.129::test/test1/

rsync: failed to connect to 192.168.31.129 (192.168.31.129): No route to host (113)

rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2]

ping测试连通性正常

安装telnet测试端口873

[root@hyc-01 ~]#yum install -y telnet

[root@hyc-01 ~]# telnet 192.168.31.129 873 端口873不通

Trying 192.168.31.129...

telnet: connect to address 192.168.31.129: No route to host

检查防火墙(可能规则阻止了端口开放):

[root@hyc-01-01 ~]# iptables –nvL 防火墙规则阻止端口开放

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

 pkts bytes target     prot opt in     out     source               destination        

14215   13M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED

    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0          

  160 33858 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0          

  160 33858 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0          

  160 33858 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0          

    2    80 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID

  150 33298 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 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED

    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0          

    0     0 FORWARD_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0          

    0     0 FORWARD_IN_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0          

    0     0 FORWARD_IN_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0          

    0     0 FORWARD_OUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0          

    0     0 FORWARD_OUT_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

    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

 

Chain OUTPUT (policy ACCEPT 1356 packets, 144K bytes)

 pkts bytes target     prot opt in     out     source               destination        

11428 1051K OUTPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0          

 

Chain FORWARD_IN_ZONES (1 references)

 pkts bytes target     prot opt in     out     source               destination        

    0     0 FWDI_work  all  --  ens37  *       0.0.0.0/0            0.0.0.0/0           [goto]

    0     0 FWDI_work  all  --  ens33  *       0.0.0.0/0            0.0.0.0/0           [goto]

    0     0 FWDI_block  all  --  l9     *       0.0.0.0/0            0.0.0.0/0          

    0     0 FWDI_work  all  --  +      *       0.0.0.0/0            0.0.0.0/0           [goto]

检查端口是否开放:

[root@hyc-01-01 ~]# netstat –lntp 873端口没有处于监听状态

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      849/sshd           

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      953/master                

tcp6       0      0 :::22                   :::*                    LISTEN      849/sshd           

tcp6       0      0 ::1:25                  :::*                    LISTEN      953/master     

关闭防火墙:

[root@hyc-01-01 ~]#systemctl stop firewalld 在服务器和客户端均需要执行

telnet测试端口:

[root@hyc-01 ~]# telnet 192.168.31.129 873 端口正常

Trying 192.168.31.129...

Connected to 192.168.31.129.

Escape character is '^]'.

@RSYNCD: 31.0

telnet> q

Connection closed.

重新执行rsync同步:

[root@hyc-01 ~]#rsync -avP /tmp/2222.txt 192.168.31.129::test/test1/

sending incremental file list

 

sent 49 bytes  received 12 bytes  5.81 bytes/sec

total size is 0  speedup is 0.00 执行成功

 

10.33 rsync通过服务同步(下)

 

修改端口后重启服务

1 修改端口号8731122

[root@hyc-01-01 ~]# vim /etc/rsyncd.conf

2 重启rsync服务

[root@hyc-01-01 var]# rsync --daemon config=/tmp/rsync 启动失败

[root@hyc-01-01 ~]# failed to create pid file /var/run/rsyncd.pid: File exists 报错

[root@hyc-01-01 ~]# rm -rf /var/run/rsyncd.pid 解决办法

[root@hyc-01-01 var]# rsync --daemon config=/tmp/rsync

[root@hyc-01-01 var]# netstat -lntp

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      849/sshd           

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      953/master         

tcp        0      0 192.168.31.129:1122     0.0.0.0:*               LISTEN      10724/rsync        

tcp6       0      0 :::22                   :::*                    LISTEN      849/sshd           

tcp6       0      0 ::1:25                  :::*                    LISTEN      953/master  

3 再次同步

[root@hyc-01 tmp]# rsync -avLP --port=1122 192.168.31.129::test/aa/ /tmp/ 指定端口号执行同步

receiving incremental file list

./

1.txt

              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=1/3)

aa.passwd

            883 100%  862.30kB/s    0:00:00 (xfr#2, to-chk=0/3)

 

sent 65 bytes  received 1,059 bytes  2,248.00 bytes/sec

total size is 883  speedup is 0.79

 

max connections:指定最大连接数,默认为0(没有限制)

read only将数据写到服务端时(推数据),若服务端定义为只读true,则数据无法写入

 

listlisttrue则列出可用模块,false则不列出

list=true

[root@hyc-01 tmp]# rsync --port=1122 192.168.31.129:: 列出远端服务器的可用模块

test 

list=false

[root@hyc-01 tmp]# rsync --port=1122 192.168.31.129::

[root@hyc-01 tmp]#

 

uid/gid:以哪个用户或组的身份同步文件

uidgid都修改为nobody

[root@hyc-01 tmp]# rsync -avLP /tmp/aa/ --port=1122 192.168.31.129::test/

sending incremental file list

rsync: failed to set times on "." (in test): Operation not permitted (1)

./

aa.passwd

            846 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/2)

rsync: chgrp ".aa.passwd.fcuSN4" (in test) failed: Operation not permitted (1)

 

sent 964 bytes  received 198 bytes  110.67 bytes/sec

total size is 846  speedup is 0.73

rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]

 

[root@hyc-01-01 aa]# id nobody nobody在系统中拥有很小的权限

uid=99(nobody) gid=99(nobody) =99(nobody)

[root@hyc-01-01 rsync]# ls -ld

drwxr-xr-x. 4 root root 46 7  21 10:10 .

[root@hyc-01-01 rsync]# pwd

/tmp/rsync

执行同步时由于权限不足被拒绝

 

auth users指定传输时使用的用户名

编辑/etc/rsypncd.conf指定auth_users=root即使用root用户同步文件

 

secrets file:指定密码文件

[root@hyc-01-01 ~]# vim /etc/rsyncd.conf 在文件中指明认证密码文件的路径

[root@hyc-01-01 ~]# vim /etc/rsyncd.passwd 编辑文件为指定用户添加密码

[root@hyc-01-01 ~]# chmod 600 !$ 密码文件权限修改为600

chmod 600 /etc/rsyncd.passwd

[root@hyc-01 ~]# rsync -avP /tmp/aa/ --port=1122 hyc@192.168.31.129::test/

Password:  指定hyc用户执行同步时需要输入密码

sending incremental file list

 

sent 76 bytes  received 12 bytes  5.33 bytes/sec

总结:

默认使用uid的用户执行同步,此时输入uid用户在passwd文件的密码;

使用auth_users的用户需要用户名@”特殊指明;

所有可以用于同步的用户必须写在auth_users中(包括uid);

 

在客户端写好认证密码,执行rsync同步时不需要再手工输入密码

1 在客户端编辑一个密码文件/etc/rsync_pass.txt输入认证时使用的密码(仅在一行输入一串密码即可)

2 将编辑的密码文件权限设为600

3 执行同步时指定客户机上的密码文件,此时不需要再手工输入密码

[root@hyc-01 etc]# vim rsync_pass.txt

[root@hyc-01 etc]# chmod 600 /etc/rsync_pass.txt

[root@hyc-01 etc]# rsync -avP /tmp/aa/ --port=1122 --password-file=/etc/rsync_pass.txt hyc@192.168.31.129::test/

 

hosts allow允许哪些机器与本地同步文件,包括推文件或拉文件,后跟机器的ip地址,有多个ipip地址间用空格隔开,或写ip段(192.168.1.0/24

 

10.34 linux系统日志

 

出现报错或执行命令不成功时多关注日志信息

 

/var/log/messages

Linux系统总日志;

除非部分服务有定义单独的日志,否则几乎所有日志信息都记录在该文件中;

[root@hyc-01 ~]# du -sh /var/log/messages

184K         /var/log/messages

日志切割机制:

Linux中有专门用来切割日志的服务(logrotate);

当日志文件增大到一定量后,服务会自动将日志文件切割:

[root@hyc-01 ~]# ls /var/log/messages*

/var/log/messages  /var/log/messages-20180716

logrotate配置文件:

[root@hyc-01 ~]# cat /etc/logrotate.conf

# see "man logrotate" for details 使用man查看帮助文档获得更多细节

# 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.

 

[root@hyc-01 ~]# ls /etc/logrotate.d 该目录下的配置文件(logrotate相关)

bootlog  chrony  syslog  wpa_supplicant  yum

 

[root@hyc-01 ~]# 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

切割时,原messages日志文件被改名(加上了日期);

此时系统会生成一个新的messages文件,新的日志信息将写入该文件;

Linux系统会根据文件句柄写文件(inode号),文件句柄决定了文件唯一的特性;

虽然创建了新的messages文件,但服务仍然会向原inode号的文件写入日志内容,所以需要重启syslogd服务,指定新的文件来写入日志信息;

    endscript

}

 

命令dmesg

 

列出系统中硬件相关的日志,这些日志信息被保存在内存中

[root@hyc-01 logrotate.d]# dmesg

-c 会将内存中的硬件相关日志清空,再使用命令查看时显示为空

[root@hyc-01 logrotate.d]# dmesg –c

 

/var/log/dmesg

记录系统启动时的信息,与dmesg命令没有关系

 

last命令

调用文件/var/log/wtmp

用于查看用户正确的登录历史信息(包括重启时的信息)

包括登录的用户,登录的终端,登录ip,登录时间、登出时间登信息

 

lastb命令

调用文件/var/log/btmp

用于查看用户登录失败的历史信息

当有人爆破服务器时此处会有大量日志

 

/var/log/secure

安全相关日志信息

当使用某用户登录失败(输错密码)时此处会有记录

登录失败产生的日志信息:

Jul 22 10:19:29 hyc-01-01 sshd[12200]: Failed password for root from 192.168.31.128 port 51560 ssh2

Jul 22 10:19:30 hyc-01-01 sshd[12200]: Failed password for root from 192.168.31.128 port 51560 ssh2

Jul 22 10:19:30 hyc-01-01 sshd[12200]: Connection closed by 192.168.31.128 port 51560 [preauth]

 

10.35 screen工具

 

关闭窗口或断开终端会使一个正在运行的任务被杀死

 

需求:

执行一个用时较长的脚本,并且会输出一些内容,所以脚本无法中断;

远程登录服务器会用到网络,一旦网络断开,用户与服务器的连接就会断开,此时该用户执行的脚本会中断;

解决办法:

将任务丢到后台执行,并指定将日志信息输出到某个文件

解决任务中断问题:

[root@hyc-01-01 ~]# nohup command & 将任务丢到后台执行即使与终端连接断开也会继续执行(command为执行的命令)

由于任务在后台,此时无法实时查看任务输出的内容

即使指定了记录日志信息,但屏幕上输出的信息无法实时查看

 

screen虚拟终端

用户可以将命令放到screen下执行,然后返回正常的终端执行其他操作,需要查看或其他操作时再将screen调回来

 

安装screen

[root@hyc-01-01 ~]# yum install -y screen

进入screen虚拟终端:

[root@hyc-01-01 ~]# screen

screen调到后台:

快捷键:ctrl+A再按D(不能同时按ctrlAD同时按)

查看screen信息:

[root@hyc-01-01 ~]# screen -lsn

There are screens on:

         12285.pts-0.hyc-01-01   (Detached)

         12298.pts-0.hyc-01-01   (Detached)

         12327.pts-0.hyc-01-01   (Detached)

         12341.pts-0.hyc-01-01   (Detached)

4 Sockets in /var/run/screen/S-root.

[root@hyc-01-01 ~]# screen -ls

There are screens on:

         12285.pts-0.hyc-01-01   (Detached)

         12298.pts-0.hyc-01-01   (Detached)

         12327.pts-0.hyc-01-01   (Detached)

         12341.pts-0.hyc-01-01   (Detached)

4 Sockets in /var/run/screen/S-root.

重新进入指定的screen

[root@hyc-01-01 ~]# screen –r 指定PID(后跟PID则进入特定PIDscreen,否则默认进入screen –ls中最末位的screen

[root@hyc-01-01 ~]# screen -r test_screen 进入特定名称的screen

自定义screen名称:

[root@hyc-01-01 ~]# screen -S "test_screen" 此时-ls中最末位的screen被定义,若最末位已被定义了名称,则新建一个screen命名

[root@hyc-01-01 ~]# screen -ls

There are screens on:

         12285.pts-0.hyc-01-01   (Detached)

         12298.pts-0.hyc-01-01   (Detached)

         12327.pts-0.hyc-01-01   (Detached)

         12341.pts-0.hyc-01-01   (Detached)

         12372.test_screen (Detached)

5 Sockets in /var/run/screen/S-root.

关闭screen会话:

使用-r选项进入screen

[root@hyc-01-01 ~]# exit 关闭会话