10.32/10.33 rsync通过服务同步10.34 linux系统日志10.35 screen工具

本文介绍rsync服务的详细配置步骤,包括服务端和客户端的设置、免密码传输及日志管理等内容。同时涵盖了Linux系统日志管理、screen工具的使用等实用技巧。

10.32/10.33 rsync通过服务同步

需要开启服务,cs架构(客户端、服务端);服务端:开启一个服务,rsync--daemon,并且需要监听一个端口,默认是873,这个端口可以自定义;客户端:通过端口进行通信

命令格式:

rsync -av test1/ 192.168.133.130::module/dir/

如果把配置信息写到配置路径下,就可以省掉启动服务的配置参数;更改配置不用重启,但配置里面更改了端口将不会生效,需要杀掉服务,重新启动

/etc/rsync.conf

启动服务

添加配置

[root@aminglinux-02 ~]# vim /etc/rsyncd.conf
port=873              //指定在哪个端口启动rsyncd服务,默认是873端口。 
log file=/var/log/rsync.log  //指定日志文件
pid file=/var/run/rsyncd.pid //指定pid文件,这个文件的作用涉及服务的启动、停止等进程管理操作。
address=192.168.133.131 //指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动。
[test]                                  //指定模块名,里面内容自定义。
path=/root/rsync             //指定数据存放的路径。
use chroot=true  //true|false  ;ture 限制同步活动目录只能为path目录,假设有软链接指向path路径外,将报错;false反之
max connections=4            //指定最大的链接数,默认为0,没有限制(多客户端链接的时候,可以使用这个)
read only=no        // 是否只读,true|false;true只能读,使用与上传文件到客户端;false反之
list=true     //true,表示可以列出远程服务端的所有模块,false反之
uid=root    //指定在传输文件时,以哪个用户传输  ;决定于服务端path 定制的路径是否允许(建议写的时候还是用root比较好)
gid=root   //指定在传输文件时,以哪个用户传输
auth users=test   //用户
secrets file=/etc/rsyncd.passwd  //定义传输密码,需要到路径下创建一个密码文件格式为( 用户:密码 )
hosts allow=192.168.133.132 1.1.1.1 2.2.2.2  192.168.133.0/24  //定义,允许那些机器进行同步(多个IP以空格分开),也可以写IP段

启动服务

[root@aminglinux-02 ~]# rsync --daemon

检查一下进程

[root@aminglinux-02 ~]# ps aux |grep rsync
root      3041  0.0  0.0 114644   552 ?        Ss   19:15   0:00 rsync --daemon
root      3045  0.0  0.0 112664   976 pts/0    R+   19:15   0:00 grep --color=auto rsync

检查一下端口情况

[root@aminglinux-02 ~]# 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:80              0.0.0.0:*               LISTEN      1205/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1080/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2298/master
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      1205/nginx: master
tcp        0      0 192.168.133.131:873     0.0.0.0:*               LISTEN      3041/rsync
tcp6       0      0 :::3306                 :::*                    LISTEN      1763/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      1080/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      2298/master

服务打开以后,尝试传输文件

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

提示:没有路由到主机(113) 使用telnet 测试一下端口是否可以通信

[root@localhost ~]# telnet 192.168.133.131 873
-bash: telnet: 未找到命令
[root@localhost ~]# yum install -y telnet
[root@localhost ~]# telnet 192.168.133.131 873
Trying 192.168.133.131...
telnet: connect to address 192.168.133.131: No route to host

还是不通,考虑一下是否iptables影响到了 关闭firewalld 服务(源机器和目标机器都需要关闭)

systemctl stop firewalld

再用telnet测试

[root@localhost ~]# telnet 192.168.133.131 873
Trying 192.168.133.131...
Connected to 192.168.133.131.
Escape character is '^]'. //使用ctrl+] 退出
@RSYNCD: 30.0
^]
telnet> quit   //退出工具
Connection closed.

在B机器上,重新执行同步命令

[root@localhost ~]# rsync -avP /tmp/1/ 192.168.133.131::test/1/
Password:

提示:需要输入密码,因为“secrets file=/etc/rsyncd.passwd //定义传输密码”

到A机器,配置文件注释掉(为了方便测试,先注释掉)

[root@localhost ~]# rsync -avP /tmp/1/ 192.168.133.131::test/1/
sending incremental file list
created directory /1
./
1.txt
        1151 100%    0.00kB/s    0:00:00 (xfer#1, to-check=7/9)
2.txt
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=6/9)
3.txt
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=5/9)
1/
1/1.txt
        1151 100%    1.10MB/s    0:00:00 (xfer#4, to-check=3/9)
1/2.txt
           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=2/9)
1/3.txt
           0 100%    0.00kB/s    0:00:00 (xfer#6, to-check=1/9)
1/4.txt
           0 100%    0.00kB/s    0:00:00 (xfer#7, to-check=0/9)
sent 2744 bytes  received 148 bytes  5784.00 bytes/sec
total size is 2302  speedup is 0.80

当服务端端口号改动,而本机器又没有开放对应端口时,需要使用 --port指定端口号

rsync -avLP --port 8730 192.168.133.130::ttest/tmp

列出服务端的所有模块(受服务端配置文件 list 项配置决定)

指定端口,写上服务端地址,不写模块名

rsync --port 873 192.168.133.130::

免密传输

使用配置里的密码进行传输(常用于计划、脚本自动传输)

在A机器里开启配置的使用用户和密码登录项将会使用密码进行登录,这样不方便,实现自动化

[root@localhost ~]# rsync -avP /tmp/1/ 192.168.133.131::test/1/
Password:
所以,在B机器上配置一个密码文件(只写密码即可)密码权限必须设置为600
vim /etc/rsync_pass.txt

未指定密码文件 --password=/etc/rsync_pass.txt

[root@localhost test]# rsync -avLP /tmp/test/ --port=873   test@192.168.133.131::test/
Password:
sending incremental file list
sent 82 bytes  received 8 bytes  36.00 bytes/sec
total size is 0  speedup is 0.00

指定密码文件

[root@localhost test]# rsync -avLP /tmp/test/ --port=873  --password-file=/etc/rsync_pass.txt test@192.168.133.131::test/
sending incremental file list
./
5.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/5)
6.txt
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=2/5)
7.txt
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=1/5)
8.txt
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=0/5)
sent 241 bytes  received 87 bytes  656.00 bytes/sec
total size is 0  speedup is 0.00

10.34 linux系统日志

系统总日志

/var/log/messages //Linux系统,总的日志;除非定义有单独日志,否则一般都单独记录在这里

日志切割服务

这个文件会经过linux系统的 logrotate 服务进行切割(为了防止这个日志,无限制的增加)
查看配置文件

cat /etc/logrotate.conf
[root@aminglinux-02 1]# 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 //压缩 (为tar.jj)
# 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
}

查看syslog文件

syslog 会对以下几个文件进行切割

[root@aminglinux-02 1]# 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
}

上面的5个日志文件都是由syslogd.pid进程服务决定的,切割日志以后需要重新启动一下这个服务,才会生成新的日志文件,因为切割文件以后,源文件就不存在了

Linux系统服务写文件的时候,是根据一个文件的句柄写的;可以认为就是indoe

dmesg命令

查看硬件信息,这个日志是保存是内存当中的;可以从这个命令输出结果,很直观的查看到硬件错误故障的信息\

dmesg -c //就可以清空

/var/log/dmesg 日志

系统启动时,记录的信息,和dmesg记录的信息很像,但是两者之间并没有关联

last 命令

用于查看正确的登录历史,调用到的日志文件为/var/log/wtmp;(包括reboot),这个日志文件为二进制文件,不能直接使用cat查看,只能使用命令last查看

lastb 命令

用于查看登录失败的历史,调用的是/var/log/btmp;也是二进制文件

安全日志

/var/log/secure 相同于 wtmp和btmp的集合记录

10.35 screen工具

虚拟屏幕,虚拟的终端;为了不让一个任务意外中断;
两个办法
一:
把命令丢到后台,做一个日志输出

nohp [执行命令] &
nohp command &

但后台,不能时时查看到

二:

使用screen 实现

安装

[root@aminglinux-02 1]# yum install -y screen

执行

[root@aminglinux-02 1]# screen
[detached from 2619.pts-0.aminglinux-02]   //按ctrl+a再按d,退出以后就看到这个提示,

查看在运行的虚拟终端

[root@aminglinux-02 1]# screen -ls    
There is a screen on:
    2619.pts-0.aminglinux-02    (Detached)
1 Socket in /var/run/screen/S-root.

回到运行的虚拟终端

screen -r 终端号或者虚拟终端的名字
[root@aminglinux-02 1]# screen -r 2619
[screen is terminating]

退出虚拟终端

在虚拟终端下,输入exit

[root@aminglinux-02 1]# screen -ls
No Sockets found in /var/run/screen/S-root.

创建自定义名字的虚拟终端

screen -S 名字
[root@aminglinux-02 1]# screen -S "test_screen"
[detached from 2658.test_screen]
[root@aminglinux-02 1]# screen -ls
There is a screen on:
    2658.test_screen    (Detached)
1 Socket in /var/run/screen/S-root.

用自定义的名字进入

[root@aminglinux-02 1]# screen -r test_screen

转载于:https://my.oschina.net/nova12315/blog/1661752

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值